UserDetails 的自定义实现示例

发布于 2024-12-09 04:01:54 字数 83 浏览 0 评论 0原文

我正在寻找在 Spring Security 3 中创建自定义 UserDetails 对象的示例。 我希望有人能帮忙,谢谢。

I am looking for an example of making a custom UserDetails object in Spring Security 3.
And I was hoping if anyone can help, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是偏爱你 2024-12-16 04:01:54

这是我用过的:

public class CustomUserDetails implements UserDetails {
    private  User user;

    public CustomUserDetails(final User _user) {
            this.user = _user;
    }

    public CustomUserDetails() {
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
             final Set<GrantedAuthority> _grntdAuths = new HashSet<GrantedAuthority>();

     List<UserRole> _roles = null;

     if (user!=null) {
             _roles = user.getRoles();
     }

     if (_roles!=null) {
             for (UserRole _role : _roles) {
                     _grntdAuths.add(new GrantedAuthorityImpl(_role.getRole()));
             }
     }

     return _grntdAuths;
    }

    @Override
    public String getPassword() {
            return user.getPassword();
    }
 @Override
    public String getUsername() {
            if (this.user == null) {
                    return null;
            }
            return this.user.getUser_name();
    }

    @Override
    public boolean isAccountNonExpired() {
            return this.user.isAccountNonExpired();
    }

    @Override
    public boolean isAccountNonLocked() {
            return this.user.isAccountNonLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
            return this.user.isCredentialsNonExpired();
    }

    @Override
    public boolean isEnabled() {
            return this.user.isEnabled();
    }

    public User getUser() {
            return user;
    }

    @Override
    public String toString() {
            return "CustomUserDetails [user=" + user + "]";
    }
}

Here's what I've used:

public class CustomUserDetails implements UserDetails {
    private  User user;

    public CustomUserDetails(final User _user) {
            this.user = _user;
    }

    public CustomUserDetails() {
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
             final Set<GrantedAuthority> _grntdAuths = new HashSet<GrantedAuthority>();

     List<UserRole> _roles = null;

     if (user!=null) {
             _roles = user.getRoles();
     }

     if (_roles!=null) {
             for (UserRole _role : _roles) {
                     _grntdAuths.add(new GrantedAuthorityImpl(_role.getRole()));
             }
     }

     return _grntdAuths;
    }

    @Override
    public String getPassword() {
            return user.getPassword();
    }
 @Override
    public String getUsername() {
            if (this.user == null) {
                    return null;
            }
            return this.user.getUser_name();
    }

    @Override
    public boolean isAccountNonExpired() {
            return this.user.isAccountNonExpired();
    }

    @Override
    public boolean isAccountNonLocked() {
            return this.user.isAccountNonLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
            return this.user.isCredentialsNonExpired();
    }

    @Override
    public boolean isEnabled() {
            return this.user.isEnabled();
    }

    public User getUser() {
            return user;
    }

    @Override
    public String toString() {
            return "CustomUserDetails [user=" + user + "]";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文