Spring 5 Oauth2-如何在我的资源服务器中提供检查令牌URL?

时间:2020-09-21 20:17:41

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

我需要一些帮助。

我使用Spring-security-oauth2中的AuthorizationServer为授予类型“ client_credentials”设置了@EnableAuthorizationServer。能够创建,检查令牌以及与此相关的一切。

/ oauth / token
/ oauth / checkToken

关注this sample for Authorization server

我有一个单独的项目,该项目具有要保护的REST API。我不能使用@EnableResourceServer,因为在Weblogic上进行部署时,该项目使用Spring 5.2.8,而spring-security-oauth2 2.5导致冲突(因为它使用4.x Spring jars并排除它们引起了更多问题) ,所以I am using this sample

现在在此示例中,如何仅提供Checktoken网址。此示例需要JWT json类型的文件,但我没有。我只是想保持简单,并使用我创建的授权服务器的Checktoken网址,类似于@EnableResourceServer的工作方式。(like provided here,除了@EnableResourceServer以外)

我在哪里提供?任何即时帮助都表示赞赏。

2 个答案:

答案 0 :(得分:1)

按照您的ResourceServer示例,这对我有用:

library(dplyr)
library(tidyr)

df %>%
  rename_all(stringr::str_replace, "x?([a-z_]*)(\\d{2})([a-z_]*)", "\\1\\3\\2") %>%
  pivot_longer(-id, names_to = c(".value", "year"), names_pattern = "([a-z_]+)(\\d{2})")
#> # A tibble: 9 x 5
#>      id year  val_a val_b   age
#>   <dbl> <chr> <dbl> <dbl> <dbl>
#> 1     1 02        0     0     1
#> 2     1 03        1     1     2
#> 3     1 04        0     0     3
#> 4     2 02        1     2     2

我使用了以下Spring安全性依赖项:

@EnableWebSecurity
public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Value("${security.oauth2.resource.tokenInfoUri}") String tokenInfoUri;
    @Value("${security.oauth2.client.clientId}") String clientId;
    @Value("${security.oauth2.client.clientSecret}") String clientSecret;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
                .authorizeRequests((authorizeRequests) ->
                        authorizeRequests
                                .antMatchers(HttpMethod.GET, "/message/**").hasAuthority("SCOPE_message:read")
                                .antMatchers(HttpMethod.POST, "/message/**").hasAuthority("SCOPE_message:write")
                                .anyRequest().authenticated()
                )
                .oauth2ResourceServer(OAuth2ResourceServerConfigurer::opaqueToken);
        // @formatter:on
    }

    @Bean
    OpaqueTokenIntrospector opaqueTokenIntrospector() {
        return new NimbusOpaqueTokenIntrospector(tokenInfoUri,clientId,clientSecret);
    }
}

将您的checkToken-Uri,client和clientSecret放入application.properties。

答案 1 :(得分:0)

我最终使用了Spring提供的JWT示例,该示例获取JWT公共密钥以在资源服务器上进行验证。 请遵循Spring源示例项目中提供的auth和资源服务器。

到目前为止效果很好,直到我们迁移到更好的IDM解决方案为止