Spring 5 Oauth2 - 如何在我的资源服务器中提供检查令牌 URL?
Spring 5 Oauth2 - How to provide the check token URL in my Resource server?
我需要一些帮助..
我使用 Spring-security-oauth2 中的 @EnableAuthorizationServer
为授权类型“client_credentials”设置了一个 AuthorizationServer
。能够创建、检查令牌和一切都很好。
/oauth/token
/oauth/checkToken
已关注 this sample for Authorization server
我有一个单独的项目,其中包含要保护的 REST API。我不能使用 @EnableResourceServer
因为那个项目使用 Spring 5.2.8 而 spring-security-oauth2 2.5 导致冲突(因为它使用 4.x Spring jar 并排除它们会导致更多问题),同时通过 Weblogic 进行部署,因此 I am using this sample.
现在在这个示例中,我如何只提供一个 Checktoken url。此示例需要 JWT json 类型的文件,但我没有。我只是想保持简单并使用我创建的授权服务器的检查令牌 url,类似于 @EnableResourceServer
的工作方式。(like provided here 除了没有 @EnableResourceServer
)
我在哪里提供?感谢任何即时帮助。
按照您的 ResourceServer 示例,这对我有用:
@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);
}
}
我使用了以下 spring 安全依赖项:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
<version>5.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>oauth2-oidc-sdk</artifactId>
<version>8.22</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId>
<version>5.3.4.RELEASE</version>
</dependency>
将您的 checkToken-Uri、client 和 clientSecret 放入您的 application.properties。
我最终使用了 Spring 提供的 JWT 示例,它获取了 JWT public 密钥以在资源服务器上进行验证。
遵循 Spring 源示例项目中提供的身份验证和资源服务器。
在我们迁移到更好的 IDM 解决方案之前,目前运行良好
我需要一些帮助..
我使用 Spring-security-oauth2 中的 @EnableAuthorizationServer
为授权类型“client_credentials”设置了一个 AuthorizationServer
。能够创建、检查令牌和一切都很好。
/oauth/token
/oauth/checkToken
已关注 this sample for Authorization server
我有一个单独的项目,其中包含要保护的 REST API。我不能使用 @EnableResourceServer
因为那个项目使用 Spring 5.2.8 而 spring-security-oauth2 2.5 导致冲突(因为它使用 4.x Spring jar 并排除它们会导致更多问题),同时通过 Weblogic 进行部署,因此 I am using this sample.
现在在这个示例中,我如何只提供一个 Checktoken url。此示例需要 JWT json 类型的文件,但我没有。我只是想保持简单并使用我创建的授权服务器的检查令牌 url,类似于 @EnableResourceServer
的工作方式。(like provided here 除了没有 @EnableResourceServer
)
我在哪里提供?感谢任何即时帮助。
按照您的 ResourceServer 示例,这对我有用:
@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);
}
}
我使用了以下 spring 安全依赖项:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
<version>5.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>oauth2-oidc-sdk</artifactId>
<version>8.22</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId>
<version>5.3.4.RELEASE</version>
</dependency>
将您的 checkToken-Uri、client 和 clientSecret 放入您的 application.properties。
我最终使用了 Spring 提供的 JWT 示例,它获取了 JWT public 密钥以在资源服务器上进行验证。 遵循 Spring 源示例项目中提供的身份验证和资源服务器。
在我们迁移到更好的 IDM 解决方案之前,目前运行良好