|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
- * Copyright 2002-2018 the original author or authors.
|
|
|
+ * Copyright 2002-2023 the original author or authors.
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -23,6 +23,7 @@ import org.springframework.core.convert.converter.Converter;
|
|
|
import org.springframework.security.authentication.AbstractAuthenticationToken;
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
import org.springframework.security.oauth2.jwt.Jwt;
|
|
|
+import org.springframework.security.oauth2.jwt.JwtClaimNames;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
/**
|
|
@@ -30,6 +31,7 @@ import org.springframework.util.Assert;
|
|
|
* a {@link AbstractAuthenticationToken Mono<AbstractAuthenticationToken>}.
|
|
|
*
|
|
|
* @author Eric Deandrea
|
|
|
+ * @author Marcus Kainth
|
|
|
* @since 5.2
|
|
|
*/
|
|
|
public final class ReactiveJwtAuthenticationConverter implements Converter<Jwt, Mono<AbstractAuthenticationToken>> {
|
|
@@ -37,12 +39,17 @@ public final class ReactiveJwtAuthenticationConverter implements Converter<Jwt,
|
|
|
private Converter<Jwt, Flux<GrantedAuthority>> jwtGrantedAuthoritiesConverter = new ReactiveJwtGrantedAuthoritiesConverterAdapter(
|
|
|
new JwtGrantedAuthoritiesConverter());
|
|
|
|
|
|
+ private String principalClaimName = JwtClaimNames.SUB;
|
|
|
+
|
|
|
@Override
|
|
|
public Mono<AbstractAuthenticationToken> convert(Jwt jwt) {
|
|
|
// @formatter:off
|
|
|
return this.jwtGrantedAuthoritiesConverter.convert(jwt)
|
|
|
.collectList()
|
|
|
- .map((authorities) -> new JwtAuthenticationToken(jwt, authorities));
|
|
|
+ .map((authorities) -> {
|
|
|
+ String principalName = jwt.getClaimAsString(this.principalClaimName);
|
|
|
+ return new JwtAuthenticationToken(jwt, authorities, principalName);
|
|
|
+ });
|
|
|
// @formatter:on
|
|
|
}
|
|
|
|
|
@@ -58,4 +65,14 @@ public final class ReactiveJwtAuthenticationConverter implements Converter<Jwt,
|
|
|
this.jwtGrantedAuthoritiesConverter = jwtGrantedAuthoritiesConverter;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the principal claim name. Defaults to {@link JwtClaimNames#SUB}.
|
|
|
+ * @param principalClaimName The principal claim name
|
|
|
+ * @since 6.1
|
|
|
+ */
|
|
|
+ public void setPrincipalClaimName(String principalClaimName) {
|
|
|
+ Assert.hasText(principalClaimName, "principalClaimName cannot be empty");
|
|
|
+ this.principalClaimName = principalClaimName;
|
|
|
+ }
|
|
|
+
|
|
|
}
|