使用 java config 在扩展WebSecurityConfigurerAdapter的类中添加以下代码 :
@Bean
public SessionRegistry sessionRegistry( ) {
SessionRegistry sessionRegistry = new SessionRegistryImpl( );
return sessionRegistry;
}
@Bean
public RegisterSessionAuthenticationStrategy registerSessionAuthStr( ) {
return new RegisterSessionAuthenticationStrategy( sessionRegistry( ) );
}
并在您的configure( HttpSecurity http )方法中添加以下内容:
http.sessionManagement( ).maximumSessions( -1 ).sessionRegistry( sessionRegistry( ) );
http.sessionManagement( ).sessionFixation( ).migrateSession( )
.sessionAuthenticationStrategy( registerSessionAuthStr( ) );
此外,在您的自定义身份验证 bean 中设置registerSessionAuthenticationStratergy,如下所示:
usernamePasswordAuthenticationFilter
.setSessionAuthenticationStrategy( registerSessionAuthStr( ) );
注意:在您的自定义身份验证 bean 中设置registerSessionAuthenticationStratergy会导致填充主体列表,因此当您尝试从 sessionRegistry ( sessionRegistry.getAllPrinicpals() ) 获取所有主体列表时,该列表不为空。