Spring 引导 CSRF
Spring Boot CSRF
尝试在最新的 Spring Boot 上实施 CSRF 保护。
网上的例子都是基于用户登录和认证的,我不需要。
我的站点没有任何需要身份验证的部分。
我想要
1) Rest requests come from within site. No direct request from outside with wget to be allowed.
2) All pages (routes) must be requested from the index page (/)
在 pom.xml
中包含安全依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
-- 在 application.properties
中定义的用户(尽管我不需要)
-- 应用创建 _csrf.token
.
-- 创建 class 扩展 WebSecurityConfigurerAdapter
并使用 "configure" 方法覆盖。
尝试了 "configure" 中所有建议的过滤器。没用,最后留空了。
问题是Wget可以直接获取api页。
如何预防?
我已经快速整理了这个配置的 POC:
@Configuration
@EnableWebSecurity
@SpringBootApplication
public class WhosebugQ40929943Application extends WebSecurityConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(WhosebugQ40929943Application.class, args);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").permitAll();
}
}
它的要点是 Spring Boot + Security 将自动保护所有端点。在这里,我们明确允许对所有端点的请求。但是,Spring Boot + Security 会自动配置开箱即用的 CSRF,我们已将其启用。因此,您可以两全其美。
注意:您可能需要进一步优化此配置以满足您的需要。
尝试在最新的 Spring Boot 上实施 CSRF 保护。 网上的例子都是基于用户登录和认证的,我不需要。
我的站点没有任何需要身份验证的部分。 我想要
1) Rest requests come from within site. No direct request from outside with wget to be allowed.
2) All pages (routes) must be requested from the index page (/)
在 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
-- 在 application.properties
中定义的用户(尽管我不需要)
-- 应用创建 _csrf.token
.
-- 创建 class 扩展 WebSecurityConfigurerAdapter
并使用 "configure" 方法覆盖。
尝试了 "configure" 中所有建议的过滤器。没用,最后留空了。
问题是Wget可以直接获取api页。 如何预防?
我已经快速整理了这个配置的 POC:
@Configuration
@EnableWebSecurity
@SpringBootApplication
public class WhosebugQ40929943Application extends WebSecurityConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(WhosebugQ40929943Application.class, args);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").permitAll();
}
}
它的要点是 Spring Boot + Security 将自动保护所有端点。在这里,我们明确允许对所有端点的请求。但是,Spring Boot + Security 会自动配置开箱即用的 CSRF,我们已将其启用。因此,您可以两全其美。
注意:您可能需要进一步优化此配置以满足您的需要。