【问题标题】:Spring Security configuration based on different http methods基于不同http方式的Spring Security配置
【发布时间】:2016-10-02 01:13:17
【问题描述】:

在我的 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 公开,即使对于匿名用户也是如此

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

【问题讨论】:

    标签: java spring spring-security spring-security-oauth2


    【解决方案1】:

    只需在匹配器中添加方法

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

    【讨论】:

      猜你喜欢
      • 2021-02-15
      • 2012-02-09
      • 2014-07-05
      • 2011-10-08
      • 2014-12-07
      • 2019-06-13
      相关资源
      最近更新 更多