【问题标题】:Spring Oauth2 configure authorization endpointsSpring Oauth2 配置授权端点
【发布时间】:2018-11-13 21:03:15
【问题描述】:

我有使用 spring-security-oauth2:2.3.3 和 spring-boot-starter-security:2.0.2 构建的带有 REST WS 的 Spring Web 应用程序。但我无法设置受保护的端点。

资源服务器配置

@EnableResourceServer
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/", "/customers", "/v2/**").permitAll()
                .antMatchers("/api/**").authenticated();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationProvider authenticationProvider) {
        return new CustomAuthenticationManager(authenticationProvider);
    }

    @Bean
    public AuthenticationProvider authenticationProvider(UserRepository userRepository, PasswordEncoder passwordEncoder) {
        return new DBAuthenticationProvider(userRepository, passwordEncoder);
    }

}

AuthorizationServerConfig

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) {
        security.checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient("ClientId")
                .secret("secret")
                .authorizedGrantTypes("password")
                .scopes("read", "write");
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        endpoints.authenticationManager(authenticationManager);
    }
}

当我首先获得访问令牌时,一切正常,但我希望端点“/”、“/customers”、“/v2/**”在没有任何授权的情况下允许所有人使用。但是当我调用 'curl http://{{host}}/' 或 http://{{host}}/customers 时,我仍然得到 401:

{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}

添加 "--header "Authorization: Basic {{base64 client id:password}}" 也无济于事。这怎么可能?

编辑 根据spring security permitAll still considering token passed in Authorization header and returns 401 if token is invalid 通过将@Order(1) 添加到ResourceServerConfig 以覆盖默认OAuth2 解决。但这只有在添加“授权”标头时才会生效,这不是我的情况。那么这怎么可能呢?

【问题讨论】:

  • 使用 curl 调用时使用的是什么方法?你有一个映射到这个 url 的端点吗?您是否正在为您的客户生成令牌?
  • 我调用 'curl -X GET localhost:8080/customers',同时使用端口 8080 上的 tomcat 在本地部署应用程序。在应用程序启动期间正确映射端点(即 RequestMappingHandlerMapping : Mapped "{[/customers],methods =[GET]}" 到公共 java.lang.String com.david.demo.customer.CustomerViewController.showCustomersList(org.springframework.ui.Model))。
  • 我没有生成任何令牌。我希望上下文路径“/”、“/customers”、“/v2/**”是公开的!
  • 哇,添加@Order(1) 确实有帮助。但我不明白为什么。 Oauth2 应仅在调用允许的端点时添加标头“授权”时拦截。但是当我使用 curl 时,我很确定我不会添加它。不需要 AnonymousAuthFilter

标签: java spring oauth-2.0 spring-security-oauth2


【解决方案1】:

将此添加到您的项目中。

@Configuration
public class ResourceServer extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/user/**")
        .authorizeRequests().anyRequest().authenticated();
    }
}

如果您想向/user/** 发出任何请求,您需要在标头中将您的访问令牌作为Authorization: Bearer {access_token} 传递。所有其他端点都不会请求令牌。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-01
    • 2018-09-26
    • 2016-12-12
    • 2017-02-25
    • 2017-11-30
    • 2015-08-26
    • 2015-11-19
    • 1970-01-01
    相关资源
    最近更新 更多