当授权服务器也是资源服务器时如何配置oAuth2

How to configure oAuth2 when Authorization Server is also the Resource server

我正在尝试在 spring 引导 2.x.x 中使用 授权代码授予 隐式设置非常基本的 oAuth2 身份验证grant 但在获得令牌后我似乎无法访问资源服务器(与授权服务器驻留在相同的 spring 启动应用程序中)。

以下是WebSecurityConfigurerAdapter

的配置
@EnableWebSecurity
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    private static final String[] IGNORE_URIS = {
            "/swagger-resources/**",
            "/swagger-ui.html",
            "/v2/api-docs",
            "/webjars/**",
            "/resources/**",
            "/h2-console/**",
            "/common/**",
            "/configuration/ui",
            "/configuration/security",
            "/error"
    };

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers(IGNORE_URIS);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/product/**")
                .hasAnyRole("ADMIN").and()
                .httpBasic().and().formLogin().and().authorizeRequests().anyRequest().authenticated();

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("{noop}admin").roles("ADMIN");
    }

    @Bean
    public PasswordEncoder bCrypt() {
        return new BCryptPasswordEncoder();
    }

还有 AuthorizationServerConfigurerAdapter

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    private final AuthenticationManager authenticationManager;

    @Autowired
    public AuthorizationServerConfiguration(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        this.authenticationManager = authenticationConfiguration.getAuthenticationManager();
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient("my-client-id")
                .authorizedGrantTypes("authorization_code", "implicit")
                .authorities("ADMIN")
                .scopes("all")
                .resourceIds("product_api")
                .secret("{noop}secret").redirectUris("https://google.com").accessTokenValiditySeconds(0);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.tokenKeyAccess("permitAll()")
                .checkTokenAccess("permitAll()");
    }

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

到目前为止一切顺利。我可以通过在浏览器中输入以下 Url 来访问默认的 Spring 登录页面。

http://localhost:8080/oauth/authorize?response_type=token&client_id=my-client-id&redirect_uri=https://google.com

然后登录页面出现,我输入我的凭据。

登录后,我可以授予对 "my-client-id" 应用程序的访问权限。

最终在我批准该应用程序后,我可以在浏览器的 URL 栏中看到新生成的访问令牌,就像这样。

https://www.google.com/#access_token=f2153498-6a26-42c6-93f0-80825ef03b16&token_type=bearer&scope=all

我的问题是,当我还配置资源服务器时,所有这些流程都无法正常工作。

@EnableResourceServer
@Configuration
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

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


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

我做错了什么?当我像以前一样尝试访问 oauth/authorize url 时,我得到以下信息:

为什么?如何访问登录页面并检索令牌?我错过了什么?

您需要使用

@Order 

为 WebMvc 和 ResourceServer 指定顺序的注释 类

@EnableWebSecurity
@Configuration
@Order(1)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
...
}

和资源服务器

@EnableResourceServer
@Configuration
@Order(2)
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
...
}

如果您想查看可行的示例,可以在此处查看https://github.com/alex-petrov81/Whosebug-answers/tree/master/auth-server-also-resource 我是根据您的代码示例创建的。