简体   繁体   English

Spring 引导 REST API - 通过客户端类型(浏览器/非浏览器)启用/禁用 CSRF 保护?

[英]Spring Boot REST API - enable / disable CSRF protection by client type (browser / non-browser)?

I have a Spring Boot REST API.我有一个 Spring 启动 REST API。 Due to a security policy I need to have CSRF protection enabled for endpoints accessed by the browser.由于安全策略,我需要为浏览器访问的端点启用 CSRF 保护。 However, this API will also be accessed by non-browsers.但是,非浏览器也可以访问此 API。 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?有没有办法可以创建两组端点,一组只能在启用 CSRF 的情况下由浏览器访问,另一组只能在禁用 CSRF 的情况下由非浏览器访问?

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:当您使用 DSL 配置 CSRF 保护时,例如http.csrf()...您可以通过传递RequestMatcher来判断您希望应用 CSRF 保护的请求,如下所示:

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 .您的RequestMatcher实现可以验证HttpServletRequest是否包含 header X-Requested-With: XMLHttpRequest或检查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.请记住,标头可以更改,您无法保证请求实际上来自浏览器或非浏览器应用程序。

I think you could have separate URL bases for the browser requests and API requests.我认为您可以为浏览器请求和 API 请求提供单独的 URL 基础。

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();例如,您可以在/api/...下以及SpringBootSecurityConfiguration class 和configure(HttpSecurity http)方法中设置所有要由非浏览器查询的端点,您可以使用http.csrf().disable();有条件地禁用 CSRF http.csrf().disable(); if the pattern matches (great tutorial can be found here )如果模式匹配(可以在这里找到很棒的教程)

Edit: here is another answer that might be useful.编辑:这是另一个可能有用的答案

As @ferrouskid said, I created two URL one for browsers and other for non-browsers:正如@ferrouskid 所说,我创建了两个 URL 一个用于浏览器,另一个用于非浏览器:

In spring security config:在 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 }

In controller:在 controller 中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Security:按客户端类型启用/禁用 CSRF(浏览器/非浏览器) - Spring Security: enable / disable CSRF by client type (browser / non-browser ) Spring Boot在Rest Client中授权用户,但未在浏览器中授权用户(Rest授权) - Spring Boot Authorizes User In Rest Client, But Not In Browser (Rest Authorization) 如何在 Spring 引导数据 rest 中禁用 hal 浏览器 - How to disable hal browser in Spring Boot data rest 使用Spring Boot的Rest API不会通过angular向客户端返回数据:产生来自浏览器的404响应 - Rest api using Spring boot not returning data to client via angular: yields 404 response from browser 如何在spring boot中启用浏览器缓存 - How to enable browser caching in spring boot 我们可以通过Grid运行非浏览器功能吗 - Can we run Non-browser functionalities through Grid Cookie 未通过客户端浏览器中 httpserveletrresponse 中的 spring rest 重定向 api 在浏览器中设置 - Cookie not getting set in browser via spring rest redirect api in httpserveletrresponse in client browser 在从 openfeign 客户端(引导 2.2.4)到 Spring Data Rest API(引导 1.5.9)的调用中禁用特殊字符转义 - Disable special character escaping in call from openfeign client (boot 2.2.4) to Spring Data Rest API (boot 1.5.9) Spring OAuth2客户端,CSRF保护 - Spring OAuth2 client, CSRF protection 无法禁用csrf Spring启动 - Cannot disable csrf Spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM