草庐IT

Spring OAuth 2 : public access to a resource

coder 2023-05-11 原文

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

我已保护所有以 /rest/** 开头的 URL,但希望将 /rest/about 公开,因此我不需要用户进行身份验证访问它。我尝试使用 permitAll() 但它仍然需要请求中的 token 。这是我的 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":"访问此资源需要完全身份验证"

最佳答案

找到了答案。我只需要添加 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 得到答案

关于 Spring OAuth 2 : public access to a resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26000040/

有关Spring OAuth 2 : public access to a resource的更多相关文章

随机推荐