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

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

一、原因

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

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

java 复制代码
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():

java 复制代码
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项目的安全性注意事项

相关推荐
辰海Coding11 小时前
MiniSpring框架学习-完成的 IoC 容器
java·spring boot·学习·架构
Maiko Star16 小时前
* SpringBoot整合LangChain4j
java·spring boot·后端·langchain4j
绝知此事17 小时前
【产品更名】通义灵码升级为 Qoder CN:AI 编码助手新时代,附大模型收费与 Spring Boot 支持全对比
人工智能·spring boot·后端·idea·ai编程
linmoo198617 小时前
Agent应用实践之四 - 基础:AgentScope-SpringBoot集成源码解析
人工智能·spring boot·agent·agentscope·openclaw
海兰18 小时前
【第21篇-续】graph-Stream-Node改造为适配openAI模型示例
java·人工智能·spring boot·spring·spring ai
Albert Edison18 小时前
基于 SpringBoot + RabbitMQ 完成企业级应用通信
spring boot·rabbitmq·java-rabbitmq
happymaker062620 小时前
Spring学习日记——DAY03(yml文件)
java·spring boot·spring
hikktn21 小时前
企业级Spring Boot应用管理:从零打造生产级启动脚本
java·spring boot·后端
霸道流氓气质21 小时前
Spring Boot + MyBatis-Plus 实现异常隔离的 Upsert 数据落库(含远程调用数据补全)
spring boot·后端·mybatis
不懂的浪漫21 小时前
01|从 Spring Boot 项目理解 RAG:ingest、query、rerank、trace 到 eval
java·人工智能·spring boot·后端·ai·rag