java无法使用GrantedAuthority字段保存对象(不存在任何创建者,如默认构造)
我一直在尝试在我的项目中启用基于表单的用户注册
只需注册一个没有角色的用户就可以了,但是添加一些角色会给我带来一个错误:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
org.springframework.security.core.GrantedAuthority
(no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: (PushbackInputStream); line: 9, column: 21] (through reference chain: com.warehousing.model.User["authorities"]->java.util.HashSet[0])
我怀疑问题来自于这样一个事实:Spring不知道如何将我的权威表示转换为扩展GrantedAuthority
的东西。话虽如此,我试着从GrantedAuthority
切换到SimpleGrantedAuthority
,但问题依然存在
我的用户类如下所示:
public class User implements UserDetails {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String fName;
private String lName;
private String phone;
private String email;
// Properties for Spring authentication
@ElementCollection(targetClass=SimpleGrantedAuthority.class, fetch = FetchType.EAGER)
@JsonDeserialize(as = SimpleGrantedAuthority.class)
private Set<? extends GrantedAuthority> grantedAuthorities;
@NotNull
private String password;
@NotNull
private String username;
private boolean isAccountNonExpired;
private boolean isAccountNonLocked;
private boolean isCredentialsNonExpired;
private boolean isEnabled;
public User(String fName, String lName, String phone, String email,
Set<SimpleGrantedAuthority> grantedAuthorities, String password, String username,
boolean isAccountNonExpired, boolean isAccountNonLocked,
boolean isCredentialsNonExpired, boolean isEnabled) {
this.fName = fName;
this.lName = lName;
this.phone = phone;
this.email = email;
this.grantedAuthorities = grantedAuthorities;
this.password = password;
this.username = username;
this.isAccountNonExpired = isAccountNonExpired;
this.isAccountNonLocked = isAccountNonLocked;
this.isCredentialsNonExpired = isCredentialsNonExpired;
this.isEnabled = isEnabled;
}
// Getters, setters & constructors
}
MyUserController创建新用户的方法:
@RestController
public class UserController {
@Autowired
UserService userService;
@PostMapping("/users")
public User createNew(@Valid @RequestBody User user) {
return userService.addUser(user);
}
}
它调用用户服务:
public class UserService implements UserDetailsService {
@Autowired
UserRepository userRepo;
public User addUser(User user) {
Set<SimpleGrantedAuthority> userRoles = user.getGrantedAuthorities()
.stream()
.map(p -> new SimpleGrantedAuthority(p.getAuthority()))
.collect(Collectors.toSet());
User newUser = new User(user.getfName(), user.getlName(),
user.getPhone(), user.getEmail(),
userRoles,
user.getPassword(), user.getUsername(),
user.isAccountNonExpired(),
user.isAccountNonLocked(),
user.isCredentialsNonExpired(),
user.isEnabled());
return userRepo.save(newUser);
}
}
现在,发布这样的JSON很好:
{
"fName": "First",
"lName": "Last",
"phone": "+3111111111",
"email": "test@mail.com",
"password": "password",
"username": "user",
"enabled": true,
"authorities": [],
"accountNonLocked": true,
"accountNonExpired": true,
"credentialsNonExpired": true
}
但是,添加某种权限会导致上述错误:
{
"fName": "First",
"lName": "Last",
"phone": "+3111111111",
"email": "test@mail.com",
"password": "password",
"username": "user",
"enabled": true,
"authorities": [{
"authority": "inventory:read"
}],
"accountNonLocked": true,
"accountNonExpired": true,
"credentialsNonExpired": true
}
感谢您的任何帮助
共 (0) 个答案