OAuth 2.0 資源伺服器不透明令牌
內省的最小依賴性
如 JWT 的最小依賴性 中所述,大多數資源伺服器支援都收集在 spring-security-oauth2-resource-server
中。但是,除非您提供自訂的 ReactiveOpaqueTokenIntrospector
,否則資源伺服器會回退到 ReactiveOpaqueTokenIntrospector
。這表示 spring-security-oauth2-resource-server
和 oauth2-oidc-sdk
都是擁有支援不透明 Bearer 令牌的最小資源伺服器所必需的。請參閱 spring-security-oauth2-resource-server
以確定 oauth2-oidc-sdk
的正確版本。
內省的最小組態
通常,您可以使用授權伺服器託管的 OAuth 2.0 Introspection Endpoint 來驗證不透明令牌。當撤銷是一個需求時,這可能很方便。
當使用 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 令牌規範處理請求。
給定一個不透明令牌,資源伺服器會執行以下操作
-
使用提供的憑證和令牌查詢提供的內省端點。
-
檢查回應中是否有
{ 'active' : true }
屬性。 -
將每個 scope 對應到一個以
SCOPE_
為前綴的權限。
預設情況下,產生的 Authentication#getPrincipal
是一個 Spring Security OAuth2AuthenticatedPrincipal
物件,而 Authentication#getName
對應到令牌的 sub
屬性(如果存在)。
從這裡,您可能想跳到
驗證後查找屬性
一旦令牌通過身份驗證,BearerTokenAuthentication
的實例就會設定在 SecurityContext
中。
這表示當您在組態中使用 @EnableWebFlux
時,它在 @Controller
方法中可用
-
Java
-
Kotlin
@GetMapping("/foo")
public Mono<String> foo(BearerTokenAuthentication authentication) {
return Mono.just(authentication.getTokenAttributes().get("sub") + " is the subject");
}
@GetMapping("/foo")
fun foo(authentication: BearerTokenAuthentication): Mono<String> {
return Mono.just(authentication.tokenAttributes["sub"].toString() + " is the subject")
}
由於 BearerTokenAuthentication
持有 OAuth2AuthenticatedPrincipal
,這也表示它也可用於控制器方法
-
Java
-
Kotlin
@GetMapping("/foo")
public Mono<String> foo(@AuthenticationPrincipal OAuth2AuthenticatedPrincipal principal) {
return Mono.just(principal.getAttribute("sub") + " is the subject");
}
@GetMapping("/foo")
fun foo(@AuthenticationPrincipal principal: OAuth2AuthenticatedPrincipal): Mono<String> {
return Mono.just(principal.getAttribute<Any>("sub").toString() + " is the subject")
}
使用 SpEL 查找屬性
您可以使用 Spring Expression Language (SpEL) 存取屬性。
例如,如果您使用 @EnableReactiveMethodSecurity
以便可以使用 @PreAuthorize
註解,您可以執行以下操作
-
Java
-
Kotlin
@PreAuthorize("principal?.attributes['sub'] = 'foo'")
public Mono<String> forFoosEyesOnly() {
return Mono.just("foo");
}
@PreAuthorize("principal.attributes['sub'] = 'foo'")
fun forFoosEyesOnly(): Mono<String> {
return Mono.just("foo")
}
覆寫或取代 Boot 自動組態
Spring Boot 為資源伺服器產生兩個 @Bean
實例。
第一個是 SecurityWebFilterChain
,它將應用程式組態為資源伺服器。當您使用不透明令牌時,此 SecurityWebFilterChain
看起來像這樣
-
Java
-
Kotlin
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange(exchanges -> exchanges
.anyExchange().authenticated()
)
.oauth2ResourceServer(ServerHttpSecurity.OAuth2ResourceServerSpec::opaqueToken)
return http.build();
}
@Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
return http {
authorizeExchange {
authorize(anyExchange, authenticated)
}
oauth2ResourceServer {
opaqueToken { }
}
}
}
如果應用程式未公開 SecurityWebFilterChain
bean,Spring Boot 會公開預設 bean(如前面的清單所示)。
您可以透過在應用程式中公開 bean 來取代它
-
Java
-
Kotlin
import static org.springframework.security.oauth2.core.authorization.OAuth2ReactiveAuthorizationManagers.hasScope;
@Configuration
@EnableWebFluxSecurity
public class MyCustomSecurityConfiguration {
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange(exchanges -> exchanges
.pathMatchers("/messages/**").access(hasScope("message:read"))
.anyExchange().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.opaqueToken(opaqueToken -> opaqueToken
.introspector(myIntrospector())
)
);
return http.build();
}
}
import org.springframework.security.oauth2.core.authorization.OAuth2ReactiveAuthorizationManagers.hasScope
@Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
return http {
authorizeExchange {
authorize("/messages/**", hasScope("message:read"))
authorize(anyExchange, authenticated)
}
oauth2ResourceServer {
opaqueToken {
introspector = myIntrospector()
}
}
}
}
前面的範例要求任何以 /messages/
開頭的 URL 都需要 message:read
的 scope。
oauth2ResourceServer
DSL 上的方法也會覆寫或取代自動組態。
例如,Spring Boot 建立的第二個 @Bean
是 ReactiveOpaqueTokenIntrospector
,它將 String
令牌解碼為 OAuth2AuthenticatedPrincipal
的驗證實例
-
Java
-
Kotlin
@Bean
public ReactiveOpaqueTokenIntrospector introspector() {
return new NimbusReactiveOpaqueTokenIntrospector(introspectionUri, clientId, clientSecret);
}
@Bean
fun introspector(): ReactiveOpaqueTokenIntrospector {
return NimbusReactiveOpaqueTokenIntrospector(introspectionUri, clientId, clientSecret)
}
如果應用程式未公開 ReactiveOpaqueTokenIntrospector
bean,Spring Boot 會公開預設的 bean(如前面的清單所示)。
您可以使用 introspectionUri()
和 introspectionClientCredentials()
來覆寫其組態,或使用 introspector()
來取代它。
使用 introspectionUri()
您可以將授權伺服器的 Introspection URI 組態為 組態屬性,或者您可以在 DSL 中提供
-
Java
-
Kotlin
@Configuration
@EnableWebFluxSecurity
public class DirectlyConfiguredIntrospectionUri {
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange(exchanges -> exchanges
.anyExchange().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.opaqueToken(opaqueToken -> opaqueToken
.introspectionUri("https://idp.example.com/introspect")
.introspectionClientCredentials("client", "secret")
)
);
return http.build();
}
}
@Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
return http {
authorizeExchange {
authorize(anyExchange, authenticated)
}
oauth2ResourceServer {
opaqueToken {
introspectionUri = "https://idp.example.com/introspect"
introspectionClientCredentials("client", "secret")
}
}
}
}
使用 introspectionUri()
優先於任何組態屬性。
使用 introspector()
introspector()
比 introspectionUri()
更強大。它完全取代了 ReactiveOpaqueTokenIntrospector
的任何 Boot 自動組態
-
Java
-
Kotlin
@Configuration
@EnableWebFluxSecurity
public class DirectlyConfiguredIntrospector {
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange(exchanges -> exchanges
.anyExchange().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.opaqueToken(opaqueToken -> opaqueToken
.introspector(myCustomIntrospector())
)
);
return http.build();
}
}
@Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
return http {
authorizeExchange {
authorize(anyExchange, authenticated)
}
oauth2ResourceServer {
opaqueToken {
introspector = myCustomIntrospector()
}
}
}
}
公開 ReactiveOpaqueTokenIntrospector
@Bean
或者,公開 ReactiveOpaqueTokenIntrospector
@Bean
與 introspector()
具有相同的效果
-
Java
-
Kotlin
@Bean
public ReactiveOpaqueTokenIntrospector introspector() {
return new NimbusReactiveOpaqueTokenIntrospector(introspectionUri, clientId, clientSecret);
}
@Bean
fun introspector(): ReactiveOpaqueTokenIntrospector {
return NimbusReactiveOpaqueTokenIntrospector(introspectionUri, clientId, clientSecret)
}
組態授權
OAuth 2.0 Introspection 端點通常會傳回一個 scope
屬性,指示已授予的 scope(或權限)——例如
{ ..., "scope" : "messages contacts"}
在這種情況下,資源伺服器會嘗試將這些 scope 強制轉換為授權列表,並為每個 scope 添加字串前綴:SCOPE_
。
這表示,若要使用從不透明令牌衍生的 scope 保護端點或方法,對應的表達式應包含此前綴
-
Java
-
Kotlin
import static org.springframework.security.oauth2.core.authorization.OAuth2ReactiveAuthorizationManagers.hasScope;
@Configuration
@EnableWebFluxSecurity
public class MappedAuthorities {
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange(exchange -> exchange
.pathMatchers("/contacts/**").access(hasScope("contacts"))
.pathMatchers("/messages/**").access(hasScope("messages"))
.anyExchange().authenticated()
)
.oauth2ResourceServer(ServerHttpSecurity.OAuth2ResourceServerSpec::opaqueToken);
return http.build();
}
}
import org.springframework.security.oauth2.core.authorization.OAuth2ReactiveAuthorizationManagers.hasScope
@Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
return http {
authorizeExchange {
authorize("/contacts/**", hasScope("contacts"))
authorize("/messages/**", hasScope("messages"))
authorize(anyExchange, authenticated)
}
oauth2ResourceServer {
opaqueToken { }
}
}
}
您可以使用方法安全執行類似的操作
-
Java
-
Kotlin
@PreAuthorize("hasAuthority('SCOPE_messages')")
public Flux<Message> getMessages(...) {}
@PreAuthorize("hasAuthority('SCOPE_messages')")
fun getMessages(): Flux<Message> { }
手動提取權限
預設情況下,不透明令牌支援從內省回應中提取 scope 宣告,並將其剖析為個別的 GrantedAuthority
實例。
考慮以下範例
{
"active" : true,
"scope" : "message:read message:write"
}
如果內省回應如前面的範例所示,資源伺服器將產生一個 Authentication
,其中包含兩個權限,一個用於 message:read
,另一個用於 message:write
。
您可以透過使用自訂的 ReactiveOpaqueTokenIntrospector
來客製化行為,該 introspector 會查看屬性集並以自己的方式轉換。
-
Java
-
Kotlin
public class CustomAuthoritiesOpaqueTokenIntrospector implements ReactiveOpaqueTokenIntrospector {
private ReactiveOpaqueTokenIntrospector delegate =
new NimbusReactiveOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret");
public Mono<OAuth2AuthenticatedPrincipal> introspect(String token) {
return this.delegate.introspect(token)
.map(principal -> 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 : ReactiveOpaqueTokenIntrospector {
private val delegate: ReactiveOpaqueTokenIntrospector = NimbusReactiveOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret")
override fun introspect(token: String): Mono<OAuth2AuthenticatedPrincipal> {
return delegate.introspect(token)
.map { principal: OAuth2AuthenticatedPrincipal ->
DefaultOAuth2AuthenticatedPrincipal(
principal.name, principal.attributes, extractAuthorities(principal))
}
}
private fun extractAuthorities(principal: OAuth2AuthenticatedPrincipal): Collection<GrantedAuthority> {
val scopes = principal.getAttribute<List<String>>(OAuth2IntrospectionClaimNames.SCOPE)
return scopes
.map { SimpleGrantedAuthority(it) }
}
}
此後,您可以透過將此自訂 introspector 公開為 @Bean
來組態它
-
Java
-
Kotlin
@Bean
public ReactiveOpaqueTokenIntrospector introspector() {
return new CustomAuthoritiesOpaqueTokenIntrospector();
}
@Bean
fun introspector(): ReactiveOpaqueTokenIntrospector {
return CustomAuthoritiesOpaqueTokenIntrospector()
}
將內省與 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
中的任何屬性都將是內省端點傳回的任何內容。
但是,假設由於某些原因,內省端點僅傳回令牌是否處於活動狀態。那又如何?
在這種情況下,您可以建立自訂的 ReactiveOpaqueTokenIntrospector
,它仍然會連線到端點,然後更新傳回的 principal 以將 JWT 宣告作為屬性。
-
Java
-
Kotlin
public class JwtOpaqueTokenIntrospector implements ReactiveOpaqueTokenIntrospector {
private ReactiveOpaqueTokenIntrospector delegate =
new NimbusReactiveOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret");
private ReactiveJwtDecoder jwtDecoder = new NimbusReactiveJwtDecoder(new ParseOnlyJWTProcessor());
public Mono<OAuth2AuthenticatedPrincipal> introspect(String token) {
return this.delegate.introspect(token)
.flatMap(principal -> this.jwtDecoder.decode(token))
.map(jwt -> new DefaultOAuth2AuthenticatedPrincipal(jwt.getClaims(), NO_AUTHORITIES));
}
private static class ParseOnlyJWTProcessor implements Converter<JWT, Mono<JWTClaimsSet>> {
public Mono<JWTClaimsSet> convert(JWT jwt) {
try {
return Mono.just(jwt.getJWTClaimsSet());
} catch (Exception ex) {
return Mono.error(ex);
}
}
}
}
class JwtOpaqueTokenIntrospector : ReactiveOpaqueTokenIntrospector {
private val delegate: ReactiveOpaqueTokenIntrospector = NimbusReactiveOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret")
private val jwtDecoder: ReactiveJwtDecoder = NimbusReactiveJwtDecoder(ParseOnlyJWTProcessor())
override fun introspect(token: String): Mono<OAuth2AuthenticatedPrincipal> {
return delegate.introspect(token)
.flatMap { jwtDecoder.decode(token) }
.map { jwt: Jwt -> DefaultOAuth2AuthenticatedPrincipal(jwt.claims, NO_AUTHORITIES) }
}
private class ParseOnlyJWTProcessor : Converter<JWT, Mono<JWTClaimsSet>> {
override fun convert(jwt: JWT): Mono<JWTClaimsSet> {
return try {
Mono.just(jwt.jwtClaimsSet)
} catch (e: Exception) {
Mono.error(e)
}
}
}
}
此後,您可以透過將此自訂 introspector 公開為 @Bean
來組態它
-
Java
-
Kotlin
@Bean
public ReactiveOpaqueTokenIntrospector introspector() {
return new JwtOpaqueTokenIntropsector();
}
@Bean
fun introspector(): ReactiveOpaqueTokenIntrospector {
return JwtOpaqueTokenIntrospector()
}
呼叫 /userinfo
端點
一般來說,資源伺服器不關心底層使用者,而是關心已授予的權限。
也就是說,有時將授權聲明連結回使用者可能很有價值。
如果應用程式也使用 spring-security-oauth2-client
,並且已設定適當的 ClientRegistrationRepository
,您可以使用自訂的 OpaqueTokenIntrospector
來執行此操作。下一個清單中的實作執行三件事
-
委派給內省端點,以確認令牌的有效性。
-
查找與
/userinfo
端點關聯的適當用戶端註冊。 -
調用並傳回來自
/userinfo
端點的回應。
-
Java
-
Kotlin
public class UserInfoOpaqueTokenIntrospector implements ReactiveOpaqueTokenIntrospector {
private final ReactiveOpaqueTokenIntrospector delegate =
new NimbusReactiveOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret");
private final ReactiveOAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService =
new DefaultReactiveOAuth2UserService();
private final ReactiveClientRegistrationRepository repository;
// ... constructor
@Override
public Mono<OAuth2AuthenticatedPrincipal> introspect(String token) {
return Mono.zip(this.delegate.introspect(token), this.repository.findByRegistrationId("registration-id"))
.map(t -> {
OAuth2AuthenticatedPrincipal authorized = t.getT1();
ClientRegistration clientRegistration = t.getT2();
Instant issuedAt = authorized.getAttribute(ISSUED_AT);
Instant expiresAt = authorized.getAttribute(OAuth2IntrospectionClaimNames.EXPIRES_AT);
OAuth2AccessToken accessToken = new OAuth2AccessToken(BEARER, token, issuedAt, expiresAt);
return new OAuth2UserRequest(clientRegistration, accessToken);
})
.flatMap(this.oauth2UserService::loadUser);
}
}
class UserInfoOpaqueTokenIntrospector : ReactiveOpaqueTokenIntrospector {
private val delegate: ReactiveOpaqueTokenIntrospector = NimbusReactiveOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret")
private val oauth2UserService: ReactiveOAuth2UserService<OAuth2UserRequest, OAuth2User> = DefaultReactiveOAuth2UserService()
private val repository: ReactiveClientRegistrationRepository? = null
// ... constructor
override fun introspect(token: String?): Mono<OAuth2AuthenticatedPrincipal> {
return Mono.zip<OAuth2AuthenticatedPrincipal, ClientRegistration>(delegate.introspect(token), repository!!.findByRegistrationId("registration-id"))
.map<OAuth2UserRequest> { t: Tuple2<OAuth2AuthenticatedPrincipal, ClientRegistration> ->
val authorized = t.t1
val clientRegistration = t.t2
val issuedAt: Instant? = authorized.getAttribute(ISSUED_AT)
val expiresAt: Instant? = authorized.getAttribute(OAuth2IntrospectionClaimNames.EXPIRES_AT)
val accessToken = OAuth2AccessToken(BEARER, token, issuedAt, expiresAt)
OAuth2UserRequest(clientRegistration, accessToken)
}
.flatMap { userRequest: OAuth2UserRequest -> oauth2UserService.loadUser(userRequest) }
}
}
如果您未使用 spring-security-oauth2-client
,它仍然非常簡單。您只需要使用您自己的 WebClient
實例調用 /userinfo
即可
-
Java
-
Kotlin
public class UserInfoOpaqueTokenIntrospector implements ReactiveOpaqueTokenIntrospector {
private final ReactiveOpaqueTokenIntrospector delegate =
new NimbusReactiveOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret");
private final WebClient rest = WebClient.create();
@Override
public Mono<OAuth2AuthenticatedPrincipal> introspect(String token) {
return this.delegate.introspect(token)
.map(this::makeUserInfoRequest);
}
}
class UserInfoOpaqueTokenIntrospector : ReactiveOpaqueTokenIntrospector {
private val delegate: ReactiveOpaqueTokenIntrospector = NimbusReactiveOpaqueTokenIntrospector("https://idp.example.org/introspect", "client", "secret")
private val rest: WebClient = WebClient.create()
override fun introspect(token: String): Mono<OAuth2AuthenticatedPrincipal> {
return delegate.introspect(token)
.map(this::makeUserInfoRequest)
}
}
無論如何,在建立您的 ReactiveOpaqueTokenIntrospector
之後,您應該將其發佈為 @Bean
以覆寫預設值
-
Java
-
Kotlin
@Bean
ReactiveOpaqueTokenIntrospector introspector() {
return new UserInfoOpaqueTokenIntrospector();
}
@Bean
fun introspector(): ReactiveOpaqueTokenIntrospector {
return UserInfoOpaqueTokenIntrospector()
}