Java Spring Security中需要授权的文本表示
我在Spring Security中的注册权限有问题 我不能做注册的方法 我试图设置每个路径的访问权限,但没有帮助 控制器 证券配置 用户服务 用户模型 错误 java.lang.IllegalArgumentException:授予的文本权限 代表须于 org.springframework.util.Assert.hasTextAssert.java:284 ~[spring-core-5.2.4.释放。震击器:5.2.4.释放]位于 org.springframework.security.core.authority.SimpleGrantedAuthority.SimpleGrantedAuthority.java:38 ~[spring-security-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]at com.xxx.xx.models.User.getAuthoritiesUser.java:71~[classes/:na] java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0Native 方法~[na:na]at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invokeNativeMethodAccessorImpl.java:62 ~[na:na]at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invokeDelegatingMethodAccessorImpl.java:43 ~[na:na]at java.base/java.lang.reflect.Method.invokeMethod.java:566~[na:na]Java Spring Security中需要授权的文本表示,java,spring-boot,spring-security,Java,Spring Boot,Spring Security,我在Spring Security中的注册权限有问题 我不能做注册的方法 我试图设置每个路径的访问权限,但没有帮助 控制器 证券配置 用户服务 用户模型 错误 java.lang.IllegalArgumentException:授予的文本权限 代表须于 org.springframework.util.Assert.hasTextAssert.java:284 ~[spring-core-5.2.4.释放。震击器:5.2.4.释放]位于 org.springframework.security
在用户模型类中,确保设置了角色,以便getAuthories方法能够工作 错误提示您正在使用空角色执行新的SimpleGrantedAuthority
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
List<GrantedAuthority> listRole = new ArrayList<GrantedAuthority>();
listRole.add(new SimpleGrantedAuthority(role)); // this is the problematic line!
return listRole;
}
如果您没有角色,那么只需返回一个空列表即可
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Collections.emptyList();
}
这是我的JSON,我使用defult角色。 我试图返回一个空列表,但我有同样的错误
{
"accountNonExpired": true,
"accountNonLocked": true,
"authorities": [
{
"authority": "string"
}
],
"credentialsNonExpired": true,
"enabled": true,
"id": 0,
"lastName": "Piotr",
"name": "Piotr",
"password": "Piotr",
"role": "ROLE_ADMIN",
"username": "Piotr"
}
邮差表格错误
**"timestamp": "2020-04-01T15:20:05.670+0000",
"status": 500,
"error": "Internal Server Error",
"message": "JSON conversion problem: A granted authority textual representation is required; nested exception is com.fasterxml.jackson.databind.JsonMappingException: A granted authority textual representation is required\n at [Source: (PushbackInputStream); line: 4, column: 18] (through reference chain: com.xxx`enter code here`.xxx.models.User[\"authorities\"])",
"path": "/register"
}**
我相信你必须创建用户对象,比如 新的org.springframework.security.core.userdetails.Usersername,passwordencode.encode user.getPass,grantedAuthorityList 这是我的作品
@Service
public class UserSecurityService implements UserDetailsService {
@Autowired
YourRepository yourRepository;
@Autowired
PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
User user = yourRepository.findUserByUser(username);
List<String> listRoles = new ArrayList<>();
//List<UserRoles> userRoles = user.getUserRoleList();
user.getUserRoleList().forEach(role->listRoles.add(role.getRole().getRole()));// get role from database - usa il tuo modo **
grantedAuthorityList = listRoles.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
//*** important ****
return new org.springframework.security.core.userdetails.User(username,
passwordEncoder.encode(user.getPass()), grantedAuthorityList);
}
请参阅我的最新答案。对于空列表,它肯定会起作用,因为您不再使用新的SimpleGrantedAuthority,这就是您的错误所在。也许错误与此不同?我发现错误是JSON的错误,因为我使用了swagger,他给我生成了一个错误的JsonI:在[Source:PushbackInputStream;line:5,column:5]通过引用链:models.User[Authories]->java.util.Collections$EmptyList[0]]对于根原因com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造org.springframework.security.core.GrantedAuthority的实例与默认构造一样,不存在任何创建者:抽象类型需要映射到具体类型,具有自定义反序列化器,或者在[来源:PushbackInputStream;行:5,列:5]通过引用链:models.User[authorities]->java.util.Collections$EmptyList[0]
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Collections.emptyList();
}
{
"accountNonExpired": true,
"accountNonLocked": true,
"authorities": [
{
"authority": "string"
}
],
"credentialsNonExpired": true,
"enabled": true,
"id": 0,
"lastName": "Piotr",
"name": "Piotr",
"password": "Piotr",
"role": "ROLE_ADMIN",
"username": "Piotr"
}
**"timestamp": "2020-04-01T15:20:05.670+0000",
"status": 500,
"error": "Internal Server Error",
"message": "JSON conversion problem: A granted authority textual representation is required; nested exception is com.fasterxml.jackson.databind.JsonMappingException: A granted authority textual representation is required\n at [Source: (PushbackInputStream); line: 4, column: 18] (through reference chain: com.xxx`enter code here`.xxx.models.User[\"authorities\"])",
"path": "/register"
}**
@Service
public class UserSecurityService implements UserDetailsService {
@Autowired
YourRepository yourRepository;
@Autowired
PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
User user = yourRepository.findUserByUser(username);
List<String> listRoles = new ArrayList<>();
//List<UserRoles> userRoles = user.getUserRoleList();
user.getUserRoleList().forEach(role->listRoles.add(role.getRole().getRole()));// get role from database - usa il tuo modo **
grantedAuthorityList = listRoles.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
//*** important ****
return new org.springframework.security.core.userdetails.User(username,
passwordEncoder.encode(user.getPass()), grantedAuthorityList);
}