🐛 修复 java.lang.IllegalArgumentException: claims cannot be empty

pull/29/head
smallbun 1 year ago
parent 2e036d73c2
commit 24d87f2744

@ -18,9 +18,7 @@
package cn.topiam.eiam.protocol.oidc.authorization.token; package cn.topiam.eiam.protocol.oidc.authorization.token;
import java.security.Principal; import java.security.Principal;
import java.util.Objects; import java.util.*;
import java.util.Optional;
import java.util.Set;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
@ -64,23 +62,29 @@ public class OAuth2TokenCustomizer implements
Optional<UserEntity> optional = userRepository.findById(Long.valueOf(principal.getId())); Optional<UserEntity> optional = userRepository.findById(Long.valueOf(principal.getId()));
if (optional.isPresent()){ if (optional.isPresent()){
UserEntity user = optional.get(); UserEntity user = optional.get();
boolean hasCustomClaims=false;
OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder(); OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder();
// Customize headers/claims for id_token // Customize headers/claims for id_token
if (authorizedScopes.contains(EMAIL)) { if (authorizedScopes.contains(EMAIL)) {
hasCustomClaims=true;
userInfoBuilder.email(StringUtils.defaultString(user.getEmail(), "")); userInfoBuilder.email(StringUtils.defaultString(user.getEmail(), ""));
userInfoBuilder.emailVerified(!Objects.isNull(user.getEmailVerified()) && user.getEmailVerified()); userInfoBuilder.emailVerified(!Objects.isNull(user.getEmailVerified()) && user.getEmailVerified());
} }
if (authorizedScopes.contains(PHONE)) { if (authorizedScopes.contains(PHONE)) {
hasCustomClaims=true;
userInfoBuilder.phoneNumber(StringUtils.defaultString(user.getPhone(), "")); userInfoBuilder.phoneNumber(StringUtils.defaultString(user.getPhone(), ""));
userInfoBuilder.phoneNumberVerified(!Objects.isNull(user.getPhoneVerified()) && user.getPhoneVerified()); userInfoBuilder.phoneNumberVerified(!Objects.isNull(user.getPhoneVerified()) && user.getPhoneVerified());
} }
if (authorizedScopes.contains(PROFILE)) { if (authorizedScopes.contains(PROFILE)) {
hasCustomClaims=true;
userInfoBuilder.preferredUsername(StringUtils.defaultString(user.getFullName(), "")); userInfoBuilder.preferredUsername(StringUtils.defaultString(user.getFullName(), ""));
userInfoBuilder.nickname(StringUtils.defaultString(user.getNickName(), "")); userInfoBuilder.nickname(StringUtils.defaultString(user.getNickName(), ""));
userInfoBuilder.updatedAt(user.getUpdateTime().format(DEFAULT_DATE_TIME_FORMATTER)); userInfoBuilder.updatedAt(user.getUpdateTime().format(DEFAULT_DATE_TIME_FORMATTER));
} }
context.getClaims().claims(claims -> if (hasCustomClaims){
claims.putAll(userInfoBuilder.build().getClaims())); context.getClaims().claims(claims ->
claims.putAll(userInfoBuilder.build().getClaims()));
}
} }
} }
} }

Loading…
Cancel
Save