【问题标题】:Spring OAuth 2: public access to a resourceSpring OAuth 2:对资源的公共访问
【发布时间】:2014-11-17 22:50:12
【问题描述】:

如何在 Spring Security OAuth-2 Rest 应用程序中允许特定 URL 中的公共访问。

我已将所有以 /rest/** 开头的 URL 设为安全,但希望将 /rest/about 公开,因此我不需要用户进行身份验证即可访问它。我尝试使用permitAll(),但它仍然需要请求中的令牌。这是我的HttpSecurity 配置:

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends
        ResourceServerConfigurerAdapter {

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId(RESOURCE_ID);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/rest/about").permitAll()
                .antMatchers("/rest/**").authenticated()
                ;
    }
}

/rest/about 的GET 请求仍然返回401 Unauthorized - "error":"unauthorized","error_description":"Full authentication is required to access this resource"

【问题讨论】:

    标签: spring rest spring-mvc spring-security oauth-2.0


    【解决方案1】:

    找到了答案。我只需要添加anonymous()

    public void configure(HttpSecurity http) throws Exception {
            http
                    .anonymous().and()
                    .authorizeRequests()
                    .antMatchers("/rest/about").permitAll()
                    .antMatchers("/rest/**").authenticated()
                    ;
        }
    

    https://stackoverflow.com/a/25280897/256245得到答案

    【讨论】:

      猜你喜欢
      • 2015-10-14
      • 2021-04-30
      • 1970-01-01
      • 2014-06-07
      • 2019-04-29
      • 2023-03-10
      • 2011-07-11
      • 2018-03-02
      • 1970-01-01
      相关资源
      最近更新 更多