【问题标题】:Spring Boot Web MVC Allow one user at a time from anywhereSpring Boot Web MVC 一次允许一个用户从任何地方
【发布时间】:2018-01-08 07:15:50
【问题描述】:

Spring Boot Web MVC 允许一个用户在任何地方一次登录,如果他/她想登录,就会强制登录。

我在网上搜索了很多,我发现我可以做类似的事情:

http.sessionManagement()
  .invalidSessionUrl("/invalidSession")
  .maximumSessions(1)
  .maxSessionsPreventsLogin(true)
  .sessionRegistry(sessionRegistry())

但这不起作用,我可以从其他浏览器登录而不会出现任何错误。

我从上周开始尝试解决这个问题,但没有找到任何可行的解决方案。

更新

http.antMatchers("/", "/register/**", "/email/**","/captcha.png/**")
  .permitAll()
  .antMatchers("/login/**")
  .permitAll()// Basically I'm allowing parameters for login so
  // .antMatchers("/services/ownerTaxInformation/**")
  .permitAll()
  .antMatchers("/forgot/password/**", "/user/verify/**")
  .permitAll()
  .antMatchers("/user/resetPassword*")
  .hasAuthority("CHANGE_PASSWORD_PRIVILEGE")
  .anyRequest()
  .authenticated()
  .and()
  .addFilterBefore(jCaptchaAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class)
   .formLogin()
   .loginPage("/login")
   .permitAll().and()
   .csrf()
   .disable()
   .sessionManagement()
   .invalidSessionUrl("/invalidSession")
   .maximumSessions(1)
   .maxSessionsPreventsLogin(true)
   .sessionRegistry(sessionRegistry()).and()
   .sessionFixation()
   .none()
   .and()
   .logout()
   .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
   .logoutSuccessUrl("/")   
   .invalidateHttpSession(false)
   .deleteCookies("JSESSIONID")
   .permitAll();

【问题讨论】:

  • 添加 .sessionRegistry(sessionRegistry) 而不是 .sessionRegistry(sessionRegistry())

标签: spring spring-mvc spring-boot spring-security


【解决方案1】:

你可以创建一个过滤器(它会过滤所有你想要一个用户的网址)然后检查 sessionRegistry 中是否没有用户 然后用户可以访问 url 否则使用户访问无效。 要访问所有登录用户的列表,您需要将 SessionRegistry 实例注入到您的 bean。

@Autowired
@Qualifier("sessionRegistry")
private SessionRegistry sessionRegistry;

here 是有关 sessionRegistry 检索登录用户列表的有用详细信息

【讨论】:

    【解决方案2】:

    是的,我遇到过类似的问题。我已通过以下 sn-p 修复。

    内部配置

    @Override
    public void configure(HttpSecurity http) throws Exception 
    {
        http.
    
                    authorizeRequests()
    
                      .and()
                      .sessionManagement()
                        .maximumSessions(1) // How many session the same user can have? This can be any number you pick
                        .expiredUrl("/login?expired")
                        .sessionRegistry(sessionRegistry);
    
    }
    
    
    
    @Bean(name = "sessionRegistry")
    public SessionRegistry sessionRegistry() {
      return new SessionRegistryImpl();
    }
    
    @Autowired
    @Lazy
    private SessionRegistry sessionRegistry;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-26
      • 2014-02-11
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2017-08-28
      • 2012-09-22
      相关资源
      最近更新 更多