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;
            }
        });
    }
     
  • 相关阅读:
    前后台验证字符串长度
    接口和抽象类该什么时候用?
    程序员常去网站汇总
    SQLServer复合查询条件(AND,OR,NOT)对NULL值的处理方法
    c#-轮询算法
    常用的SQL语句
    HTTP请求工具类
    asp.net mvc jQuery 城市二级联动
    ibatis动态多条件查询及模糊查询(oracle,mysql,sql)
    iBatis 中 Like 的写法实现模糊查询
  • 原文地址:https://www.cnblogs.com/gugia/p/10723318.html
Copyright © 2011-2022 走看看