GET UserDetails权限
GET UserDetails Authorities
我创建了一个@ManyToMany
table。一个 table 中的用户和另一个中的角色。即一个用户可以有多个角色,一个角色可以有多个用户。我认为没有什么异常或错误。
我就是这样得到的 roles
:
List<AuthoritiesEntity> roleList = userEntity.getAuthoritiesEntities();
我还有UserDetails
所以我需要以某种方式将这些角色推入 UserDetails
,但我做不到。
请告诉我该怎么做?
MyUserDetail.java
public class MyUserDetail implements UserDetailsService {
@Autowired
ServiceJpa serviceJpa;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
UserEntity userEntity = serviceJpa.findUserByEmail(email);
List<AuthoritiesEntity> roleList = userEntity.getAuthoritiesEntities();
return new org.springframework.security.core.userdetails.User("va@va.va", "1111",
true, true, true, true, roleList);
}
}
您可以调用 mapRolesToAuthorites
方法并传递您的 roleList
并定义这样的函数。
return new org.springframework.security.core.userdetails.User("va@va.va", "1111",
true, true, true, true, mapRolesToAuthorites(roleList));
private Collection<? extends GrantedAuthority> mapRolesToAuthorites(Collection<Role> roles) {
return roles.stream().map(role -> new SimpleGrantedAuthority(role.getName())).collect(Collectors.toList());
}
private Collection<? extends GrantedAuthority> getAuthorities(Collection<AuthoritiesEntity> roles) {
List<GrantedAuthority> authorities = new ArrayList<>();
for (AuthoritiesEntity authoritiesEntity: roles) {
authorities.add(new SimpleGrantedAuthority(authoritiesEntity.getRoleEnum().toString()));
}
return authorities;
}
您需要修改您的getAuthoritiesEntities
private List<GrantedAuthority> getAuthoritiesEntities(Set<Role> userRoles) {
Set<GrantedAuthority> roles = new HashSet<>();
userRoles.forEach((role) -> {
roles.add(new SimpleGrantedAuthority(role.getRole()));
});
List<GrantedAuthority> grantedAuthorities = new ArrayList<>(roles);
return grantedAuthorities;
}
现在get roleList
List<AuthoritiesEntity>roleList=userEntity.getAuthoritiesEntities(userEntity.getRoles());
现在return认证
return new org.springframework.security.core.userdetails.User(userEntity.getUsername(), userEntity.getPassword(), roleList);
我创建了一个@ManyToMany
table。一个 table 中的用户和另一个中的角色。即一个用户可以有多个角色,一个角色可以有多个用户。我认为没有什么异常或错误。
我就是这样得到的 roles
:
List<AuthoritiesEntity> roleList = userEntity.getAuthoritiesEntities();
我还有UserDetails
所以我需要以某种方式将这些角色推入 UserDetails
,但我做不到。
请告诉我该怎么做?
MyUserDetail.java
public class MyUserDetail implements UserDetailsService {
@Autowired
ServiceJpa serviceJpa;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
UserEntity userEntity = serviceJpa.findUserByEmail(email);
List<AuthoritiesEntity> roleList = userEntity.getAuthoritiesEntities();
return new org.springframework.security.core.userdetails.User("va@va.va", "1111",
true, true, true, true, roleList);
}
}
您可以调用 mapRolesToAuthorites
方法并传递您的 roleList
并定义这样的函数。
return new org.springframework.security.core.userdetails.User("va@va.va", "1111",
true, true, true, true, mapRolesToAuthorites(roleList));
private Collection<? extends GrantedAuthority> mapRolesToAuthorites(Collection<Role> roles) {
return roles.stream().map(role -> new SimpleGrantedAuthority(role.getName())).collect(Collectors.toList());
}
private Collection<? extends GrantedAuthority> getAuthorities(Collection<AuthoritiesEntity> roles) {
List<GrantedAuthority> authorities = new ArrayList<>();
for (AuthoritiesEntity authoritiesEntity: roles) {
authorities.add(new SimpleGrantedAuthority(authoritiesEntity.getRoleEnum().toString()));
}
return authorities;
}
您需要修改您的getAuthoritiesEntities
private List<GrantedAuthority> getAuthoritiesEntities(Set<Role> userRoles) {
Set<GrantedAuthority> roles = new HashSet<>();
userRoles.forEach((role) -> {
roles.add(new SimpleGrantedAuthority(role.getRole()));
});
List<GrantedAuthority> grantedAuthorities = new ArrayList<>(roles);
return grantedAuthorities;
}
现在get roleList
List<AuthoritiesEntity>roleList=userEntity.getAuthoritiesEntities(userEntity.getRoles());
现在return认证
return new org.springframework.security.core.userdetails.User(userEntity.getUsername(), userEntity.getPassword(), roleList);