hasAuthority 方法仅适用于 POST 和 PUT httpmethods

Posted

技术标签:

【中文标题】hasAuthority 方法仅适用于 POST 和 PUT httpmethods【英文标题】:hasAuthority method for only POST and 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() 方法中完成此操作。 我该怎么做?

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

以上是关于hasAuthority 方法仅适用于 POST 和 PUT httpmethods的主要内容,如果未能解决你的问题,请参考以下文章