Spring Boot REST API - 按客户端类型(浏览器/非浏览器)启用/禁用 CSRF 保护?

发布于 2025-01-15 04:48:33 字数 146 浏览 3 评论 0 原文

我有一个 Spring Boot REST API。由于安全策略的原因,我需要为浏览器访问的端点启用 CSRF 保护。但是,非浏览器也可以访问此 API。有没有办法创建两组端点,一组只能在启用 CSRF 的情况下由浏览器访问,另一组只能在禁用 CSRF 的情况下由非浏览器访问?

I have a Spring Boot REST API. Due to a security policy I need to have CSRF protection enabled for endpoints accessed by the browser. However, this API will also be accessed by non-browsers. Is there a way I can create two sets of endpoints, one accessible by browsers only with CSRF enabled and the other accessible by non-browsers only with CSRF disabled?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏尔 2025-01-22 04:48:33

当您使用 DSL 配置 CSRF 保护时,例如 http.csrf()...,您可以通过传递 RequestMatcher 来判断您希望应用 CSRF 保护的请求>,就像这样:

http.csrf(csrf -> csrf.requireCsrfProtectionMatcher(new MyBrowserRequestMatcher()));

您的 RequestMatcher 实现可以验证 HttpServletRequest 是否包含标头 X-Requested-With: XMLHttpRequest 或检查用户代理

请记住,标头可以更改,并且您不能保证请求实际上来自浏览器或非浏览器应用程序。

When you configure your CSRF protection using the DSL, like this http.csrf()... you can tell which requests you want the CSRF protection to be applied by passing a RequestMatcher, like so:

http.csrf(csrf -> csrf.requireCsrfProtectionMatcher(new MyBrowserRequestMatcher()));

And your implementation of RequestMatcher could verify if the HttpServletRequest contains the header X-Requested-With: XMLHttpRequest or check the User-Agent.

Just keep in mind that the headers can be changed and you have no guarantee that the request actually come from a browser or non-browser app.

鸠书 2025-01-22 04:48:33

我认为您可以为浏览器请求和 API 请求使用单独的 URL 库。

例如,您可以在 /api/... 下和 SpringBootSecurityConfiguration 类中拥有非浏览器要查询的所有端点,并 configure( HttpSecurity http) 方法,如果模式匹配,您可以使用 http.csrf().disable(); 有条件地禁用 CSRF(可以找到很棒的教程 这里

编辑:这里是另一个答案可能有用。

I think you could have separate URL bases for the browser requests and API requests.

For example, you could have all the endpoints that are to be queried by non-browsers under /api/... and in your SpringBootSecurityConfiguration class and configure(HttpSecurity http) method you could conditionally disable CSRF with http.csrf().disable(); if the pattern matches (great tutorial can be found here)

Edit: here is another answer that might be useful.

桃扇骨 2025-01-22 04:48:33

正如 @ferrouskid 所说,我创建了两个 URL,一个用于浏览器,另一个用于非浏览器:

在 spring 安全配置中:

@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http.csrf().ignoringAntMatchers("/withoutCsrf/**")
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .and()
            .cors().disable();
//complete your configuration }

在控制器中:

@Controller
@RequestMapping({"books","withoutCsrf/books"})
public class BookController {}

As @ferrouskid said, I created two URL one for browsers and other for non-browsers:

In spring security config:

@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http.csrf().ignoringAntMatchers("/withoutCsrf/**")
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .and()
            .cors().disable();
//complete your configuration }

In controller:

@Controller
@RequestMapping({"books","withoutCsrf/books"})
public class BookController {}
我爱人 2025-01-22 04:48:33

由于依赖请求头可能无法准确识别请求来源,因此创建单独的服务 url,一个用于浏览器客户端,一个用于非浏览器客户端,并对非浏览器服务禁用 CSRF 保护也可以考虑作为一种解决方案。

创建 SecurityFilterChain 时可以禁用特定 URL 的 CSRF 保护。

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    httpSecurity.csrf((csrf) -> csrf.ignoringRequestMatchers("/trade/item/nxtitems", "/trade/item/**")).build();
}

Since relying on request headers may not accurately identify the source of requests, creating separate service urls, one for browser and one for non browser clients and disabling CSRF protection for non browser service can also be considered as a solution.

CSRF protection for specific URLs can be disabled while creating SecurityFilterChain.

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    httpSecurity.csrf((csrf) -> csrf.ignoringRequestMatchers("/trade/item/nxtitems", "/trade/item/**")).build();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文