配置文件详解
目录
配置文件详解
3.X
4.X
单点登录配置session-management
1.1 检测session超时
1.2 concurrency-control
1.3 session 固定攻击保护
3.X
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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">
<global-method-security pre-post-annotations="enabled">
</global-method-security>
<!-- 该路径下的资源不用过滤 -->
<http pattern="/include/js/**" security="none" />
<http pattern="/include/css/**" security="none" />
<http pattern="/include/scripts/**" security="none" />
<http pattern="/include/jsp/**" security="none" />
<http pattern="/images/**" security="none" />
<http pattern="/login.jsp" security="none" />
<!--auto-config = true 则使用from-login. 如果不使用该属性 则默认为http-basic(没有session).-->
<!-- lowercase-comparisons:表示URL比较前先转为小写。-->
<!-- path-type:表示使用Apache Ant的匹配模式。-->
<!--access-denied-page:访问拒绝时转向的页面。-->
<!-- access-decision-manager-ref:指定了自定义的访问策略管理器。-->
<http use-expressions="true" auto-config="true"
access-denied-page="/include/jsp/timeout.jsp">
<!--login-page:指定登录页面。 -->
<!-- login-processing-url:指定了客户在登录页面中按下 Sign In 按钮时要访问的 URL。-->
<!-- authentication-failure-url:指定了身份验证失败时跳转到的页面。-->
<!-- default-target-url:指定了成功进行身份验证和授权后默认呈现给用户的页面。-->
<!-- always-use-default-target:指定了是否在身份验证通过后总是跳转到default-target-url属性指定的URL。
-->
<form-login login-page="/login.jsp" default-target-url='/system/default.jsp'
always-use-default-target="true" authentication-failure-url="/login.jsp?login_error=1" />
<!--logout-url:指定了用于响应退出系统请求的URL。其默认值为:/j_spring_security_logout。-->
<!-- logout-success-url:退出系统后转向的URL。-->
<!-- invalidate-session:指定在退出系统时是否要销毁Session。-->
<logout invalidate-session="true" logout-success-url="/login.jsp"
logout-url="/j_spring_security_logout" />
<!-- 实现免登陆验证 -->
<remember-me />
<!-- max-sessions:允许用户帐号登录的次数。范例限制用户只能登录一次。-->
<!-- 此值表示:用户第二次登录时,前一次的登录信息都被清空。-->
<!-- exception-if-maximum-exceeded:默认为false,-->
<!-- 当exception-if-maximum-exceeded="true"时系统会拒绝第二次登录。-->
<session-management invalid-session-url="/login.jsp"
session-fixation-protection="none">
<concurrency-control max-sessions="1"
error-if-maximum-exceeded="false" />
</session-management>
<custom-filter ref="myFilter" before="FILTER_SECURITY_INTERCEPTOR" />
<session-management
session-authentication-strategy-ref="sas" />
</http>
<beans:bean id="sas"
class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
<beans:constructor-arg name="sessionRegistry"
ref="sessionRegistry" />
<beans:property name="maximumSessions" value="1" />
<!-- 防止session攻击 -->
<beans:property name="alwaysCreateSession" value="true" />
<beans:property name="migrateSessionAttributes" value="false" />
<!-- 同一个帐号 同时只能一个人登录 -->
<beans:property name="exceptionIfMaximumExceeded"
value="false" />
</beans:bean>
<beans:bean id="sessionRegistry"
class="org.springframework.security.core.session.SessionRegistryImpl" />
<!--
事件监听:实现了ApplicationListener监听接口,包括AuthenticationCredentialsNotFoundEvent 事件,-->
<!-- AuthorizationFailureEvent事件,AuthorizedEvent事件, PublicInvocationEvent事件-->
<beans:bean
class="org.springframework.security.authentication.event.LoggerListener" />
<!-- 自定义资源文件 提示信息 -->
<beans:bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basenames" value="classpath:message_zh_CN">
</beans:property>
</beans:bean>
<!-- 配置过滤器 -->
<beans:bean id="myFilter"
class="com.taskmanager.web.security.MySecurityFilter">
<!-- 用户拥有的权限 -->
<beans:property name="authenticationManager" ref="myAuthenticationManager" />
<!-- 用户是否拥有所请求资源的权限 -->
<beans:property name="accessDecisionManager" ref="myAccessDecisionManager" />
<!-- 资源与权限对应关系 -->
<beans:property name="securityMetadataSource" ref="mySecurityMetadataSource" />
</beans:bean>
<!-- 实现了UserDetailsService的Bean -->
<authentication-manager alias="myAuthenticationManager">
<authentication-provider user-service-ref="myUserDetailServiceImpl">
<!-- 登入 密码 采用MD5加密 -->
<password-encoder hash="md5" ref="passwordEncoder">
</password-encoder>
</authentication-provider>
</authentication-manager>
<!-- 验证用户请求资源 是否拥有权限 -->
<beans:bean id="myAccessDecisionManager"
class="com.taskmanager.web.security.MyAccessDecisionManager">
</beans:bean>
<!-- 系统运行时加载 系统要拦截的资源 与用户请求时要过滤的资源 -->
<beans:bean id="mySecurityMetadataSource"
class="com.taskmanager.web.security.MySecurityMetadataSource">
<beans:constructor-arg name="powerService" ref="powerService">
</beans:constructor-arg>
</beans:bean>
<!-- 获取用户登入角色信息 -->
<beans:bean id="myUserDetailServiceImpl"
class="com.taskmanager.web.security.MyUserDetailServiceImpl">
<beans:property name="userService" ref="userService"></beans:property>
</beans:bean>
<!-- 用户的密码加密或解密 -->
<beans:bean id="passwordEncoder"
class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" />
</beans:beans>
4.X
<beans:beans
xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<context:component-scan base-package="com.framework.security"/>
<!--<http pattern="/pm/**" security="none" />-->
<http pattern="/login.jsp" security="none" />
<http pattern="/common/**" security="none" />
<http pattern="/*.ico" security="none" />
<http use-expressions="false" >
<!-- IS_AUTHENTICATED_ANONYMOUSLY 匿名登录 -->
<intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<intercept-url pattern="/pm/**/*.jsp" access="ROLE_STATIC" />
<form-login login-page="/login" authentication-failure-url="/login?error=1" authentication-success-forward-url="/main.to" />
<logout invalidate-session="true" logout-url="/logout" logout-success-url="/" />
<http-basic/>
<headers >
<frame-options disabled="true"></frame-options>
</headers>
<csrf token-repository-ref="csrfTokenRepository" />
<session-management session-authentication-error-url="/frame.expired" >
<!-- max-sessions只容许一个账号登录,error-if-maximum-exceeded 后面账号登录后踢出前一个账号,expired-url session过期跳转界面 -->
<concurrency-control max-sessions="1" error-if-maximum-exceeded="false" expired-url="/frame.expired" session-registry-ref="sessionRegistry" />
</session-management>
<expression-handler ref="webexpressionHandler" ></expression-handler>
</http>
<beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />
<beans:bean id="userDetailsService" class="com.framework.security.UserDetailsServiceImpl" />
<!--配置web端使用权限控制-->
<beans:bean id="webexpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler" />
<authentication-manager >
<authentication-provider ref="authenticationProvider" />
</authentication-manager>
<!-- 自定义userDetailsService, 盐值加密 -->
<beans:bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="hideUserNotFoundExceptions" value="true" />
<beans:property name="userDetailsService" ref="userDetailsService" />
<beans:property name="passwordEncoder" ref="passwordEncoder" />
<beans:property name="saltSource" ref="saltSource" />
</beans:bean>
<!-- Md5加密 -->
<beans:bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" />
<!-- 盐值加密 salt对应数据库字段-->
<beans:bean id="saltSource" class="org.springframework.security.authentication.dao.ReflectionSaltSource">
<beans:property name="userPropertyToUse" value="salt"/>
</beans:bean>
<beans:bean id="csrfTokenRepository" class="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository" />
</beans:beans>
转:https://www.cnblogs.com/yyxxn/p/8080141.html
单点登录配置session-management
Spring Security通过http元素下的子元素session-management提供了对Http Session管理的支持。
1.1 检测session超时
Spring Security可以在用户使用已经超时的sessionId进行请求时将用户引导到指定的页面。这个可以通过如下配置来实现。
<security:http>
...
<!-- session管理,invalid-session-url指定使用已经超时的sessionId进行请求需要重定向的页面 -->
<security:session-management invalid-session-url="/session_timeout.jsp"/>
...
</security:http>
需要注意的是session超时的重定向页面应当是不需要认证的,否则再重定向到session超时页面时会直接转到用户登录页面。此外如果你使用这种方式来检测session超时,当你退出了登录,然后在没有关闭浏览器的情况下又重新进行了登录,Spring Security可能会错误的报告session已经超时。这是因为即使你已经退出登录了,但当你设置session无效时,对应保存session信息的cookie并没有被清除,等下次请求时还是会使用之前的sessionId进行请求。解决办法是显示的定义用户在退出登录时删除对应的保存session信息的cookie。
<security:http>
...
<!-- 退出登录时删除session对应的cookie -->
<security:logout delete-cookies="JSESSIONID"/>
...
</security:http>
此外,Spring Security并不保证这对所有的Servlet容器都有效,到底在你的容器上有没有效,需要你自己进行实验。
1.2 concurrency-control
通常情况下,在你的应用中你可能只希望同一用户在同时登录多次时只能有一个是成功登入你的系统的,通常对应的行为是后一次登录将使前一次登录失效,或者直接限制后一次登录。Spring Security的session-management为我们提供了这种限制。
首先需要我们在web.xml中定义如下监听器。
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
在session-management元素下有一个concurrency-control元素是用来限制同一用户在应用中同时允许存在的已经通过认证的session数量。这个值默认是1,可以通过concurrency-control元素的max-sessions属性来指定。
<security:http auto-config="true">
...
<security:session-management>
<security:concurrency-control max-sessions="1"/>
</security:session-management>
...
</security:http>
当同一用户同时存在的已经通过认证的session数量超过了max-sessions所指定的值时,Spring Security的默认策略是将先前的设为无效。如果要限制用户再次登录可以设置concurrency-control的error-if-maximum-exceeded的值为true。
<security:http auto-config="true">
...
<security:session-management>
<security:concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/>
</security:session-management>
...
</security:http>
设置error-if-maximum-exceeded为true后如果你之前已经登录了,然后想再次登录,那么系统将会拒绝你的登录,同时将重定向到由form-login指定的authentication-failure-url。如果你的再次登录是通过Remember-Me来完成的,那么将不会转到authentication-failure-url,而是返回未授权的错误码401给客户端,如果你还是想重定向一个指定的页面,那么你可以通过session-management的session-authentication-error-url属性来指定,同时需要指定该url为不受Spring Security管理,即通过http元素设置其secure=”none”。
<security:http security="none" pattern="/none/**" />
<security:http>
<security:form-login/>
<security:logout/>
<security:intercept-url pattern="/**" access="ROLE_USER"/>
<!-- session-authentication-error-url必须是不受Spring Security管理的 -->
<security:session-management session-authentication-error-url="/none/session_authentication_error.jsp">
<security:concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/>
</security:session-management>
<security:remember-me data-source-ref="dataSource"/>
</security:http>
在上述配置中我们配置了session-authentication-error-url为“/none/session_authentication_error.jsp”,同时我们通过<security:http security="none" pattern="/none/**" />指定了以“/none”开始的所有URL都不受Spring Security控制,这样当用户进行登录以后,再次通过Remember-Me进行自动登录时就会重定向到“/none/session_authentication_error.jsp”了。
在上述配置中为什么我们需要通过<security:http security="none" pattern="/none/**" />指定我们的session-authentication-error-url不受Spring Security控制呢?把它换成<security:intercept-url pattern="/none/**"access="IS_AUTHENTICATED_ANONYMOUSLY"/>不行吗?这就涉及到之前所介绍的它们两者之间的区别了。前者表示不使用任何Spring Security过滤器,自然也就不需要通过Spring Security的认证了,而后者是会被Spring Security的FilterChain进行过滤的,只是其对应的URL可以匿名访问,即不需要登录就可访问。使用后者时,REMEMBER_ME_FILTER检测到用户没有登录,同时其又提供了Remember-Me的相关信息,这将使得REMEMBER_ME_FILTER进行自动登录,那么在自动登录时由于我们限制了同一用户同一时间只能登录一次,后来者将被拒绝登录,这个时候将重定向到session-authentication-error-url,重定向访问session-authentication-error-url时,经过REMEMBER_ME_FILTER时又会自动登录,这样就形成了一个死循环。所以session-authentication-error-url应当使用<security:http security="none" pattern="/none/**" />设置为不受Spring Security控制,而不是使用<security:intercept-url pattern="/none/**"access="IS_AUTHENTICATED_ANONYMOUSLY"/>。
此外,可以通过expired-url属性指定当用户尝试使用一个由于其再次登录导致session超时的session时所要跳转的页面。同时需要注意设置该URL为不需要进行认证。
<security:http auto-config="true">
<security:form-login/>
<security:logout/>
<security:intercept-url pattern="/expired.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:intercept-url pattern="/**" access="ROLE_USER"/>
<security:session-management>
<security:concurrency-control max-sessions="1" expired-url="/expired.jsp" />
</security:session-management>
</security:http>
1.3 session 固定攻击保护
session固定是指服务器在给客户端创建session后,在该session过期之前,它们都将通过该session进行通信。session 固定攻击是指恶意攻击者先通过访问应用来创建一个session,然后再让其他用户使用相同的session进行登录(比如通过发送一个包含该sessionId参数的链接),待其他用户成功登录后,攻击者利用原来的sessionId访问系统将和原用户获得同样的权限。Spring Security默认是对session固定攻击采取了保护措施的,它会在用户登录的时候重新为其生成一个新的session。如果你的应用不需要这种保护或者该保护措施与你的某些需求相冲突,你可以通过session-management的session-fixation-protection属性来改变其保护策略。该属性的可选值有如下三个。
l migrateSession:这是默认值。其表示在用户登录后将新建一个session,同时将原session中的attribute都copy到新的session中。
l none:表示继续使用原来的session。
l newSession:表示重新创建一个新的session,但是不copy原session拥有的attribute。
转:https://www.cnblogs.com/fenglan/p/5913352.html
(注:本文是基于Spring Security3.1.6所写)