【发布时间】:2020-10-24 04:48:11
【问题描述】:
我得到了所有活跃用户,但是当用户退出时,它仍然被列为活跃用户。如何防止用户在退出后被列为活动用户?
我无法在文档中找到解决方案。
https://github.com/romanych2021/TestSession
谢谢。
ActiveUserServiceImpl.java
@Service
public class ActiveUserServiceImpl implements ActiveUserService{
@Autowired
SessionRegistry sessionRegistry;
public List<String > getAllActiveUser(){
List<Object> principals = sessionRegistry.getAllPrincipals();
User[] users = (User[]) principals.toArray(new User[0]);
return Arrays.stream(users)
.filter(user -> !sessionRegistry.getAllSessions(user, false)
.isEmpty()).map(User::getUsername).collect(Collectors.toList());
}
}
SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.mvcMatchers("/").permitAll()
.mvcMatchers("/login").anonymous()
.mvcMatchers("/user", "/allUser").hasAnyRole("ADMIN", "USER")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/")
.and().csrf().disable()
.logout()
.permitAll()
.logoutUrl("/logout")
.logoutSuccessUrl("/")
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.and().sessionManagement()
.maximumSessions(1)
.expiredUrl("/login")
.sessionRegistry(sessionRegistry);
}
}
HTML
<form method="post" action="/logout">
<button type="submit">Exit</button>
</form>
【问题讨论】:
标签: spring spring-mvc spring-security