gpt4 book ai didi

java - 基于不同http方法的Spring Security配置

转载 作者:行者123 更新时间:2023-12-01 09:26:18 25 4
gpt4 key购买 nike

在我的 Spring Boot 应用程序中,我配置了以下 OAuth2 ResourceServer:

@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.antMatcher("/api/**").authorizeRequests()
.antMatchers("/api/v1.0/users").permitAll()
.anyRequest().authenticated()
.and()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(STATELESS);
// @formatter:on
}

在我的 REST API UserController 中,我有两种不同的请求处理程序方法 - 用于 POST 和 GET http 方法。现在上面配置中的两个都是公开的。

我想保护 POST 方法的安全,并将 GET 设为公开,即使对于匿名用户也是如此

如何更改上述配置以支持此功能?

最佳答案

只需在匹配器中添加方法即可

antMatchers(HttpMethod.POST, "/api/v1.0/users")

关于java - 基于不同http方法的Spring Security配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39809333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com