java - 是否有必要保护 JAX-RS 请求免受 CSRF 攻击?

标签 java rest spring-security jax-rs csrf

是否有必要针对 CSRF 保护 JAX-RS 请求?

通过 definition REST 是无状态的,因此不存在 session ID( session cookie),因为根本没有 session (另请参见 https://stackoverflow.com/a/15746639/5277820 )。

我的 Spring Security Java 配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Configuration
    @Order(1)
    public static class JaxRsWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(final HttpSecurity http) throws Exception {
            http
                .antMatcher("/services/**")
                .csrf().disable()
                .authorizeRequests()
                    .antMatchers(HttpMethod.OPTIONS, "/services/**").permitAll()              
                    .anyRequest().hasAuthority("ROLE_user")
                    .and()
                .httpBasic()
                    .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
             }
        }
    }
}

但我发现例如以下博客:Stateless Spring Security Part 1: Stateless CSRF protection .不幸的是,该博客没有解释为什么需要 CSRF 保护。

没有session cookie还有其他CSRF攻击吗?

最佳答案

CSRF 攻击不需要 session 存在。 CSRF 攻击包括代表用户做某事,方法是诱使他/她单击链接或提交表单,该表单转到用户登录的应用程序。

使用基本身份验证还是 session cookie 来识别用户是无关紧要的。

请注意,使用 cookie 并不意味着该应用不是无状态的。 Cookie 与基本身份验证一样,只是在每个 HTTP 请求中发送一个额外的 header 。

关于java - 是否有必要保护 JAX-RS 请求免受 CSRF 攻击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33211562/

相关文章:

java - 如何使用selenium webdriver访问pdf链接

java - 当我们在应用程序日志中有线程名称时,如何识别线程启动的 Java 代码

java - 将可运行对象发布到 UI 线程后调用 wait() 直到完成

java - Java Web 服务器上的服务器发送事件

c# - Asp.NET Web API "The operation was canceled."异常

java - 如何使用 spring boot 在 html 元标记中获取 csrf token

java - 字符串到字节数组的转换因windows和ubuntu而异

php - Backbone JS + Codeigniter Restful API 和 MYSQL 子字符串功能不起作用

java - Java EE 的 Spring Security 之类的框架?

spring-security - Spring Boot - Redis 持久化 token