java - 使 session spring 安全无效

标签 java jsf-2 spring-security

我的网络应用程序使用 spring security 在登录时对用户进行身份验证。我也有并发控制,以避免用户在不同的机器上登录两次。这工作正常,但我的问题是: 如果用户在机器上登录,则关闭浏览器。然后他重新打开网络应用程序,尝试再次登录,他收到以下消息“超出此主体的最大 session 数 1”。我想在浏览器关闭时使 session 无效。我该怎么做?

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

    }
}

最佳答案

"/"(第一页)请求和logout请求添加以下代码。

@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");
    }
}

使用这个过滤器 How to get session time out message using Spring security

关于java - 使 session spring 安全无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37803440/

相关文章:

jquery - 无法接受来自托管 bean jsf2 primefaces 的 jquery 选择器

java - 未找到 UsernamePasswordAuthenticationToken 的 AuthenticationProvider

java - Spring安全permitall()适用于@RestController但不适用于@Controller

java - Spring 启动安全

java - Mule ESB 在服务器上部署时不接受 SSL 证书,但在本地开发机器上运行良好

java - 将 JSF .xhtml 文件映射到无扩展名

java - 在什么情况下一个类的方法可以在另一个类的对象上调用?

html - <p :media> component 隐藏的 PrimeFaces 菜单栏选项

java - Optional.orElse(null) 用 org.jetbrains.annotations.NotNull 标记

Java 在另一个方法中更改 JLabel 的文本