Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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有任何角色不工作_Java_Spring_Spring Security - Fatal编程技术网

Java Spring Security有任何角色不工作

Java Spring Security有任何角色不工作,java,spring,spring-security,Java,Spring,Spring Security,我正在用spring安全认证和MongoDB开发一个应用程序。身份验证方法工作正常,但仅适用于角色\u ADMIN。我需要进行身份验证的所有方法都带有以下注释: @预授权(“hasAnyRole('ROLE\u ADMIN'、'ROLE\u USER')”) 当我尝试与具有角色的用户\u user进行身份验证时,总是会出现访问被拒绝错误 我的spring安全配置是: <security:global-method-security pre-post-annotations="enabled

我正在用spring安全认证和MongoDB开发一个应用程序。身份验证方法工作正常,但仅适用于
角色\u ADMIN
。我需要进行身份验证的所有方法都带有以下注释:

@预授权(“hasAnyRole('ROLE\u ADMIN'、'ROLE\u USER')”)

当我尝试与具有
角色的用户\u user
进行身份验证时,总是会出现访问被拒绝错误

我的spring安全配置是:

<security:global-method-security pre-post-annotations="enabled" />  
<security:http auto-config="true" use-expressions="true">
<intercept-url pattern="/login" access="permitAll" />
<intercept-url pattern="/logout" access="permitAll" />
<intercept-url pattern="/admin/**" 
  access="hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')" />
<security:form-login login-page="/login" default-target-url="/admin/main" 
   authentication-failure-url="/accessdenied" />
        <security:logout logout-success-url="/logout" />

        <security:session-management>
            <security:concurrency-control error-if-maximum-exceeded="true" 
               max-sessions="1"/>
        </security:session-management>
    </security:http>
我可以用
ROLE\u ADMIN
用户登录,但不能用
ROLE\u用户
登录

在我的
登录服务中
我有:

public UserDetails loadUserByUsername(String email)
            throws UsernameNotFoundException {
        boolean enabled = true;
        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean accountNonLocked = true;

        User user = getUserDetail(email);

        userdetails = new org.springframework.security.core.userdetails.User(
                user.getEmail(), user.getPwd(), enabled,
                accountNonExpired, credentialsNonExpired, accountNonLocked,
                getAuthorities(user.getIsAdmin()));

        return userdetails;
    }

    public List<GrantedAuthority> getAuthorities(Integer role) {
        List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
        if (role.intValue() == 0) {
            authList.add(new SimpleGrantedAuthority("ROLE_USER"));
        } else if (role.intValue() == 1) {
            authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        }
        System.out.println(authList);
        return authList;
    }
public UserDetails loadUserByUsername(字符串电子邮件)
抛出UsernameNotFoundException{
布尔启用=真;
布尔值accountNonExpired=true;
布尔CredentialsNoExpired=true;
布尔值accountNonLocked=true;
用户=getUserDetail(电子邮件);
userdetails=new org.springframework.security.core.userdetails.User(
user.getEmail(),user.getPwd(),已启用,
AccountNoExpired、CredentialsNoExpired、AccountNoLocked、,
getAuthories(user.getIsAdmin());
返回用户详细信息;
}
公共列表权限(整数角色){
List authList=new ArrayList();
if(role.intValue()==0){
添加(新的SimpleGrantedAuthority(“角色用户”);
}else if(role.intValue()==1){
添加(新的SimpleGrantedAuthority(“角色管理”);
}
System.out.println(authList);
返回authList;
}
我错过了什么?

这个

access="hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')
这意味着两个角色必须同时设置,并且示例代码显示它永远不会发生,因为只能设置两个角色中的一个


它不同于
hasAnyRole('ROLE\u ADMIN','ROLE\u USER')
,如果至少设置了其中一个角色,则该语句应为true。station
hasAnyRole
类似于
条件。

当您遇到此类问题时,请先尝试添加许多
系统.out.println
(或调试日志)以查看您的方法是否有效,同时更改:

hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')
为了


并检查
role.intValue()==0
是否使用
sysout

打印
true
。如果用户只能有一个rol(
role\u user
role\u ADMIN
),则第一个示例必须是
hasRole('role\u ADMIN')或hasRole('role\u user')
(注意
).Still
先前已验证:org.springframework.security.authentication。UsernamePasswordAuthenticationToken@8f0d3531:Principal:org.springframework.security.core.userdetails。User@2684c716:用户名:sub。coutinho@xxx.xyz; 密码:[受保护];启用:真;AccountNoExpired:正确;无需证明的凭证:真实;AccountNonLocked:true;未授予任何权限;凭据:[受保护];认证:正确;详细信息:org.springframework.security.web.authentication。WebAuthenticationDetails@0:RemoteIP地址:0:0:0:0:0:0:0:0:1;会话ID:1DCQDWF2IQ51**未授予任何权限**
Samething。自动识别过程可行,但没有给出授权。堆栈跟踪在上面。问题出在哪里?
access
getAuthorities
方法?,
System.out.println
的打印内容是什么,在
if role.intValue()==XX
中添加更多打印内容并添加输出。您的方法
getAuthorities
似乎不起作用,您确定它被调用了吗?请添加
getuserDetail(email)
方法。问题是getAuthorities。正在调用GetAuthories方法,但我不知道
role.intValue()==0
是否为普通用户(role\u用户)提供了错误信息。运行debug时,会调用所有内容,但当用户具有ROLE\u时,会将user not athorities提供给该用户。是的,我更改了该设置,但ROLE\u用户不工作。不要将'ROLE\u'前缀与
hasRole
hasAnyRole
一起使用@有了它,我们可以更改为
hasAnyRole('ADMIN','USER')
access="hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')
hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')
hasRole('ROLE_ADMIN') or hasRole('ROLE_USER')