spring security session-management

1、在配置文件中增加 一个listen

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 2、FilterChainProxy 增加 ConcurrentSessionFilter ,这个Filter 要两个属性 sessionRegistry(需要SessionRegistryImpl实例expiredUrl过滤失败时跳转的url

<http>
<custom-filter position="FORM_LOGIN_FILTER" ref="loginFilter" />
<custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrentSessionFilter" />
<!-- 防止同一用户多次登录,使第二次登录失 -->
<session-management session-authentication-strategy-ref="concurrentSessionControlStrategy" />
</http>
<!-- Login Filter -->
<beans:bean id="loginFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
	 <beans:property name="sessionAuthenticationStrategy" ref="concurrentSessionControlStrategy" />
	<beans:property name="authenticationManager" ref="authenticationManager" />
	<beans:property name="authenticationSuccessHandler" ref="loginAuthenticationSuccessHandler" />
	<beans:property name="authenticationFailureHandler" ref="loginAuthenticationFailureHandler" />
</beans:bean>
<beans:bean id="concurrentSessionFilter" class="org.springframework.security.web.session.ConcurrentSessionFilter">
		<beans:property name="sessionRegistry" ref="sessionRegistry" />
		<beans:property name="expiredUrl" value="/admin/login" />
		<beans:property name="logoutHandlers">
			<beans:list>
				<beans:ref bean="logoutHandler" />
			</beans:list>
		</beans:property>
	</beans:bean>
<beans:bean id="logoutHandler" class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
<!-- the flowing settings for session management -->
<beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />
<beans:bean id="concurrentSessionControlStrategy" class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
		<beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" />
		<beans:property name="maximumSessions" value="1" />
		<beans:property name="migrateSessionAttributes" value="true" />
		<beans:property name="exceptionIfMaximumExceeded" value="false" />
</beans:bean>

 3、代码  AbstractAuthenticationProcessingFilter  在方法dofilter()根据属性 concurrentSessionControlStrategy的方法onAuthentication

在ConcurrentSessionFilter Filter 中做具体是通过还是不过的验证

  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        HttpSession session = request.getSession(false);

        if (session != null) {
            SessionInformation info = sessionRegistry.getSessionInformation(session.getId());

            if (info != null) {
                if (info.isExpired()) {
                    // Expired - abort processing
                    doLogout(request, response);

                    String targetUrl = determineExpiredUrl(request, info);

                    if (targetUrl != null) {
                        redirectStrategy.sendRedirect(request, response, targetUrl);

                        return;
                    } else {
                        response.getWriter().print("This session has been expired (possibly due to multiple concurrent " +
                                "logins being attempted as the same user).");
                        response.flushBuffer();
                    }

                    return;
                } else {
                    // Non-expired - update last request date/time
                    sessionRegistry.refreshLastRequest(info.getSessionId());
                }
            }
        }

        chain.doFilter(request, response);
    }

 

李永博客的主页

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Session timeout in Spring Security refers to the amount of time a user's session can remain active without any activity. When a user logs in to a web application, a session is created for that user. The session remains active until the user logs out, or until the session timeout period expires. By default, Spring Security sets the session timeout to 30 minutes. However, this can be configured in the application's configuration file. To change the session timeout value, you need to set the `server.servlet.session.timeout` property in your `application.properties` file. For example, to set the session timeout to 60 minutes, you can add the following line to your `application.properties` file: ``` server.servlet.session.timeout=60m ``` In addition to setting the session timeout, you can also configure Spring Security to handle session expiration. For example, you can redirect the user to a login page or display a custom message when the session expires. To do this, you need to configure the `session-management` element in your Spring Security configuration file. ``` <http> ... <session-management> <concurrency-control max-sessions="1" expired-url="/login?expired=true" /> </session-management> </http> ``` In the above example, the `max-sessions` attribute limits the user to only one session at a time. If the user tries to open a new session, the previous session will be invalidated. The `expired-url` attribute specifies the URL to redirect the user to when the session expires. In this case, the user will be redirected to the login page with a query parameter indicating that the session has expired.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值