【问题标题】:hasAuthority method for only POST and PUT httpmethodshasAuthority 方法仅适用于 POST 和 PUT httpmethods
【发布时间】:2017-11-08 07:14:01
【问题描述】:

这是我的配置代码sn-p

@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 httpmethods。他应该无法访问GETDELETE httpmethod。我需要在单个 .antMatchers() 方法中完成此操作。 我该怎么做?

【问题讨论】:

    标签: spring-mvc spring-boot spring-security spring-security-oauth2


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多