zoukankan      html  css  js  c++  java
  • Spring Security自定义GrantedAuthority前缀

    如果我们正使用Spring Security提供默认的基于方法的权限认证注解如下:

    @PreAuthorize("hasAnyRole('ADMIN', 'USER')")
    public void moveTo(String id, String parentId) {
        // ...
    }

    而在我们自定义实现的GrantedAuthority,或是SS提供的SimpleGrantedAuthority,

    public interface GrantedAuthority extends Serializable {
    	
    	/**
    	 * 这里返回的即是hasAnyRole(..)中的角色
    	 */
    	String getAuthority();
    }

    这里的getAuthority需要加上”ROLE_”的前缀:

    public final class SimpleGrantedAuthority implements GrantedAuthority {
    	private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
    	private final String role;
    	@Override
    	public String getAuthority() {
    		return "Role_" + role;
    	}
    }

     

    否则你会发现

    @PreAuthorize 中的角色不能正确被识别,这是因为Spring Security框架内的DefaultMethodSecurityExpressionHandler 
    在验证表达式时是有”ROLE_”前缀的。
     
    有人就问了,那我就是不想要这个前缀,怎么办?(其实这个人就是我)
     
    两个办法,方法1:定义一个GrantedAuthorityDefaults并注入,that’s all
    @Bean
    GrantedAuthorityDefaults grantedAuthorityDefaults() {
        return new GrantedAuthorityDefaults(""); // Remove the ROLE_ prefix
    }
     
    方法2:在WebSecurityConfigurerAdapter自定义实现中,重写方法,替换默认的DefaultWebSecurityExpressionHandler设置
     
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.expressionHandler(new DefaultWebSecurityExpressionHandler() {
            @Override
            protected SecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, FilterInvocation fi) {
                WebSecurityExpressionRoot root = (WebSecurityExpressionRoot) super.createSecurityExpressionRoot(authentication, fi);
                root.setDefaultRolePrefix(""); //remove the prefix ROLE_
                return root;
            }
        });
    }
     
  • 相关阅读:
    hdoj 1872 稳定排序
    nyoj 60 谁获得了最高奖学金
    hdoj 2066 一个人的旅行
    nyoj 8 一种排序
    bzoj1798 1
    bzoj4031
    SPOJ-HIGH
    学习笔记::矩阵树定理
    学习笔记::树上莫队
    Strip
  • 原文地址:https://www.cnblogs.com/gugia/p/10723318.html
Copyright © 2011-2022 走看看