Spring Security-自定义UserDetailsService和UserDetails停止工作并发控制



伙计们,

我在spring-security.xml中有一个块,它控制到我的系统的并发会话:

<security:session-management>
    <security:concurrency-control error-if-maximum-exceeded="true" max-sessions="1"/>
</security:session-management>

这没关系,一直有效到现在。

我在UserDetailsService和UserDetails上进行了自定义实现,以在主体中存储额外的列。

一切正常,但在此之后,并发控制停止工作。用户仍然在同一时间使用相同的用户名登录多次。

我认为在我的自定义服务中,我需要实现一些其他策略来处理并发,但我不知道如何实现。

我的自定义UserDetailsService是:

public class AuthenticationService implements UserDetailsService {
    @Autowired
    private UserService userService;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
        User user = userService.getByUsername(username);
        CustomUserDetail customUserDetail = new CustomUserDetail(user);
        return customUserDetail ;
    }
}

我的自定义UserDetails是一个简单的bean:

public class CustomUserDetail implements UserDetails {
    private String username;
    private String password;
    private boolean accountnonexpired;
    private boolean accountnonlocked;
    private boolean credentialsnonexpired;
    private boolean enabled;
    private String user_id;
    private String org_id;
    private String email;
    private final Collection<? extends GrantedAuthority> authorities;
public CustomUserDetail(User user) {
    this.username = user.getUsername();
    this.password = user.getPassword();
    this.authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
    this.user_id = user.getId();
    this.org_id = user.getOrganization().getId();
    this.email = user.getEmail();
    if(user.isActive()){
        this.accountnonexpired = true;
        this.accountnonlocked = true;
        this.credentialsnonexpired = true;
        this.enabled = true;
    }
}

有人知道我还需要做什么吗?

谢谢你的帮助。

如果您没有更改任何其他内容,我怀疑是您没有在CustomUserDetail对象中定义equals方法。实际上,它只是继承了Object中比较对象实例的默认值。每当同一用户重新登录时,就会创建一个新的CustomUserDetail实例,该实例将被框架视为不同的实例
原始User对象按用户名检查相等性。这就是新实现的方式。

最新更新