OAuth 2.0 資源伺服器不透明令牌
內省的最少依賴項
如 JWT 的最少依賴項 中所述,Resource Server 的大部分支援集中在 spring-security-oauth2-resource-server
中。然而,除非提供了自定義的 OpaqueTokenIntrospector
,Resource Server 將回退到 NimbusOpaqueTokenIntrospector。這意味著要使支援不透明 Bearer 令牌的 Resource Server 正常工作,spring-security-oauth2-resource-server
和 oauth2-oidc-sdk
都是必需的。請參考 spring-security-oauth2-resource-server
來確定 oauth2-oidc-sdk
的正確版本。
內省的最少配置
通常,不透明令牌可以透過授權伺服器託管的 OAuth 2.0 內省端點 進行驗證。這在需要撤銷時非常有用。
使用 Spring Boot 時,將應用程式配置為使用內省的資源伺服器包含兩個基本步驟。首先,包含所需的依賴項;其次,指定內省端點詳情。
指定授權伺服器
要指定內省端點的位置,只需執行
spring:
security:
oauth2:
resourceserver:
opaque-token:
introspection-uri: https://idp.example.com/introspect
client-id: client
client-secret: secret
其中 idp.example.com/introspect 是由您的授權伺服器託管的內省端點,而 client-id
和 client-secret
是訪問該端點所需的憑據。
資源伺服器將使用這些屬性進行進一步的自配置,然後驗證傳入的 JWT。
使用內省時,授權伺服器的響應即為最終判定。如果授權伺服器響應令牌有效,那麼它就是有效的。 |
就這樣!
執行時預期行為
應用程式啟動後,資源伺服器將嘗試處理包含 Authorization: Bearer
請求頭的任何請求
GET / HTTP/1.1
Authorization: Bearer some-token-value # Resource Server will process this
只要指定了此方案,資源伺服器將嘗試根據 Bearer Token 規範處理請求。
給定一個不透明令牌,資源伺服器將執行以下操作:
-
使用提供的憑據和令牌查詢指定的內省端點
-
檢查響應中是否存在
{ 'active' : true }
屬性 -
將每個 scope 對映到帶有字首
SCOPE_
的許可權
預設情況下,生成的 Authentication#getPrincipal
是一個 Spring Security OAuth2AuthenticatedPrincipal
物件,而 Authentication#getName
對映到令牌的 sub
屬性(如果存在)。
接下來,您可能想跳轉到:
不透明令牌認證工作原理
接下來,讓我們看看 Spring Security 用於在基於 Servlet 的應用程式中支援不透明令牌認證的架構元件,就像我們剛剛看到的示例一樣。
OpaqueTokenAuthenticationProvider
是一個 AuthenticationProvider
實現,它利用 OpaqueTokenIntrospector
來認證不透明令牌。
讓我們看看 OpaqueTokenAuthenticationProvider
在 Spring Security 中如何工作。該圖解釋了來自 讀取 Bearer 令牌 圖中 AuthenticationManager
的工作細節。

OpaqueTokenAuthenticationProvider
的使用 來自 讀取 Bearer 令牌 的認證
Filter
將一個 BearerTokenAuthenticationToken
傳遞給由 ProviderManager
實現的 AuthenticationManager
。
ProviderManager
配置為使用型別為 OpaqueTokenAuthenticationProvider
的 AuthenticationProvider。
OpaqueTokenAuthenticationProvider
使用 OpaqueTokenIntrospector
內省不透明令牌並新增授予的許可權。當認證成功時,返回的 Authentication
型別為 BearerTokenAuthentication
,其 Principal 是配置的 OpaqueTokenIntrospector
返回的 OAuth2AuthenticatedPrincipal
。最終,返回的 BearerTokenAuthentication
將由認證 Filter
設定到 SecurityContextHolder
中。
認證後查詢屬性
令牌認證成功後,一個 BearerTokenAuthentication
例項將設定到 SecurityContext
中。
這意味著在您的配置中使用 @EnableWebMvc
時,它在 @Controller
方法中可用
-
Java
-
Kotlin
@GetMapping("/foo")
public String foo(BearerTokenAuthentication authentication) {
return authentication.getTokenAttributes().get("sub") + " is the subject";
}
@GetMapping("/foo")
fun foo(authentication: BearerTokenAuthentication): String {
return authentication.tokenAttributes["sub"].toString() + " is the subject"
}
由於 BearerTokenAuthentication
包含一個 OAuth2AuthenticatedPrincipal
,這也意味著它對 controller 方法也可用
-
Java
-
Kotlin
@GetMapping("/foo")
public String foo(@AuthenticationPrincipal OAuth2AuthenticatedPrincipal principal) {
return principal.getAttribute("sub") + " is the subject";
}
@GetMapping("/foo")
fun foo(@AuthenticationPrincipal principal: OAuth2AuthenticatedPrincipal): String {
return principal.getAttribute<Any>("sub").toString() + " is the subject"
}
透過 SpEL 查詢屬性
當然,這也意味著屬性可以透過 SpEL 訪問。
例如,如果使用 @EnableGlobalMethodSecurity
以便您可以使用 @PreAuthorize
註解,您可以這樣做
-
Java
-
Kotlin
@PreAuthorize("principal?.attributes['sub'] == 'foo'")
public String forFoosEyesOnly() {
return "foo";
}
@PreAuthorize("principal?.attributes['sub'] == 'foo'")
fun forFoosEyesOnly(): String {
return "foo"
}
覆蓋或替換 Boot 自動配置
Spring Boot 代表資源伺服器生成兩個 @Bean
。
第一個是配置應用程式作為資源伺服器的 SecurityFilterChain
。使用不透明令牌時,此 SecurityFilterChain
看起來像
-
Java
-
Kotlin
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.opaqueToken(Customizer.withDefaults())
);
return http.build();
}
@Bean
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeRequests {
authorize(anyRequest, authenticated)
}
oauth2ResourceServer {
opaqueToken { }
}
}
return http.build()
}
如果應用程式沒有暴露 SecurityFilterChain
bean,那麼 Spring Boot 將暴露上面預設的那個。
替換它就像在應用程式中暴露該 bean 一樣簡單
-
Java
-
Kotlin
import static org.springframework.security.oauth2.core.authorization.OAuth2AuthorizationManagers.hasScope;
@Configuration
@EnableWebSecurity
public class MyCustomSecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/messages/**").access(hasScope("message:read"))
.anyRequest().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.opaqueToken(opaqueToken -> opaqueToken
.introspector(myIntrospector())
)
);
return http.build();
}
}
import org.springframework.security.oauth2.core.authorization.OAuth2AuthorizationManagers.hasScope;
@Configuration
@EnableWebSecurity
class MyCustomSecurityConfiguration {
@Bean
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeRequests {
authorize("/messages/**", hasScope("SCOPE_message:read"))
authorize(anyRequest, authenticated)
}
oauth2ResourceServer {
opaqueToken {
introspector = myIntrospector()
}
}
}
return http.build()
}
}
上述配置要求所有以 /messages/
開頭的 URL 具有 message:read
範圍。
oauth2ResourceServer
DSL 上的方法也將覆蓋或替換自動配置。
例如,Spring Boot 建立的第二個 @Bean
是一個 OpaqueTokenIntrospector
,它將 String
令牌解碼為經過驗證的 OAuth2AuthenticatedPrincipal
例項
-
Java
-
Kotlin
@Bean
public OpaqueTokenIntrospector introspector() {
return new NimbusOpaqueTokenIntrospector(introspectionUri, clientId, clientSecret);
}
@Bean
fun introspector(): OpaqueTokenIntrospector {
return NimbusOpaqueTokenIntrospector(introspectionUri, clientId, clientSecret)
}
如果應用程式沒有暴露 OpaqueTokenIntrospector
bean,那麼 Spring Boot 將暴露上面預設的那個。
並且其配置可以使用 introspectionUri()
和 introspectionClientCredentials()
方法覆蓋,或使用 introspector()
方法替換。
如果應用程式沒有暴露 OpaqueTokenAuthenticationConverter
bean,那麼 spring-security 將構建 BearerTokenAuthentication
。
或者,如果您根本不使用 Spring Boot,那麼所有這些元件 - 過濾鏈、一個 OpaqueTokenIntrospector
和一個 OpaqueTokenAuthenticationConverter
- 都可以在 XML 中指定。
過濾鏈如下指定
-
XML
<http>
<intercept-uri pattern="/**" access="authenticated"/>
<oauth2-resource-server>
<opaque-token introspector-ref="opaqueTokenIntrospector"
authentication-converter-ref="opaqueTokenAuthenticationConverter"/>
</oauth2-resource-server>
</http>
而 OpaqueTokenIntrospector
如下指定
-
XML
<bean id="opaqueTokenIntrospector"
class="org.springframework.security.oauth2.server.resource.introspection.NimbusOpaqueTokenIntrospector">
<constructor-arg value="${spring.security.oauth2.resourceserver.opaquetoken.introspection_uri}"/>
<constructor-arg value="${spring.security.oauth2.resourceserver.opaquetoken.client_id}"/>
<constructor-arg value="${spring.security.oauth2.resourceserver.opaquetoken.client_secret}"/>
</bean>
而 OpaqueTokenAuthenticationConverter
如下指定
-
XML
<bean id="opaqueTokenAuthenticationConverter"
class="com.example.CustomOpaqueTokenAuthenticationConverter"/>
使用 introspectionUri()
授權伺服器的內省 Uri 可以配置為一個配置屬性,也可以在 DSL 中提供
-
Java
-
Kotlin
-
XML
@Configuration
@EnableWebSecurity
public class DirectlyConfiguredIntrospectionUri {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.opaqueToken(opaqueToken -> opaqueToken
.introspectionUri("https://idp.example.com/introspect")
.introspectionClientCredentials("client", "secret")
)
);
return http.build();
}
}
@Configuration
@EnableWebSecurity
class DirectlyConfiguredIntrospectionUri {
@Bean
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeRequests {
authorize(anyRequest, authenticated)
}
oauth2ResourceServer {
opaqueToken {
introspectionUri = "https://idp.example.com/introspect"
introspectionClientCredentials("client", "secret")
}
}
}
return http.build()
}
}
<bean id="opaqueTokenIntrospector"
class="org.springframework.security.oauth2.server.resource.introspection.NimbusOpaqueTokenIntrospector">
<constructor-arg value="https://idp.example.com/introspect"/>
<constructor-arg value="client"/>
<constructor-arg value="secret"/>
</bean>
使用 introspectionUri()
優先於任何配置屬性。
使用 introspector()
比 introspectionUri()
更強大的是 introspector()
方法,它將完全替換 Boot 關於 OpaqueTokenIntrospector
的任何自動配置。
-
Java
-
Kotlin
-
XML
@Configuration
@EnableWebSecurity
public class DirectlyConfiguredIntrospector {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.opaqueToken(opaqueToken -> opaqueToken
.introspector(myCustomIntrospector())
)
);
return http.build();
}
}
@Configuration
@EnableWebSecurity
class DirectlyConfiguredIntrospector {
@Bean
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeRequests {
authorize(anyRequest, authenticated)
}
oauth2ResourceServer {
opaqueToken {
introspector = myCustomIntrospector()
}
}
}
return http.build()
}
}
<http>
<intercept-uri pattern="/**" access="authenticated"/>
<oauth2-resource-server>
<opaque-token introspector-ref="myCustomIntrospector"/>
</oauth2-resource-server>
</http>
暴露一個 OpaqueTokenIntrospector
的 @Bean
或者,暴露一個 OpaqueTokenIntrospector
的 @Bean
與使用 introspector()
方法具有相同的效果。
@Bean
public OpaqueTokenIntrospector introspector() {
return new NimbusOpaqueTokenIntrospector(introspectionUri, clientId, clientSecret);
}
配置授權
OAuth 2.0 內省端點通常會返回一個 scope
屬性,指示授予的範圍(或許可權),例如
{ …, "scope" : "messages contacts"}
在這種情況下,資源伺服器將嘗試將這些範圍強制轉換為已授予許可權的列表,併為每個範圍新增字首字串 "SCOPE_"。
這意味著要使用從不透明令牌派生的範圍來保護端點或方法,相應的表示式應包含此字首
-
Java
-
Kotlin
-
XML
import static org.springframework.security.oauth2.core.authorization.OAuth2AuthorizationManagers.hasScope;
@Configuration
@EnableWebSecurity
public class MappedAuthorities {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorizeRequests -> authorizeRequests
.requestMatchers("/contacts/**").access(hasScope("contacts"))
.requestMatchers("/messages/**").access(hasScope("messages"))
.anyRequest().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.opaqueToken(Customizer.withDefaults())
);
return http.build();
}
}
import org.springframework.security.oauth2.core.authorization.OAuth2AuthorizationManagers.hasScope
@Configuration
@EnableWebSecurity
class MappedAuthorities {
@Bean
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeRequests {
authorize("/contacts/**", hasScope("contacts"))
authorize("/messages/**", hasScope("messages"))
authorize(anyRequest, authenticated)
}
oauth2ResourceServer {
opaqueToken { }
}
}
return http.build()
}
}
<http>
<intercept-uri pattern="/contacts/**" access="hasAuthority('SCOPE_contacts')"/>
<intercept-uri pattern="/messages/**" access="hasAuthority('SCOPE_messages')"/>
<oauth2-resource-server>
<opaque-token introspector-ref="opaqueTokenIntrospector"/>
</oauth2-resource-server>
</http>
或者類似地應用於方法安全
-
Java
-
Kotlin
@PreAuthorize("hasAuthority('SCOPE_messages')")
public List<Message> getMessages(...) {}
@PreAuthorize("hasAuthority('SCOPE_messages')")
fun getMessages(): List<Message?> {}
手動提取許可權
預設情況下,不透明令牌支援將從內省響應中提取 scope claim 並將其解析為單獨的 GrantedAuthority
例項。
例如,如果內省響應是
{
"active" : true,
"scope" : "message:read message:write"
}
那麼資源伺服器將生成一個包含兩個許可權的 Authentication
,一個用於 message:read
,另一個用於 message:write
。
當然,這可以使用自定義的 OpaqueTokenIntrospector
進行定製,該內省器會檢視屬性集並以自己的方式進行轉換。
-
Java
-
Kotlin
public class CustomAuthoritiesOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
private OpaqueTokenIntrospector delegate =
new NimbusOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret");
public OAuth2AuthenticatedPrincipal introspect(String token) {
OAuth2AuthenticatedPrincipal principal = this.delegate.introspect(token);
return new DefaultOAuth2AuthenticatedPrincipal(
principal.getName(), principal.getAttributes(), extractAuthorities(principal));
}
private Collection<GrantedAuthority> extractAuthorities(OAuth2AuthenticatedPrincipal principal) {
List<String> scopes = principal.getAttribute(OAuth2IntrospectionClaimNames.SCOPE);
return scopes.stream()
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
}
}
class CustomAuthoritiesOpaqueTokenIntrospector : OpaqueTokenIntrospector {
private val delegate: OpaqueTokenIntrospector = NimbusOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret")
override fun introspect(token: String): OAuth2AuthenticatedPrincipal {
val principal: OAuth2AuthenticatedPrincipal = delegate.introspect(token)
return DefaultOAuth2AuthenticatedPrincipal(
principal.name, principal.attributes, extractAuthorities(principal))
}
private fun extractAuthorities(principal: OAuth2AuthenticatedPrincipal): Collection<GrantedAuthority> {
val scopes: List<String> = principal.getAttribute(OAuth2IntrospectionClaimNames.SCOPE)
return scopes
.map { SimpleGrantedAuthority(it) }
}
}
之後,只需將其作為 @Bean
暴露即可配置此自定義內省器。
-
Java
-
Kotlin
@Bean
public OpaqueTokenIntrospector introspector() {
return new CustomAuthoritiesOpaqueTokenIntrospector();
}
@Bean
fun introspector(): OpaqueTokenIntrospector {
return CustomAuthoritiesOpaqueTokenIntrospector()
}
配置超時
預設情況下,資源伺服器在與授權伺服器協調時,連線超時和 socket 超時都設定為 30 秒。
在某些場景下,這可能太短了。此外,它沒有考慮更復雜的模式,如回退和發現。
為了調整資源伺服器連線授權伺服器的方式,NimbusOpaqueTokenIntrospector
接受一個 RestOperations
例項。
-
Java
-
Kotlin
@Bean
public OpaqueTokenIntrospector introspector(RestTemplateBuilder builder, OAuth2ResourceServerProperties properties) {
RestOperations rest = builder
.basicAuthentication(properties.getOpaquetoken().getClientId(), properties.getOpaquetoken().getClientSecret())
.setConnectTimeout(Duration.ofSeconds(60))
.setReadTimeout(Duration.ofSeconds(60))
.build();
return new NimbusOpaqueTokenIntrospector(introspectionUri, rest);
}
@Bean
fun introspector(builder: RestTemplateBuilder, properties: OAuth2ResourceServerProperties): OpaqueTokenIntrospector? {
val rest: RestOperations = builder
.basicAuthentication(properties.opaquetoken.clientId, properties.opaquetoken.clientSecret)
.setConnectTimeout(Duration.ofSeconds(60))
.setReadTimeout(Duration.ofSeconds(60))
.build()
return NimbusOpaqueTokenIntrospector(introspectionUri, rest)
}
將內省用於 JWT
一個常見問題是內省是否與 JWT 相容。Spring Security 的不透明令牌支援被設計成不關心令牌的格式——它會很樂意將任何令牌傳遞給提供的內省端點。
所以,假設您有一個需求,要求您在每次請求時都向授權伺服器檢查,以防 JWT 已被撤銷。
即使您使用 JWT 格式的令牌,您的驗證方法也是內省,這意味著您希望這樣做:
spring:
security:
oauth2:
resourceserver:
opaque-token:
introspection-uri: https://idp.example.org/introspection
client-id: client
client-secret: secret
在這種情況下,生成的 Authentication
將是 BearerTokenAuthentication
。相應的 OAuth2AuthenticatedPrincipal
中的任何屬性都將是內省端點返回的內容。
但是,假設,奇怪的是,內省端點只返回令牌是否啟用。現在該怎麼辦?
在這種情況下,您可以建立一個自定義的 OpaqueTokenIntrospector
,它仍然會訪問端點,但隨後會更新返回的 principal,使其包含 JWT 的 claims 作為屬性。
-
Java
-
Kotlin
public class JwtOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
private OpaqueTokenIntrospector delegate =
new NimbusOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret");
private JwtDecoder jwtDecoder = new NimbusJwtDecoder(new ParseOnlyJWTProcessor());
public OAuth2AuthenticatedPrincipal introspect(String token) {
OAuth2AuthenticatedPrincipal principal = this.delegate.introspect(token);
try {
Jwt jwt = this.jwtDecoder.decode(token);
return new DefaultOAuth2AuthenticatedPrincipal(jwt.getClaims(), NO_AUTHORITIES);
} catch (JwtException ex) {
throw new OAuth2IntrospectionException(ex);
}
}
private static class ParseOnlyJWTProcessor extends DefaultJWTProcessor<SecurityContext> {
JWTClaimsSet process(SignedJWT jwt, SecurityContext context)
throws JOSEException {
return jwt.getJWTClaimsSet();
}
}
}
class JwtOpaqueTokenIntrospector : OpaqueTokenIntrospector {
private val delegate: OpaqueTokenIntrospector = NimbusOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret")
private val jwtDecoder: JwtDecoder = NimbusJwtDecoder(ParseOnlyJWTProcessor())
override fun introspect(token: String): OAuth2AuthenticatedPrincipal {
val principal = delegate.introspect(token)
return try {
val jwt: Jwt = jwtDecoder.decode(token)
DefaultOAuth2AuthenticatedPrincipal(jwt.claims, NO_AUTHORITIES)
} catch (ex: JwtException) {
throw OAuth2IntrospectionException(ex.message)
}
}
private class ParseOnlyJWTProcessor : DefaultJWTProcessor<SecurityContext>() {
override fun process(jwt: SignedJWT, context: SecurityContext): JWTClaimsSet {
return jwt.jwtClaimsSet
}
}
}
之後,只需將其作為 @Bean
暴露即可配置此自定義內省器。
-
Java
-
Kotlin
@Bean
public OpaqueTokenIntrospector introspector() {
return new JwtOpaqueTokenIntrospector();
}
@Bean
fun introspector(): OpaqueTokenIntrospector {
return JwtOpaqueTokenIntrospector()
}
呼叫 /userinfo
端點
一般來說,資源伺服器不關心底層使用者,而是關心已授予的許可權。
話雖如此,有時將授權宣告與使用者關聯起來會很有價值。
如果應用程式也使用了 spring-security-oauth2-client
,並且設定了適當的 ClientRegistrationRepository
,那麼使用自定義的 OpaqueTokenIntrospector
來實現這一點非常簡單。下面的實現做了三件事:
-
委託給內省端點,以確認令牌的有效性
-
查詢與
/userinfo
端點關聯的適當客戶端註冊 -
呼叫
/userinfo
端點並返回響應
-
Java
-
Kotlin
public class UserInfoOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
private final OpaqueTokenIntrospector delegate =
new NimbusOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret");
private final OAuth2UserService oauth2UserService = new DefaultOAuth2UserService();
private final ClientRegistrationRepository repository;
// ... constructor
@Override
public OAuth2AuthenticatedPrincipal introspect(String token) {
OAuth2AuthenticatedPrincipal authorized = this.delegate.introspect(token);
Instant issuedAt = authorized.getAttribute(ISSUED_AT);
Instant expiresAt = authorized.getAttribute(EXPIRES_AT);
ClientRegistration clientRegistration = this.repository.findByRegistrationId("registration-id");
OAuth2AccessToken token = new OAuth2AccessToken(BEARER, token, issuedAt, expiresAt);
OAuth2UserRequest oauth2UserRequest = new OAuth2UserRequest(clientRegistration, token);
return this.oauth2UserService.loadUser(oauth2UserRequest);
}
}
class UserInfoOpaqueTokenIntrospector : OpaqueTokenIntrospector {
private val delegate: OpaqueTokenIntrospector = NimbusOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret")
private val oauth2UserService = DefaultOAuth2UserService()
private val repository: ClientRegistrationRepository? = null
// ... constructor
override fun introspect(token: String): OAuth2AuthenticatedPrincipal {
val authorized = delegate.introspect(token)
val issuedAt: Instant? = authorized.getAttribute(ISSUED_AT)
val expiresAt: Instant? = authorized.getAttribute(EXPIRES_AT)
val clientRegistration: ClientRegistration = repository!!.findByRegistrationId("registration-id")
val accessToken = OAuth2AccessToken(BEARER, token, issuedAt, expiresAt)
val oauth2UserRequest = OAuth2UserRequest(clientRegistration, accessToken)
return oauth2UserService.loadUser(oauth2UserRequest)
}
}
如果您不使用 spring-security-oauth2-client
,仍然非常簡單。您只需使用自己的 WebClient
例項呼叫 /userinfo
即可。
-
Java
-
Kotlin
public class UserInfoOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
private final OpaqueTokenIntrospector delegate =
new NimbusOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret");
private final WebClient rest = WebClient.create();
@Override
public OAuth2AuthenticatedPrincipal introspect(String token) {
OAuth2AuthenticatedPrincipal authorized = this.delegate.introspect(token);
return makeUserInfoRequest(authorized);
}
}
class UserInfoOpaqueTokenIntrospector : OpaqueTokenIntrospector {
private val delegate: OpaqueTokenIntrospector = NimbusOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret")
private val rest: WebClient = WebClient.create()
override fun introspect(token: String): OAuth2AuthenticatedPrincipal {
val authorized = delegate.introspect(token)
return makeUserInfoRequest(authorized)
}
}
無論哪種方式,建立 OpaqueTokenIntrospector
後,您都應該將其作為 @Bean
釋出以覆蓋預設配置。
-
Java
-
Kotlin
@Bean
OpaqueTokenIntrospector introspector() {
return new UserInfoOpaqueTokenIntrospector(...);
}
@Bean
fun introspector(): OpaqueTokenIntrospector {
return UserInfoOpaqueTokenIntrospector(...)
}