使会话春季安全性无效 [英] Invalidate session spring security

查看:49
本文介绍了使会话春季安全性无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序使用Spring Security在登录时对用户进行身份验证.我还具有并发控制,以避免用户在不同的计算机上登录两次.这工作正常,但我的问题是:如果用户在计算机上登录,则关闭浏览器.然后他重新打开Web应用程序,尝试再次登录,他得到以下消息超出此主体的最大会话数为1".我想使浏览器关闭时的会话无效.我该怎么办?

My web application uses spring security to authenticate user on login. I also have concurrency control to avoid user to login twice on different machine. This is working fine but my problem is that: If user is login on a machine, then close the browser. And he reopen the web app , try to login again he gets the following msg 'Maximum sessions of 1 for this principal exceeded' . I want to invalidate the session on browser closed. How can I do this?

Spring-security.xml

Spring-security.xml

       <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://.   www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/.    XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/security
                       http://www.springframework.org/schema/security/.  spring-security-3.1.xsd">

  <security:global-method-security
        secured-annotations="enabled" />

  <security:http auto-config="false"
        authentication-manager-ref="authenticationManager" use-expressions="true">
        <!-- Override default login and logout pages -->
        <security:form-login
              authentication-failure-handler-ref="fail"
              authentication-success-handler-ref="success" login-page="/car/login.xhtml"
              default-target-url="/jsf/car/home.xhtml" />
        <security:logout invalidate-session="true"
              logout-url="/j_spring_security_logout" success-handler-ref="customLogoutHandler" delete-cookies="JSESSIONID"/>
        <security:session-management>
              <security:concurrency-control
                    max-sessions="1" error-if-maximum-exceeded="true" />
        </security:session-management>
        <security:intercept-url pattern="/jsf/**"
              access="isAuthenticated()" />
        <security:intercept-url pattern="/run**"
              access="isAuthenticated()" />
        <security:intercept-url pattern="/pages/login.xhtml"
              access="permitAll" />
  </security:http>

  <bean id="success" class="com.car.LoginSuccess" />

  <bean id="fail" class="com.car.LoginFailed">
        <property name="defaultFailureUrl" value="/?login_error=true" />
  </bean>
  <bean id="passwordEncoder"
        class="org.springframework.security.authentication.encoding.ShaPasswordEncoder" />

  <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider
              user-service-ref="userDetailsService">
              <security:password-encoder ref="passwordEncoder"
                    hash="sha" />
        </security:authentication-provider>
  </security:authentication-manager>

    public class FilterToGetTimeOut extends OncePerRequestFilter {

@Override
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException {
    try {
        if(request.getRequestURI().equals("/") || request.getRequestURI().equals("/car/login.xhtml")){
            if(request.getSession().getAttribute("login") != null && (Boolean)request.getSession().getAttribute("login") == true){
                response.sendRedirect("/jsf/car/home.xhtml");     //After login page
            }
        } else if(request.getSession().getAttribute("login") == null && !request.getRequestURI().equals("/j_spring_security_logout")){
            response.sendRedirect(request.getContextPath()+"/?timeout=true");   //If timeout is true send session timeout error message to JSP
        }
        filterChain.doFilter(request, response);
    } catch (Exception e) {
        //Log Exception

    }
}

推荐答案

"/" (第一页)请求和 注销 请求.

Add below code for "/"(first page) request and logout request.

@Controller
public class LoginController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView loadApp(HttpServletRequest request) {
        HttpSession session= request.getSession(false);
        SecurityContextHolder.clearContext();
        if(session != null) {
            session.invalidate();
        }

        return new ModelAndView("/car/login");
    }
}

使用此过滤器如何获取会话超时使用Spring Security的消息

这篇关于使会话春季安全性无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆