Spring Boot项目的控制器貌似只能get不能post问题

我这2天新建了一个Spring Boot项目测试,在控制器上写了两个接口,一个是支持Get方式访问,另一个支持Post方式访问,发现Get可以,而Post不行。前端Post后,报403,找不到这个方法。

一、原因

原因是spring boot的防csrf(跨站点请求伪造)机制导致。

当我们新建一个Spring Boot项目的时候,如果有添加了 spring-boot-starter-security 依赖,系统就会默认启用基本认证来保护所有端点,这是为了增加应用的安全性。如果没有配置显式的安全规则,Spring Security 将会要求进行身份验证。表现在前端,就是访问这个接口的时候,会弹出一个窗口让我们输入账号密码,而且这个窗口还不是我们自己做的。通常我们会在项目里添加一个配置类,然后里面加一些白名单。比如获取验证码、登录这些接口应设置为免身份认证。

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/captcha","/verify").permitAll()
                .anyRequest().authenticated()
                .and().httpBasic();  // 使用 HTTP 基本认证
    }
}

但这段代码只解决了"/captcha","/verify"被允许无须身份验证就能访问的问题,但没有解决get可以访问,而post不能访问的问题。也许 Spring Security 认为,GET 请求相对于 POST 请求的行为通常会有所不同,所以对GET比较宽容,而POST就较为严格。这涉及到对CSRF的理解。

二、解决

解决之道就是在上面的Security配置中禁止对CSRF防范的限制,加上http.csrf().disable():

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
                .antMatchers("/slideCaptcha","/slideVerify")
                //.antMatchers("/**")
                .permitAll()
                .anyRequest().authenticated()
                .and().httpBasic();  // 使用 HTTP 基本认证
    }
}

三、小结

去除防CSRF限制,好像有点不安全。但事实上,通常post方式访问的接口,都需要进行身份验证。身份验证的安全性要高于CSRF防范。另外,现在微服务架构大行其道,前后端分离,二者地址、端口都不一样。前端向后端请求、提交的地址,几乎都经过nginx转发。也就是说,跨域请求是常态,防范没有太多意义。

相关拙作:
防御CSRF问题
WEB项目的安全性注意事项

相关推荐

  1. Spring Boot项目控制器貌似只能getpost问题

    2024-07-14 12:22:03       33 阅读
  2. GET正常,POST获取到数据

    2024-07-14 12:22:03       20 阅读
  3. springboot注解及GETPOST接口写法

    2024-07-14 12:22:03       35 阅读
  4. getpost区别!

    2024-07-14 12:22:03       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 12:22:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 12:22:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 12:22:03       58 阅读
  4. Python语言-面向对象

    2024-07-14 12:22:03       69 阅读

热门阅读

  1. Django是干什么的?好用么?

    2024-07-14 12:22:03       25 阅读
  2. HTTPS 加密流程全解析

    2024-07-14 12:22:03       27 阅读
  3. mysql快速精通(四)多表查询

    2024-07-14 12:22:03       24 阅读
  4. 如何隐藏 Ubuntu 顶部状态栏

    2024-07-14 12:22:03       31 阅读
  5. springboot2——功能和原理

    2024-07-14 12:22:03       22 阅读
  6. Oracle 数据清理

    2024-07-14 12:22:03       22 阅读
  7. win32:第一个窗口程序-初始化实例(part.5)

    2024-07-14 12:22:03       26 阅读
  8. Flask与Celery实现Python调度服务

    2024-07-14 12:22:03       22 阅读
  9. `speech_recognition` 是一个流行的库

    2024-07-14 12:22:03       21 阅读
  10. 致十年后的自己

    2024-07-14 12:22:03       14 阅读
  11. 25秋招面试算法题 (Go版本)

    2024-07-14 12:22:03       25 阅读
  12. yii2 AssetBundle使用

    2024-07-14 12:22:03       22 阅读