Oauth 2.0 WebApp托管Oauth API调用集并控制访问

Oauth 2.0 WebApp托管Oauth API调用集并控制访问,oauth-2.0,spring-security-oauth2,Oauth 2.0,Spring Security Oauth2,我计划创建一个war文件(webapp),它将承载对不同后端系统的各种API调用。我将使用OAuth2控制通过客户端的访问。我计划向不同的应用程序所有者发布clientId/secrets,以控制对我的安全API的应用程序访问 是否可以将我的spring安全配置配置为 仅允许某些客户端ID访问特定的API和块 他们不使用别人吗? 例如,client123可以访问apiA的调用GET,但不能调用apiB的PUT 我曾想过使用示波器,但似乎 我的CAS OAuth服务器可能不支持客户端凭据的作用域

我计划创建一个war文件(webapp),它将承载对不同后端系统的各种API调用。我将使用OAuth2控制通过客户端的访问。我计划向不同的应用程序所有者发布clientId/secrets,以控制对我的安全API的应用程序访问

  • 是否可以将我的spring安全配置配置为 仅允许某些客户端ID访问特定的API和块 他们不使用别人吗? 例如,client123可以访问apiA的调用GET,但不能调用apiB的PUT
  • 我曾想过使用示波器,但似乎 我的CAS OAuth服务器可能不支持客户端凭据的作用域

  • 是的,Spring安全性支持多种模式,尽管您正确地认为作用域是首选方法

    我可能会建议您在Spring安全方面仍然使用作用域,即使该表示只存在于您的API中,这样,随着CAS的成熟,或者如果您将来能够切换到作用域,资源服务器将保持不变

    假设您有一个使用JWTs的Spring Security OAuth 2.0资源服务器:

    @Override
    protected void configure(HttpSecurity http) {
        http
            .oauth2ResourceServer()
                .jwt();
    }
    
    Jwt
    转换为
    grandedauthority
    s的
    集合的钩子是
    Converter
    。您的实现可能类似于:

    public class ByClientIdJwtGrantedAuthoritiesConverter
        implements Converter<Jwt, Collection<GrantedAuthority>> {
    
        @Override
        public Collection<GrantedAuthority> convert(Jwt jwt) {
            // manually infer scopes from client ids
            String clientId = jwt.getSubject();
            if ("abc".equals(clientId)) {
                return Arrays.asList(new SimpleGrantedAuthority("SCOPE_read"));
            }
            // ... etc.
        }
    }
    
    我做了一些假设,包括如何从令牌中派生客户机,但总体思路是这样的

    SpringSecurity5.2Gas将在几周内发布,并将支持不透明令牌,以防OAuth2 CA不支持JWTs。在这种情况下,您可以做类似的事情,只需使用
    opaqueToken()
    DSL即可

    此后,您可以按照与其他身份验证机制相同的方式通过路径配置Spring安全性:

    @Override
    protected void configure(HttpSecurity http) {
        http
            .authorizeRequests()
                .antMatchers("/apiA/**").hasAuthority("SCOPE_apiA")
                .antMatchers(HttpMethod.GET, "/apiB/**").hasAuthority("SCOPE_apiB:read")
                .and()
           .oauth2ResourceServer()
                .jwt()
                    .jwtAuthenticationConverter(...);
    }
    
    @Override
    protected void configure(HttpSecurity http) {
        http
            .authorizeRequests()
                .antMatchers("/apiA/**").hasAuthority("SCOPE_apiA")
                .antMatchers(HttpMethod.GET, "/apiB/**").hasAuthority("SCOPE_apiB:read")
                .and()
           .oauth2ResourceServer()
                .jwt()
                    .jwtAuthenticationConverter(...);
    }