我想使用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
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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。