Has*_*ali 40 java csrf spring-security spring-restcontroller
我正在使用spring security和java config
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/*").hasRole("ADMIN")
.and()
.addFilterAfter(new CsrfTokenResponseHeaderBindingFilter(), CsrfFilter.class)
.exceptionHandling()
.authenticationEntryPoint(restAuthenticationEntryPoint)
.and()
.formLogin()
.successHandler(authenticationSuccessHandler)
.failureHandler(new SimpleUrlAuthenticationFailureHandler());
Run Code Online (Sandbox Code Playgroud)
我正在使用PostMan来测试我的REST服务.我成功获得'csrf token',我可以使用X-CSRF-TOKEN请求标头登录.但登录后我点击发布请求(我在请求标题中包含相同的令牌,我用于登录发布请求)我收到以下错误消息:
HTTP状态403 - 无法验证提供的CSRF令牌,因为找不到您的会话.
任何人都可以指导我做错了什么.
Der*_*ick 72
根据spring.io:
什么时候应该使用CSRF保护?我们的建议是对普通用户可以由浏览器处理的任何请求使用CSRF保护.如果您只创建非浏览器客户端使用的服务,则可能需要禁用CSRF保护.
所以要禁用它:
@Configuration
public class RestSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
Run Code Online (Sandbox Code Playgroud)
注意: Java配置默认启用CSRF保护
尝试这个:@Override protected boolean sameOriginDisabled() { return true;}
@Configuration
public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
...
// Determines if a CSRF token is required for connecting. This protects against remote
// sites from connecting to the application and being able to read/write data over the
// connection. The default is false (the token is required).
@Override
protected boolean sameOriginDisabled() {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
来源:WebSocket 安全:禁用 WebSocket 内的 CSRF
小智 -4
我已经通过在登录页面添加最后一个属性解决了这个问题,也许它会对您有所帮助。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59823 次 |
| 最近记录: |