开发者社区> 问答> 正文

无法理解表达式“ hasRole(USER)”

我想使用Spring Security 5.3

像这样在xml上配置

<http auto-config="true">
    <intercept-url pattern="/list" access="hasRole(USER)"/>
    <intercept-url pattern="/security" access="isAnonymous()"/>
    <http-basic />
    <form-login login-page="/security"
                  login-processing-url="/security"
                  default-target-url="/list"
                  authentication-failure-url="/security?error"
                  username-parameter="username"
                  password-parameter="password"/>

    <logout logout-success-url="/security?logout"/>
    <csrf disabled="true"/>
</http>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="user" password="$2a$10$BHjEcnhAgqRH0Vj6aPmGTOtQfYdx3PsvTWjsVxVBouiLTzGSLTSz2" authorities="USER"/>
        </user-service>
        <password-encoder ref="encoder" />
    </authentication-provider>
</authentication-manager>

<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

登录时出现此错误:

java.lang.IllegalArgumentException:无法评估表达式“ hasRole(USER)” org.springframework.security.access.expression.ExpressionUtils.evaluateAsBoolean(ExpressionUtils.java:30)org.springframework.security.web.access.expression.WebExpressionVoter。投票(WebExpressionVoter.java:52)org.springframework.security.web.access.expression.WebExpressionVoter.vote(WebExpressionVoter.java:33)org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:63) org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:233)org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:123)org.springframework.security.web。 access.intercept.FilterSecurityInterceptor。doFilter(FilterSecurityInterceptor.java:90)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)组织。 springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain。 doFilter(FilterChainProxy.java:334)org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)组织。springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:158)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)org.springframework.security.web.savedrequest.RequestCacheAwareFilter。 doFilter(RequestCacheAwareFilter.java:63)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:155) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)org.springframework.security.web。authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter。 java:116)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:92)org.springframework.security。 web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:77)org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)org.springframework.security。 web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy。 java:334)org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)org.springframework.web.filter.DelegatingFilterProxy。invokeDelegate(DelegatingFilterProxy.java:358)org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)

根本原因

org.springframework.expression.spel.SpelEvaluationException:EL1008E:在“ org.springframework.security.web.access.expression.WebSecurityExpressionRoot”类型的对象上找不到属性或字段“ USER”-可能不是公共的或无效的?org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217)org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)org.springframework.expression.spel.ast。 PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91)org.springframework.expression.spel.ast.MethodReference.getArguments(MethodReference.java:164)org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:94) org.springframework.expression.spel。

我的代码有什么错误?

问题来源:Stack Overflow

展开
收起
montos 2020-03-24 14:56:55 478 0
1 条回答
写回答
取消 提交回答
  • org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'USER' cannot be found on object of type

    从上面的错误中您可以看到问题是Spring Security框架无法识别USER。您忘记了将USER括在''中。

    问题在于表达式:<intercept-url pattern="/list" access="hasRole(USER)"/>。替换为 <intercept-url pattern="/list" access="hasRole('USER')"/>它将起作用。

    我看到的另一个问题是您正在使用hasRole保护您的/list资源,但是您已授予用户权限USER authorities="USER"。将会发生的是hasRole会将前缀ROLE_添加到您传入的USER参数中。因此ROLE_USER不会等于USER,这是您分配给用户的权限。

    要解决此问题,您可以将权限更改为```authorities =“ ROLE_USER”``。

    或者,您也可以将hasRole替换为hasAuthority,如下所示: access="hasAuthority('USER')"

    回答来源:Stack Overflow

    2020-03-24 14:57:47
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载