【问题标题】:I can't get a list of online users is spring security我无法获取在线用户列表是 spring security
【发布时间】:2017-12-19 00:11:18
【问题描述】:

我无法获取在线用户列表。

@Override
public void configure(HttpSecurity http) throws Exception {
    http
        .httpBasic()
            .realmName("GlxssSecurity")
            .and()
        .requestMatchers()
            .antMatchers("/oauth/authorize")
            .and()
        .authorizeRequests()
            .antMatchers("/oauth/authorize").authenticated()
            .and()
        .sessionManagement()
            .maximumSessions(1)
            .sessionRegistry(sessionRegistry());
}

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

@Bean
public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
    return new SecurityEvaluationContextExtension();
}

@Bean
public SessionRegistry sessionRegistry () {
    return new SessionRegistryImpl();
}

@Bean
public ServletListenerRegistrationBean<HttpSessionEventPublisher> httpSessionEventPublisher() {
    return new ServletListenerRegistrationBean<HttpSessionEventPublisher>(new HttpSessionEventPublisher());
}
@Autowired
private  SessionRegistry sessionRegistry;

public List getAdminUsers(){
    List<Object> list = sessionRegistry.getAllPrincipals();
    log.info(list.toString());
    return list;
}

【问题讨论】:

标签: spring spring-boot spring-security


【解决方案1】:
@Bean
public HandshakeInterceptor handsUserInterceptor() {

    return new HandshakeInterceptor() {
        @Override
        public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> map) throws Exception {
            if (request instanceof ServletServerHttpRequest) {
                ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
                Principal principal = request.getPrincipal();
                User user= userService.getUserWithAuthoritiesByLogin(principal.getName()).get();
                for (Authority authority : user.getAuthorities()) {
                    if ("ROLE_ADMIN".equals(authority.getName())){
                        SecurityUtils.getLoginAdminUsers().add(user);
                        break;
                    }
                }
            }
            return true;
        }

        @Override
        public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {

        }
    };
}

我是这样解决的,但他不太符合规范。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-18
    • 2016-11-03
    • 2015-05-30
    • 2017-04-04
    • 2014-09-18
    • 1970-01-01
    • 2014-11-09
    • 2021-02-18
    相关资源
    最近更新 更多