Spring 基于不同http方式的安全配置

Spring Security configuration based on different http methods

在我的 Spring 引导应用程序中,我配置了以下 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 方法。现在他们在上面的配置中都是 public.

我想保护 POST 方法并使 GET 成为 public 即使对于匿名用户也是如此

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

在匹配器中添加方法即可

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