我有一个Spring服务,我正在尝试向它添加安全性。我遵循了本教程,但是当我试图直接访问服务时,我会得到以下错误:
出现了意外错误(type=Internal服务器错误,status=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令牌添加到命令中?服务有一个自签名证书,如果这有任何区别的话。
发布于 2016-04-11 20:04:34
您需要拦截-url元素中的hasRole('ROLE_USER')。
<intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>可以使用的其他表达式,请参见文档。
发布于 2016-09-21 18:20:16
如果不需要启用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,或者需要将下面的logout.The添加到表单中:
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}" />发布于 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" />
)
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") .and().formLogin() .csrf().disable() ;}https://stackoverflow.com/questions/36557321
复制相似问题