springboot 3.2.5集成spring security 只放行get请求,其他请求403

环境配置

  • jdk 17
xml 复制代码
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-security</artifactId>
     </dependency>

解决办法

java 复制代码
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.csrf(AbstractHttpConfigurer::disable);// 关闭 crsf保护

		// 设置需要进行权限认证的请求
        http.authorizeRequests((authorizeRequests) ->
                        authorizeRequests
                                .requestMatchers("/**").hasRole("USER")
                ).formLogin(withDefaults());
        return http.build();
    }

验证

相关推荐
Wang's Blog2 分钟前
Java框架 SpringCloud 快速入门: 路由断言工厂
java·spring·spring cloud
ym hyd 11111 分钟前
门诊挂号系统源码 Java+SpringBoot+Vue3 前后分离
java·vue.js·spring boot·毕设
松就是我9029818 分钟前
如何设计多Agent
后端
高频因子挖掘机21 分钟前
盘中筛选要同时看价格和盘口?按需分层比“一次全拿”更好维护
后端·github·api
asong26 分钟前
从写代码到部署上线,Cloudflare 给 AI 配了一把新钥匙
前端·javascript·后端
RobinDevNotes27 分钟前
PagedAttention原理,KV缓存也能分页
人工智能·后端
卷无止境28 分钟前
当AI写代码遇见Rust为什么会卡壳
后端·python
Percy_kk31 分钟前
Django的CRUD映射
后端·django
用户EasyAdminBlazor33 分钟前
Blazor Server 性能优化实战:Circuit、数据库、Redis 与并发
后端
王中阳Go37 分钟前
Agent 第一句就 500,我们查了三轮:入口日志少打了一个参数
后端·agent·ai编程