首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未能使用spring安全性计算表达式

未能使用spring安全性计算表达式
EN

Stack Overflow用户
提问于 2016-04-11 19:14:37
回答 3查看 12.6K关注 0票数 4

我有一个Spring服务,我正在尝试向它添加安全性。我遵循了本教程,但是当我试图直接访问服务时,我会得到以下错误:

出现了意外错误(type=Internal服务器错误,status=500)。未能计算表达式“ROLE_USER”

以下是我的安全配置:

webSecurityConfig.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
public class SpringSecurityConfig {

    public SpringSecurityConfig() {
        super();
    }

}

当我试图使用curl登录时,我也会得到这个错误:

代码语言:javascript
复制
{
"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令牌添加到命令中?服务有一个自签名证书,如果这有任何区别的话。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-11 20:04:34

您需要拦截-url元素中的hasRole('ROLE_USER')

代码语言:javascript
复制
<intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>

可以使用的其他表达式,请参见文档

票数 3
EN

Stack Overflow用户

发布于 2016-09-21 18:20:16

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

代码语言:javascript
复制
        <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,或者需要将下面的logout.The添加到表单中:

代码语言:javascript
复制
<input type="hidden" name="${_csrf.parameterName}"
            value="${_csrf.token}" />
票数 4
EN

Stack Overflow用户

发布于 2016-06-01 15:38:39

Spring安全阻止发布请求.

要启用它,您可以:

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

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

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

)

  • 或者,如果使用指定,可以通过在csrf().disable上添加WebSecurityConfigurerAdapter来直接在代码中发布(我还没有找到对应的WebSecurityConfigurerAdapter): @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") .and().formLogin() .csrf().disable() ;}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36557321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档