Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring security添加了前缀";角色“uquot;所有角色的名称?_Java_Spring_Spring Mvc_Spring Security_Role - Fatal编程技术网

Java Spring security添加了前缀";角色“uquot;所有角色的名称?

Java Spring security添加了前缀";角色“uquot;所有角色的名称?,java,spring,spring-mvc,spring-security,role,Java,Spring,Spring Mvc,Spring Security,Role,我的Web安全配置中有以下代码: @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/api/**") .hasRole("ADMIN") .and() .httpBasic().and().c

我的Web安全配置中有以下代码:

 @Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/api/**")
            .hasRole("ADMIN")
            .and()
            .httpBasic().and().csrf().disable();

}
因此,我在数据库中添加了一个具有“ADMIN”角色的用户,当我尝试与该用户登录时,总是出现403错误,然后我为spring启用了log,我发现这一行:

2015-10-18 23:13:24.112 DEBUG 4899 --- [nio-8080-exec-1] o.s.s.w.a.i.FilterSecurityInterceptor    : Secure object: FilterInvocation: URL: /api/user/login; Attributes: [hasRole('ROLE_ADMIN')]

为什么Spring Security要找“角色\管理员”而不是“管理员”

默认情况下,Spring security会添加前缀“角色”

如果您想删除或更改此内容,请查看

编辑:也找到了这个:

在Spring 4中,有两种方法
hasAuthority()
hasAnyAuthority()
org.springframework.security.access.expression.SecurityExpressionRoot
类中定义。这两种方法只检查您的自定义角色名,而不检查添加
角色前缀的。定义如下:

public final boolean hasAuthority(String authority) {
    return hasAnyAuthority(authority);
}
public final boolean hasAnyAuthority(String... authorities) {
    return hasAnyAuthorityName(null, authorities);
}
private boolean hasAnyAuthorityName(String prefix, String... roles) {
    Set<String> roleSet = getAuthoritySet();

    for (String role : roles) {
        String defaultedRole = getRoleWithDefaultPrefix(prefix, role);
        if (roleSet.contains(defaultedRole)) {
            return true;
        }
    }

    return false;
}
private static String getRoleWithDefaultPrefix(String defaultRolePrefix, String role) {
    if (role == null) {
        return role;
    }
    if (defaultRolePrefix == null || defaultRolePrefix.length() == 0) {
        return role;
    }
    if (role.startsWith(defaultRolePrefix)) {
        return role;
    }
    return defaultRolePrefix + role;
}
public权限(字符串权限){
返回hasAnyAuthority(authority);
}
公共最终布尔值hasAnyAuthority(字符串…权限){
return hasAnyAuthorityName(null,authorities);
}
私有布尔hasAnyAuthorityName(字符串前缀、字符串…角色){
Set-roleSet=getAuthoritySet();
for(字符串角色:角色){
字符串defaultedRole=getRoleWithDefaultPrefix(前缀,角色);
if(角色集包含(defaultedRole)){
返回true;
}
}
返回false;
}
私有静态字符串getRoleWithDefaultPrefix(字符串defaultRolePrefix,字符串角色){
如果(角色==null){
返回角色;
}
如果(defaultRolePrefix==null | | defaultRolePrefix.length()==0){
返回角色;
}
if(role.startsWith(defaultRolePrefix)){
返回角色;
}
返回defaultRolePrefix+角色;
}
用法示例:



正如@olyanren所说,您可以在Spring4中使用hasAuthority()方法,而不是hasRole()。我正在添加JavaConfig示例:

@Override
protected void configure(HttpSecurity http) throws Exception {
    .authorizeRequests()
    .antMatchers("/api/**")
    .access("hasAuthority('ADMIN')")
    .and()
    .httpBasic().and().csrf().disable();
}

您可以创建映射器,在所有角色的开头添加
角色

@Bean
public GrantedAuthoritiesMapper authoritiesMapper() {
    SimpleAuthorityMapper mapper = new SimpleAuthorityMapper();
    mapper.setPrefix("ROLE_"); // this line is not required 
    mapper.setConvertToUpperCase(true); // convert your roles to uppercase
    mapper.setDefaultAuthority("USER"); // set a default role

    return mapper;
}
您应该将映射器添加到您的提供程序:

@Bean
public DaoAuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    // your config ...
    provider.setAuthoritiesMapper(authoritiesMapper());

    return provider;
}

_spring安全性使用角色前缀来标识它是否为角色。角色具有一组权限,也称为权限,这些权限为角色定义不同的权限。 示例:-编辑配置文件,删除配置文件

您可以同时定义角色和权限,如果要定义角色,则必须在其前面加上“role_”


在您的例子中,您正在查找一个角色,因此默认情况下,spring security会查找一个前缀为“role_uu”的字符串。

如果我在@Secured annotation中添加一个角色,spring是否会默认添加role_u?比如,如果我添加@Secured(“abc”),Spring会检查角色\u abc还是只检查abc?@SajibAcharya您可以更改默认的
角色\uu
。请看
@Bean
public DaoAuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    // your config ...
    provider.setAuthoritiesMapper(authoritiesMapper());

    return provider;
}