2017-09-07 69 views
1

我在我的系统中两个角色:用户管理如何通过登录注销用户(不是当前)?

管理员可以通过名字登出任何用户

我该怎么做? 我需要的是这样的:

service.logoutUser(anotherUserName)

的配置是这样的:

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers 
       .... 
       .formLogin() 
       .loginProcessingUrl("/api/login") 
       .successHandler(authenticationSuccessHandler) 
       .failureHandler(new SimpleUrlAuthenticationFailureHandler()) 
       .and() 
      .rememberMe() 
       .key("...") 
       .rememberMeCookieName("...") 
       .userDetailsService(userDetailsService) 
       .and() 
      .csrf() 
       .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) 
       .and() 
      .logout() 
       .deleteCookies("JSESSIONID") 
       .logoutUrl("/logout") 
       .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()) 
       .permitAll(); 

回答

2

事情是这样的:

import org.springframework.security.core.userdetails.User; 
... 
... 
Set<SimpleGrantedAuthority> userRoles = new HashSet<>(); 
userRoles.add(new SimpleGrantedAuthority("ROLE_USER")); 
User user = new User(anotherUserName, "", userRoles); 
List<SessionInformation> sessions = sessionRegistry.getAllSessions(u, false); 

for(SessionInformation info : infos) { 
    info.expireNow(); 
} 
+0

你能澄清为什么要使用空字符串代替密码? – gstackoverflow

+0

我想你可能会知道https://stackoverflow.com/questions/46101537/use-sessionregistry-without-session-count-limit-for-user – gstackoverflow

+0

你不需要密码,你正在尝试做什么。此外,您的代码不需要知道用户密码。密码只能由用户编写并加密。 – mario

相关问题