用户用多个角色,默认角色登录后,在不退出的情况下进行多角色切换,重新设置权限。
代码//根据当前用户所选角色,获取对应权限 SetgrantedAuths = obtainGrantedAuthorities(this.getLoginUser().getSysUser(), selRoleId); //通过SecurityContextHolder.getContext()安全上下文,获取Authentication 实例 Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if(auth != null) { //重新认证 Authentication newAuth = new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials(),grantedAuths); //写回上下文 SecurityContextHolder.getContext().setAuthentication(newAuth); }



