RT,记录下,便于下次使用,解决代码
public static void updateAuthentication(User user, UserRolesMapper userRolesMapper) {
if (Objects.isNull(user) || Objects.isNull(user.getId())) {
return;
}
Integer id = user.getId();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
List<GrantedAuthority> updatedAuthorities = new ArrayList<>();
// get authentication
List<Role> roles = userRolesMapper.selectRolesByUserId(id);
if (!CollectionUtils.isEmpty(roles)) {
for (Role role : roles) {
updatedAuthorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName()));
}
}
// update authorities
Authentication newAuth = new UsernamePasswordAuthenticationToken(auth.getPrincipal(),
auth.getCredentials(), updatedAuthorities);
SecurityContextHolder.getContext().setAuthentication(newAuth);
}