【发布时间】:2020-06-09 18:56:37
【问题描述】:
我们可以通过使用此代码重置 Spring SecurityContextHolder 中的 Authentication 对象(安全令牌)来动态更新已登录用户的权限,而无需注销并登录。
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
List<GrantedAuthority> updatedAuthorities = new ArrayList<>(auth.getAuthorities());
updatedAuthorities.add(...); //add your role here [e.g., new SimpleGrantedAuthority("ROLE_NEW_ROLE")]
Authentication newAuth = new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials(), updatedAuthorities);
SecurityContextHolder.getContext().setAuthentication(newAuth);
但此代码不会更新数据库中的权限,我想更新 OAuth oauth_access_token 和 oauth_refresh_token 表的数据库表中的权限。实际上,我正在开发一个用户权限经常变化的社交应用程序。
Spring 是否提供开箱即用的此功能?
或者你有什么自定义逻辑?
【问题讨论】:
标签: java spring-boot spring-security oauth-2.0 spring-security-oauth2