`
snoopy7713
  • 浏览: 1125960 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

spring-security3 入门篇

阅读更多

1.下载spring security的最新版本,工程下载的是3.1

2. 新建工程,结构如下:



 其中,涉及到的jar包可以在spring-security包中的例子中获取

3、配置spring-security.xml

Xml代码  收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < beans   xmlns = "http://www.springframework.org/schema/beans"   
  3.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   xmlns:security = "http://www.springframework.org/schema/security"   
  4.     xsi:schemaLocation ="http://www.springframework.org/schema/beans   
  5.             http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.             http://www.springframework.org/schema/security   
  7.             http://www.springframework.org/schema/security/spring-security.xsd">   
  8.   
  9.     <!-- 保护应用程序的所有URL,只有拥有ROLE_USER才可以访问 -->   
  10.     < security:http   auto-config = "true" >   
  11.         < security:intercept-url   pattern = "/**"   access = "ROLE_USER"   />   
  12.     </ security:http >   
  13.       
  14.     <!--配置认证管理器,只有用户名为user,密码为user的用户,角色为ROLE_USER可访问指定的资源 -->   
  15.     < security:authentication-manager >   
  16.         < security:authentication-provider >   
  17.             < security:user-service >   
  18.                 < security:user   name = "user"    password = "user"   authorities = "ROLE_USER" />   
  19.             </ security:user-service >   
  20.         </ security:authentication-provider >   
  21.     </ security:authentication-manager >   
  22. </ beans >   

 4.配置web.xml

Xml代码  收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < web-app   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   xmlns = "http://java.sun.com/xml/ns/javaee"   xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   id = "WebApp_ID"   version = "2.5" >   
  3.   < display-name > springSecurity </ display-name >   
  4.     <!--******************************** -->   
  5.     <!--*******log4j日志信息的配置****** -->   
  6.     <!--******************************* -->   
  7.     < context-param >   
  8.         < param-name > log4jConfigLocation </ param-name >   
  9.         < param-value > classpath:log4j.xml </ param-value >   
  10.     </ context-param >   
  11.     <!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond,可以不设置 -->   
  12.     < context-param >   
  13.         < param-name > log4jRefreshInterval </ param-name >   
  14.         < param-value > 60000 </ param-value >   
  15.     </ context-param >   
  16.   
  17.     <!--******************************** -->   
  18.     <!--*******spring bean的配置******** -->   
  19.     <!--******************************* -->   
  20.     < context-param >   
  21.         < param-name > contextConfigLocation </ param-name >   
  22.         < param-value > classpath:applicationContext.xml </ param-value >   
  23.     </ context-param >   
  24.       
  25.     < listener >   
  26.         < listener-class > org.springframework.web.util.Log4jConfigListener </ listener-class >   
  27.     </ listener >   
  28.     < listener >   
  29.         < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >   
  30.     </ listener >   
  31.     < listener >   
  32.         < listener-class > org.springframework.web.util.IntrospectorCleanupListener </ listener-class >   
  33.     </ listener >   
  34.     <!--******************************** -->   
  35.     <!--*******字符集 过滤器************ -->   
  36.     <!--******************************* -->   
  37.     < filter >   
  38.         < filter-name > CharacterEncodingFilter </ filter-name >   
  39.         < filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class >   
  40.         < init-param >   
  41.             < param-name > encoding </ param-name >   
  42.             < param-value > UTF-8 </ param-value >   
  43.         </ init-param >   
  44.         < init-param >   
  45.             < param-name > forceEncoding </ param-name >   
  46.             < param-value > true </ param-value >   
  47.         </ init-param >   
  48.     </ filter >   
  49.     < filter-mapping >   
  50.         < filter-name > CharacterEncodingFilter </ filter-name >   
  51.         < url-pattern > /* </ url-pattern >   
  52.     </ filter-mapping >   
  53.   
  54.     <!--******************************** -->   
  55.     <!--*******session的配置************ -->   
  56.     <!--******************************* -->   
  57.     < session-config >   
  58.         < session-timeout > 30 </ session-timeout >   
  59.     </ session-config >   
  60.       
  61.     <!-- SpringSecurity必须的begin -->   
  62.     < filter >   
  63.         < filter-name > springSecurityFilterChain </ filter-name >   
  64.         < filter-class > org.springframework.web.filter.DelegatingFilterProxy </ filter-class >   
  65.     </ filter >   
  66.     <!-- 拦截所有的请求 -->   
  67.     < filter-mapping >   
  68.         < filter-name > springSecurityFilterChain </ filter-name >   
  69.         < url-pattern > /* </ url-pattern >   
  70.     </ filter-mapping >   
  71.     <!-- SpringSecurity必须的end -->   
  72.       
  73.   < welcome-file-list >   
  74.     < welcome-file > index.jsp </ welcome-file >   
  75.   </ welcome-file-list >   
  76. </ web-app >   

 

5.index.jsp

Html代码  收藏代码
  1. < %@ page  language = "java"   contentType = "text/html; charset=UTF-8"   
  2.     pageEncoding = "UTF-8" % >   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  4. < html >   
  5. < head >   
  6. < meta   http-equiv = "Content-Type"   content = "text/html; charset=UTF-8" >   
  7. < title > 首页 </ title >   
  8. </ head >   
  9. < body >   
  10.     < h1 > 这里是首页,欢迎你! </ h1 >   
  11.     < %   
  12.         String[] str  =  session .getValueNames();  
  13.         for(int i = 0 ;i < str.length ;i++){  
  14.             out.println("key =="+str[i]);  
  15.             out.println("value =="+session.getAttribute(str[i]));  
  16.         }  
  17.     %>   
  18. </ body >   
  19. </ html >   

 

6部署应用,在首次浏览index.jsp时,由于没登录,spring security会自动生成登录页面,页面内容如下:

 



 7输入用户名和密码,user,则进入首页

 

 

 至此,简单的权限控制完成,在index页面中通过session可以看到存入session中的用户信息。

分享到:
评论

相关推荐

    springsecurity入门代码资源

    hello大家好,这里是X,今天这篇博文带来的是SpringBoot安全管理:SpringSecurity,讲到安全管理,不得不说几乎所有的大型项目开发必备之一,而且有了它,对项目的安全也起到了非常大的效果,可以说是项目搭建的必备...

    Spring Security 3系列文章——入门篇(一)

    NULL 博文链接:https://clongjava.iteye.com/blog/1453648

    java获取jsp源码-spring-mvc-tutorial:SpringMVC5教程-springmvc框架指南

    每篇文章的源代码示例均使用最新的 Spring 5.1.0 RELEASE、JDK 8 和 Maven 3.2+ 开发。 Spring MVC 入门/基础 - 在本文中,我们将学习如何使用 Spring MVC 5 +、JSP、Maven 构建工具和 Eclipse IDE 创建一个简单的 ...

    128元尚硅谷Java视频教程_Spring Boot视频教程(下)整合篇

    着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),分布式...

    WebService CXF学习文档

    WebService CXF学习——入门篇 1.CXF由来 2.HelloWorld 3.WSDL描述 WebService CXF学习——进阶篇 1.SOAP讲解 2.JAX-WS讲解 3.对象传递 WebService CXF学习——高级篇(一)(二) 1.整合Spring框架 2.CXF...

    java8集合源码分析-java-demos:java-演示

    [全网最详细的一篇SpringCloud总结] () [feign] () [Spring Security 真正的前后分离实现] () [Spring Cloud Gateway] () [SpingCloud Gateway网关核心概念和原理] () [基于Redis实现Spring Cloud Gateway] () ...

    尚硅谷SpringBoot视频全套(核心技术篇+整合篇)

    着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),分布式...

    java面试题及技巧3

    │ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini │ │ │ 考题_1.doc │ ...

    asp.net知识库

    如何在.NET中实现脚本引擎 (CodeDom篇) .NET的插件机制的简单实现 我对J2EE和.NET的一点理解 难分难舍的DSO(一) InternalsVisibleToAttribute,友元程序集访问属性 Essential .NET 读书笔记 [第一部分] ...

    java面试题以及技巧

    │ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini │ │ │ 考题_1.doc │ ...

    java面试题目与技巧1

    │ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini │ │ │ 考题_1.doc │ ...

    java面试题及技巧4

    │ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini │ │ │ 考题_1.doc │ ...

    java面试题以及技巧6

    │ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini │ │ │ 考题_1.doc │ ...

Global site tag (gtag.js) - Google Analytics