Spring Boot Web MVC从任何地方一次允许一个用户,如果他/她想登录,则会强制登录。
我在互联网上搜索了很多,我发现我可以做类似的事情:
http..sessionManagement()invalidSessionUrl(" / invalidSession&#34)。 .maximumSessions(1) .maxSessionsPreventsLogin(真) .sessionRegistry(的SessionRegistry())
但是这不起作用,我可以从不同的浏览器登录而没有任何错误。
我试图从上周解决这个问题,但没有找到任何可行的解决方案。
更新
http.antMatchers(" /"," / register / "," / email / ", " /captcha.png/的")permitAll()antMatchers(" /登录/ &#34) .permitAll()//基本上我允许登录参数 // .antMatchers(" / services / ownerTaxInformation / ")。permitAll() .antMatchers(" /忘记/密码/ "," / user / verify / **")。permitAll()。antMatchers(" / user / * resetPassword&#34) .hasAuthority(" CHANGE_PASSWORD_PRIVILEGE&#34)。anyRequest()认证()和()。 .addFilterBefore(jCaptchaAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class).formLogin() .loginPage(" /登录&#34)。permitAll()和() .csrf()。禁用() 。.sessionManagement()invalidSessionUrl(" / invalidSession&#34) .maximumSessions(1) .maxSessionsPreventsLogin(真) .sessionRegistry(的SessionRegistry())。和() .sessionFixation()。无()和() 。登出() .logoutRequestMatcher(新的AntPathRequestMatcher(" / logout"))。logoutSuccessUrl(" /") .invalidateHttpSession(假).deleteCookies(" JSESSIONID&#34)。permitAll();
请朋友麻我出去。
答案 0 :(得分:0)
你可以创建一个过滤器(它会过滤你想要一个用户的所有网址),然后检查sessionRegistry中是否没有用户 然后用户可以访问url else使用户访问无效。 要访问所有登录用户的列表,您需要将SessionRegistry实例注入您的bean。
@Autowired
@Qualifier("sessionRegistry")
private SessionRegistry sessionRegistry;
here是有关sessionRegistry检索登录用户列表的有用详细信息
答案 1 :(得分:-1)
是的,我遇到过类似的问题。我已经修改了以下代码段。
内部配置
@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;