【问题标题】:Spring Security Access roleSpring 安全访问角色
【发布时间】:2011-01-01 03:11:40
【问题描述】:

我有一个使用 Spring Security 在 Spring Web Flow 上运行的 j2ee Web 应用程序。如何在运行时更改会话中保存的角色?

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    如果可能的话,应该是这样的:

    SecurityContext context = SecurityContextHolder.getContext();
    Object principal = context.getAuthentication().getPrincipal();
    Object credentials = context.getAuthentication().getCredentials();
    GrantedAuthority[] authorities = new GrantedAuthority[1];
    
    authorities[0] = new GrantedAuthorityImpl("MY_NEW_ROLE");
    
    Authentication auth = new UsernamePasswordAuthenticationToken(
        principal, credentials, authorities);
    SecurityContextHolder.getContext().setAuthentication(auth);
    

    【讨论】:

    • 如果用户正在访问受限页面,我如何将其重定向到另一个页面。因为目前它显示错误 404--Not Found From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1: 10.4.5 404 Not Found 服务器没有找到任何匹配请求 URI 的东西。没有说明这种情况是暂时的还是永久性的。
    • 我的回答对您有帮助吗?我在你的新帖子中回答了这个不相关的问题。
    猜你喜欢
    • 2013-12-15
    • 1970-01-01
    • 2014-07-23
    • 2012-04-08
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    相关资源
    最近更新 更多