doc-链接:http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity.html
http://projects.spring.io/spring-security/#quick-start
官方文档-中文-链接:http://www.mossle.com/docs/springsecurity3/html/springsecurity.html
简介-链接:
http://sgyyz.blog.51cto.com/5069360/1358359
http://www.cnblogs.com/Beyond-bit/p/SpringMVC_And_SpringSecurity.html
http://www.blogjava.net/fancydeepin/archive/2012/08/31/spring_security.html
可行简单demo-链接:http://blog.csdn.net/wtderek/article/details/17267387
4个数据库表-链接:http://skysoft.blog.51cto.com/6091763/1031523
详细-链接:http://www.blogjava.net/SpartaYew/archive/2011/05/19/spingsecurity3.html
深入-链接:http://kingxss.iteye.com/blog/1908011
附源码研究-链接:http://www.yihaomen.com/article/java/447.htm
书-链接:http://sishuok.com/forum/blogPost/list/3870.html
入门教程old-链接:http://www.blogjava.net/fastzch/archive/2013/05/02/315028.html
自定义过滤器链-链接:http://blog.csdn.net/shadowsick/article/details/8576449
security几篇博文-链接:http://blog.csdn.net/shadowsick/article/category/1342575
---步骤:
1.配置web.xml的filter
2.配置security的配置文件
1、 配置<http>
2、 配置认证管理器<authentication-provider>
3.security过滤器
---spring security配置:
1. <!-- spring securit 3.X新的资源放行配置方式,不受保护的资源 假设 A 文件夹下有个 B 文件夹,则: /A/* 表示:A 文件夹下的所有文件,但不包括 B 文件夹,B 文件夹里的文件与其无关; /A/** 表示:A 文件夹下的所有文件,包括 B 文件夹和 B 文件夹下的所有文件; 它们的区别是,“/*” 不能够跨文件夹,而 “/**” 能够跨文件夹。 --> <http pattern="/**/*.jpg" security="none"/> 2. <!-- 开启默认的拦截器 --> <http auto-config='true' access-denied-page="/no.jsp"> <!--访问被拒绝后的页面,没有配置的话会直接返回错误码403--> <!-- 下面是对Action配置。注意在前面加/,否则不会被SS3进行拦截验证。表示具有访问/unitsManager资源的用户必须具有ROLE_PLATFORMADMIN的权限。 当用户登录时,SS3将用户的所有权限从数据库中提取出来,形成列表。 当用户访问该资源时,SS3将 登录用户的权限列表提出来跟下面配置的权限进行比对,若有,则允许访问,若没有,则给出AccessDeniedException。--> <intercept-url pattern="/unitsManager" access="ROLE_PLATFORMADMIN" /> <intercept-url pattern="/secure/extreme/**" access="hasRole('supervisor')"/> <intercept-url pattern="/secure/**" access="isAuthenticated()" /> <!-- 允许访问的uri :Allow all other requests--> <intercept-url pattern="/**" access="ROLE_USER" /> <!-- 登陆页面配置(如果没有配置会使用默认自带的登录页) --> <form-login login-page="/login.jsp" default-target-url="/index.jsp" authentication-failure-url="/login.jsp?error=true"/> <!--退出 --> <logout invalidate-session="true" logout-url="/logout.jsp"/> <!-- "记住我"功能,采用持久化策略(将用户的登录信息存放在数据库表中) --> <remember-me /> <remember-me data-source-ref="dataSource" /> <!-- Uncomment to limit the number of sessions a user can have --> <session-management invalid-session-url="/timeout.jsp"> <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" /> </session-management> <!-- 检测失效的sessionId,超时时定位到另外一个URL --> <session-management invalid-session-url="/sessionTimeout.jsp" /> </http> 3. <!-- 权限管理者 --> <authentication-manager> <authentication-provider> <!-- 可提供登陆访问的用户 --> <user-service> <user name="haha" password="haha" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="xixi" password="xixi" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> 3.1 帐号密码加密:MD5加密 <beans:bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder"/> <authentication-manager> <authentication-provider> <password-encoder ref="encoder"/> <user-service> <user name="rod" password="4efe081594ce25ee4efd9f7067f7f678a347bccf2de201f3adf2a3eb544850b465b4e51cdc3fcdde" authorities="supervisor, user, teller" /> <user name="dianne" password="957ea522524a41cbfb649a3e293d56268f840fd5b661b499b07858bc020d6d223f912e3ab303b00f" authorities="user,teller" /> <user name="scott" password="fb1f9e48058d30dc21c35ab4cf895e2a80f2f03fac549b51be637196dfb6b2b7276a89c65e38b7a1" authorities="user" /> <user name="peter" password="e175750688deee19d7179d444bfaf92129f4eea8b4503d83eb8f92a7dd9cda5fbae73638c913e420" authorities="user" /> </user-service> </authentication-provider> </authentication-manager> 3.2帐号密码保存数据库 http://www.yihaomen.com/article/java/440.htm <authentication-manager> <authentication-provider> <jdbc-user-service data-source-ref="dataSource" users-by-username-query=" select username,password, enabled from users where USERNAME=?" authorities-by-username-query=" select u.username, ur.authority from users u, user_roles ur where u.user_id = ur.user_id and u.username =? " /> </authentication-provider> </authentication-manager>
=================from:吴老师教学讲义-SpringSecurity_3.0_教程
1.<!--密码加密-->
ok
2.<!--将帐号信息存入数据库-->
ok
3.获取登录用户信息
ok
第一种方式:使用java代码:SecurityContextHolder
SecurityContextHolder.getContext().getAuthentication()
第二种方式:使用Spring Security 框架的标签库.
4.有选择的显示具备权限的链接
amdin登录的时候,链接显示,而uer登录的时候,不显示链接 .这个可以使用 标签库可以做到
<body>
这是首页,欢迎<sec:authentication property="name" /> ! <br />
<sec:authorize ifAllGranted="ROLE_ADMIN" >
<a href="admin.jsp">进入admin.jsp页面</a>
</sec:authorize>
</body>
sec:authorize 标签的属性:
A) ifAllGranted:只有当前用户拥有所有指定的权限时,才能显示标签体的内容 (相当于“与” 的关系)
B) ifAnyGranted:当前用户拥有指定的权限中的一个的时候,就能显示标签内部内容(相当于“或”的关系)
C) ifNotGranted:没有指定的权限的时候,显示标签体的内容 (相当于“非”的关系)
或者:
当前用户如果能访问/admin.jsp,则显示标签体的内容
<sec:authorize url="/admin.jsp" >
<a href="admin.jsp">进入admin.jsp页面</a>
</sec:authorize>
5.同步会话控制
多个用户不能使用同一个账号同时登陆系统。为了实现这个功能,我们首先要在web.xml文件中添加一个监听器,这个监听器会在session 创建和销毁的时候通知Spring Security。
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
然后,在applicationContext中添加下面的配置, 这将防止一个用户重复登录好几次-第二次登录会让第一次登录失效。
6.保护业务代码的执行
Spring Security 使用AOP对方法调用进行了权限控制,这部分内容来源于Spring提供的AOP功能,Spring Security进行了自己的封装,我们可以使用声明和编程两种方式进行权限管理
在国内大多数项目中,均设置了比较复杂的权限控制,一般就会涉及到用户、角色、权限、资源4张表,若要加上4张表之间的对应关系表3张,得有7张表才行。
---最小配置:
1.web.xml
<!-- spring securit start --> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring securit start -->
2.security-config.xml
<?xml version="1.0" encoding="UTF-8"?> <b:beans xmlns="http://www.springframework.org/schema/security" xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> <!-- spring securit 3.X新的资源放行配置方式,不受保护的资源 “/*” 不能够跨文件夹,而 “/**” 能够跨文件夹。 --> <http pattern="/**/*.jpg" security="none"/> <http pattern="/**/*.png" security="none"/> <http pattern="/**/*.gif" security="none"/> <http pattern="/**/*.ico" security="none"/> <http pattern="/**/*.css" security="none"/> <http pattern="/**/*.js" security="none"/> <http pattern="/login.jsp" security="none"/> <!-- 开启默认的拦截器 --> <http auto-config='true' access-denied-page="/no.jsp"> <!--访问被拒绝后的页面,没有配置的话会直接返回错误码403--> <!-- 允许访问的uri :Allow all other requests--> <intercept-url pattern="/**" access="ROLE_USER" /> <!-- 登陆页面配置(如果没有配置会使用默认自带的登录页) --> <form-login login-page="/login.jsp" default-target-url="/index.jsp" authentication-failure-url="/login.jsp?error=true"/> <!--退出 --> <logout invalidate-session="true" logout-url="/logout.jsp"/> <!-- remember-me--> <remember-me /> <!-- Uncomment to limit the number of sessions a user can have --> <session-management invalid-session-url="/timeout.jsp"> <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" /> </session-management> </http> <!-- 权限管理者 --> <authentication-manager> <!-- 权限提供者 --> <authentication-provider> <!-- 可提供登陆访问的用户 --> <user-service> <user name="haha" password="haha" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="xixi" password="xixi" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> </b:beans>
3.login.jsp
<h1>user login</h1> <form action="j_spring_security_check" method="post"> 用户名<input type="text" name="j_username"/><br/> 密码<input type="password" name="j_password"/><br/> <input type="submit" value="submit"/> </form> 注销请求,默认为/j_spring_security_logout
(待使用,待深入研究)