-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/example/spring/authorizationserver/config/JwtTokenCustomizerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example.spring.authorizationserver.config; | ||
|
||
import com.example.spring.authorizationserver.security.OidcUserInfoService; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.oauth2.core.oidc.OidcUserInfo; | ||
import org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames; | ||
import org.springframework.security.oauth2.server.authorization.OAuth2TokenType; | ||
import org.springframework.security.oauth2.server.authorization.token.JwtEncodingContext; | ||
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenCustomizer; | ||
|
||
import static org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames.ID_TOKEN; | ||
import static org.springframework.security.oauth2.server.authorization.OAuth2TokenType.ACCESS_TOKEN; | ||
|
||
@Configuration | ||
public class JwtTokenCustomizerConfig { | ||
@Bean | ||
public OAuth2TokenCustomizer<JwtEncodingContext> tokenCustomizer(OidcUserInfoService userInfoService) { | ||
return (context) -> { | ||
if (ID_TOKEN.equals(context.getTokenType().getValue()) || ACCESS_TOKEN.equals(context.getTokenType())) { | ||
OidcUserInfo userInfo = userInfoService.loadUser( | ||
context.getPrincipal().getName()); | ||
context.getClaims().claims(claims -> | ||
claims.putAll(userInfo.getClaims())); | ||
} | ||
}; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/example/spring/authorizationserver/security/OidcUserInfoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.example.spring.authorizationserver.security; | ||
|
||
import com.example.spring.authorizationserver.user.User; | ||
import org.springframework.security.oauth2.core.oidc.OidcUserInfo; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class OidcUserInfoService { | ||
private final AuthorizationServerUserDetailsService userDetailsService; | ||
|
||
public OidcUserInfoService(AuthorizationServerUserDetailsService userDetailsService) { | ||
this.userDetailsService = userDetailsService; | ||
} | ||
|
||
public OidcUserInfo loadUser(String username) { | ||
User user = (User) userDetailsService.loadUserByUsername(username); | ||
return OidcUserInfo.builder() | ||
.subject(user.getIdentifier().toString()) | ||
.name(user.getFirstName() + " " + user.getLastName()) | ||
.givenName(user.getFirstName()) | ||
.familyName(user.getLastName()) | ||
.nickname(username) | ||
.preferredUsername(username) | ||
.profile("https://example.com/" + username) | ||
.website("https://example.com") | ||
.email(user.getEmail()) | ||
.emailVerified(true) | ||
.claim("roles", user.getRoles()) | ||
.zoneinfo("Europe/Berlin") | ||
.locale("de-DE") | ||
.updatedAt("1970-01-01T00:00:00Z") | ||
.build(); | ||
} | ||
} |