Spring security Spring OAuth:具有授权服务器后端的资源服务器

Spring security Spring OAuth:具有授权服务器后端的资源服务器,spring-security,oauth-2.0,spring-security-oauth2,spring-cloud,Spring Security,Oauth 2.0,Spring Security Oauth2,Spring Cloud,我想开发两个独立的服务,一个用于业务人员,另一个用于使用SpringOAuth2的用户身份验证 让我们称之为业务服务和OAuth服务 现在,如果请求没有经过身份验证,我希望业务服务委托给OAuth服务。客户端应用程序(Android应用程序)事先不应该知道OAuth服务,它应该只由业务服务委托给它,对于未经身份验证的请求,使用302http重定向。确切地说,我希望我的API登录页提供指向的链接,当我的客户端应用程序决定遵循此链接时,它将被重定向到OAuth服务 如果我用注释业务服务,当我在没有访

我想开发两个独立的服务,一个用于业务人员,另一个用于使用SpringOAuth2的用户身份验证

让我们称之为业务服务和OAuth服务

现在,如果请求没有经过身份验证,我希望业务服务委托给OAuth服务。客户端应用程序(Android应用程序)事先不应该知道OAuth服务,它应该只由业务服务委托给它,对于未经身份验证的请求,使用302http重定向。确切地说,我希望我的API登录页提供指向的链接,当我的客户端应用程序决定遵循此链接时,它将被重定向到OAuth服务

如果我用注释业务服务,当我在没有访问令牌的情况下对其进行卷积时,它的所有资源都会受到保护,并返回401。到现在为止,一直都还不错。如果我提供如下访问令牌:

curl -v http://localhost:8667/resource/ -H "Authorization: Bearer $TOKEN"
我可以访问资源。还不错

但是,如果我用注释业务服务以启用到OAuth服务的重定向,它将失去使用访问令牌访问资源的能力(与上面相同),它只向登录页面返回302

如果我同时使用这两种注释,@enableAuth2Resource似乎总是“赢”,因为身份验证工作正常,但调用返回404


那么,创建一个资源服务器的正确方法是什么,它可以将未经身份验证的呼叫委托给身份验证服务器?

经过几个小时的尝试,我现在找到了一个解决方案

业务服务器(资源服务器)现在如下所示:

@SpringBootApplication
@EnableOAuth2Sso
@EnableOAuth2Resource
public class BusinessService {

    public static void main(final String[] args) {
        final ConfigurableApplicationContext context = SpringApplication.run(BusinessService.class, args);
    }

}
有两种配置,一种用于SSO:

@Configuration
public class OAuth2SsoConfiguration extends OAuth2SsoConfigurerAdapter {

    @Override
    public void match(final RequestMatchers matchers) {
        matchers.antMatchers("/");
    }

    @Override
    public void configure(final HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll();
    }

}
一个用于资源:

@Configuration
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(final HttpSecurity http) throws Exception {
        http.requestMatchers().antMatchers("/resource/**").and().authorizeRequests().anyRequest().authenticated().antMatchers("/").permitAll();

    }

}


这将导致以下结果:

curl -v http://localhost:8667/
返回

HTTP/1.1 200 OK
{"links":[{"rel":"login","href":"http://localhost:8667/login"}]}


返回

HTTP/1.1 401 Unauthorized
{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}
HTTP/1.1 302 Found
Location: http://localhost:8666/user/oauth/authorize?client_id=clientId&redirect_uri=http%3A%2F%2Flocalhost%3A8667%2Flogin&response_type=code&state=YmmNO9


返回

HTTP/1.1 401 Unauthorized
{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}
HTTP/1.1 302 Found
Location: http://localhost:8666/user/oauth/authorize?client_id=clientId&redirect_uri=http%3A%2F%2Flocalhost%3A8667%2Flogin&response_type=code&state=YmmNO9

因此,我的业务服务是安全的,作为一个资源服务器,所有业务资源都返回401。服务的根目录适用于所有客户端,因此它们可以发现登录关系,如果遵循此关系,它们将重定向到授权服务器

您能否分享在用户通过oauth2验证后如何在资源服务器中获取用户信息?@Kane,为时已晚,但可能会帮助其他人。您可以在端点中使用以下参数:@RequestHeader(value=“Authorization”)String authorizationHeader,Principal currentUser