hasAuthority 方法仅适用于 POST 和 PUT httpmethods

hasAuthority method for only POST and PUT httpmethods

这是我的配置代码片段

@Override
    public void configure(HttpSecurity http) throws Exception {
        http.
        authorizeRequests()
        .antMatchers("/").permitAll()
        .antMatchers("/api/user/**").hasAnyAuthority("ROLE_ADMIN")
        .antMatchers("/api/status/**").hasAuthority("ROLE_ADMIN").anyRequest()
        .authenticated()
        .and()
        .exceptionHandling()
        .accessDeniedHandler(new OAuth2AccessDeniedHandler());
        }

在此我需要让 ROLE_ADMIN 仅访问 POSTPUT http 方法。他应该无法访问 GETDELETE http 方法。我需要在单个 .antMatchers() 方法中完成此操作。 我该怎么做?

看看这个 Spring example project。您可以为每个路径 HTTP 动词定义匹配器。

http
    .authorizeRequests()
        .antMatchers(HttpMethod.POST, "/employees").hasRole("ADMIN")
        .antMatchers(HttpMethod.PUT, "/employees/**").hasRole("ADMIN")
        .antMatchers(HttpMethod.PATCH, "/employees/**").hasRole("ADMIN")