gpt4 book ai didi

spring - 自定义实现UserDetails的示例

转载 作者:行者123 更新时间:2023-12-02 07:09:09 25 4
gpt4 key购买 nike

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

最佳答案

这是我使用的:

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 + "]";
}
}

关于spring - 自定义实现UserDetails的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7737602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com