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

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

在我的 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/

相关文章:

java - Java 嵌套子类实例化声明

java - key=value 的正则表达式是什么,以逗号分隔?

java - PreparedStatement 用于插入未知数量的未知类型的值

spring - CSRF token 在登录期间过期

authentication - Spring 安全: Bypass login form

java - 了解关于引用队列的虚引用与弱引用

java - 数据库连接未关闭

java - @ControllerAdvice 不适用于 SIBLING 包?

java - 通过Spring框架获取依赖图(逆图)

grails - grails spring安全性登录页面显示