【问题标题】:Adding an authentication with SecurityContext.xml on Spring Boot在 Spring Boot 上使用 SecurityContext.xml 添加身份验证
【发布时间】:2018-08-16 11:43:21
【问题描述】:

我无法使用SecurityContext.xml 添加页面角色。 我该怎么办?

使用配置类进行推送配置。 但 XML 配置不起作用:

我的 XML 配置

<?xml version="1.0" encoding="UTF-8" ?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">

<http auto-config="true">
    <intercept-url pattern="/**" access="ROLE_USER" />
</http>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="user" password="user" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

</beans:beans>

此配置在运行时产生此错误。

java.lang.IllegalArgumentException: 无法在 org.springframework.security.access.expression.ExpressionUtils.evaluateAsBoolean(ExpressionUtils.java:30) 评估表达式“ROLE_USER”~[spring-security-core-5.0.7.RELEASE .jar:5.0.7.RELEASE] 在 org.springframework.security.web.access.expression.WebExpressionVoter.vote(WebExpressionVoter.java:52) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在

【问题讨论】:

  • access 需要一个表达式,而不仅仅是角色的名称。类似于access="hasRole('ROLE_USER')"

标签: xml spring spring-boot spring-security illegalargumentexception


【解决方案1】:

这是不对的:

    <authentication-provider>
       <user-service>
          <user name="user" password="user" authorities="ROLE_USER" />
       </user-service>
    </authentication-provider>

对于 authentication-provider,您需要提供对 AuthenticationProvider 的 bean 的引用。

例如

<authentication-manager >
    <authentication-provider ref="myAuthProvider"/>
</authentication-manager>

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2014-09-10
    • 2021-09-03
    • 2018-02-27
    • 2017-04-12
    • 2022-01-20
    • 2018-11-17
    • 2018-08-29
    • 2023-03-19
    相关资源
    最近更新 更多