【问题标题】:Failed to evaluate expression with spring security无法使用弹簧安全评估表达式
【发布时间】:2016-08-02 02:59:05
【问题描述】:

我有一个 Spring rest 服务,我正在尝试为其添加安全性。我关注了this tutorial,但是当我尝试直接访问该服务时,出现以下错误:

出现意外错误(类型=内部服务器错误, 状态=500)。无法评估表达式“ROLE_USER”

这是我的安全配置:

webSecurityConfig.xml

<http entry-point-ref="restAuthenticationEntryPoint">
      <intercept-url pattern="/**" access="ROLE_USER"/>

      <form-login
         authentication-success-handler-ref="mySuccessHandler"
         authentication-failure-handler-ref="myFailureHandler"
      />

      <logout />
   </http>

   <beans:bean id="mySuccessHandler"
      class="com.eficid.cloud.security.rest.AuthenticationSuccessHandler"/>
   <beans:bean id="myFailureHandler" class=
     "org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"/>


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

SpringSecurityConfig:

public class SpringSecurityConfig {

    public SpringSecurityConfig() {
        super();
    }

}

我在尝试使用 curl 登录时也遇到此错误:

{
"timestamp":1460399841286,
"status":403,"error":"Forbidden",
"message":"Could not verify the provided CSRF token because your session was not found.",
"path":"/spring-security-rest/login"
}

我需要手动将 csrf 令牌添加到命令中吗?该服务有一个自签名证书,如果这有什么不同的话。

【问题讨论】:

  • 您似乎没有 HTTP 会话。

标签: spring spring-security csrf self-signed


【解决方案1】:

如果您不需要启用 CRF,那么您可以在 webSecurityConfig.xml 文件中禁用它,如下所示:

        <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/login.html" access="hasRole('ANONYMOUS')" />
        <intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
        <!-- This form is a default form that used to login  
            <http-basic/>
         -->
         <form-login login-page="/login.html"/>
         <csrf disabled="true"/>
    </http>

如果启用了 CSRF,您必须在要登录或注销的页面中包含一个 _csrf.token。以下代码需要添加到表单中:

<input type="hidden" name="${_csrf.parameterName}"
            value="${_csrf.token}" />

【讨论】:

    【解决方案2】:

    intercept-url 元素中需要hasRole('ROLE_USER')

    <intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
    

    请参阅docs 了解您可以使用的其他表达式。

    【讨论】:

    • 海@Evgeni。当我与“X-CSRF-TOKEN”一起登录时,我的登录成功执行。但在那之后,当我发送一些请求时,我得到“无法验证提供的 CSRF 令牌,因为找不到您的会话。”信息。我在给定的代码中给出了正确的模式和访问权限。你能建议什么吗?提前致谢
    • @HaseebWali 我建议您提出另一个问题并提供更多详细信息,例如您的配置...
    【解决方案3】:

    Spring 安全性阻止 POST 请求。

    要启用它,您可以:

    • 在所有表单请求之后添加:

    &lt;input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" class="form-control" /&gt;

    (例如: <form id="computerForm" action="addComputer" method="POST"> <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" class="form-control" />

    )

    • 或者如果您使用注释,您可以通过在您的 WebSecurityConfigurerAdapter 上添加 csrf().disable 来直接在代码中允许 POST(我还没有找到等效的 xml):

      @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") .and().formLogin() .csrf().disable() ;}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多