Skip to content

Commit 16f4e06

Browse files
committed
oidc debug log
1 parent 60bd514 commit 16f4e06

1 file changed

Lines changed: 51 additions & 8 deletions

File tree

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/oidc/OidcAuthenticationSuccessEventListener.java

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
2020
import com.ctrip.framework.apollo.portal.spi.configuration.OidcExtendProperties;
21+
import java.util.Map;
22+
import java.util.Map.Entry;
2123
import java.util.concurrent.ConcurrentHashMap;
2224
import java.util.concurrent.ConcurrentMap;
2325
import org.slf4j.Logger;
@@ -36,6 +38,12 @@ public class OidcAuthenticationSuccessEventListener implements
3638
private static final Logger log = LoggerFactory
3739
.getLogger(OidcAuthenticationSuccessEventListener.class);
3840

41+
private static final Logger oidcLog = LoggerFactory.getLogger(
42+
OidcAuthenticationSuccessEventListener.class.getName() + ".oidc");
43+
44+
private static final Logger jwtLog = LoggerFactory.getLogger(
45+
OidcAuthenticationSuccessEventListener.class.getName() + ".jwt");
46+
3947
private final OidcLocalUserService oidcLocalUserService;
4048

4149
private final OidcExtendProperties oidcExtendProperties;
@@ -63,18 +71,36 @@ public void onApplicationEvent(AuthenticationSuccessEvent event) {
6371
}
6472

6573
private void oidcUserLogin(OidcUser oidcUser) {
74+
String subject = oidcUser.getSubject();
75+
String userDisplayName = OidcUserInfoUtil.getOidcUserDisplayName(oidcUser,
76+
this.oidcExtendProperties);
77+
String email = oidcUser.getEmail();
78+
79+
this.logOidc(oidcUser, subject, userDisplayName, email);
80+
6681
UserInfo newUserInfo = new UserInfo();
67-
newUserInfo.setUserId(oidcUser.getSubject());
68-
newUserInfo.setName(
69-
OidcUserInfoUtil.getOidcUserDisplayName(oidcUser, this.oidcExtendProperties));
70-
newUserInfo.setEmail(oidcUser.getEmail());
71-
if (this.contains(oidcUser.getSubject())) {
82+
newUserInfo.setUserId(subject);
83+
newUserInfo.setName(userDisplayName);
84+
newUserInfo.setEmail(email);
85+
if (this.contains(subject)) {
7286
this.oidcLocalUserService.updateUserInfo(newUserInfo);
7387
return;
7488
}
7589
this.oidcLocalUserService.createLocalUser(newUserInfo);
7690
}
7791

92+
private void logOidc(OidcUser oidcUser, String subject, String userDisplayName,
93+
String email) {
94+
oidcLog.debug("oidc authentication success, sub=[{}] userDisplayName=[{}] email=[{}]", subject,
95+
userDisplayName, email);
96+
if (oidcLog.isTraceEnabled()) {
97+
Map<String, Object> claims = oidcUser.getClaims();
98+
for (Entry<String, Object> entry : claims.entrySet()) {
99+
oidcLog.trace("oidc authentication claims [{}={}]", entry.getKey(), entry.getValue());
100+
}
101+
}
102+
}
103+
78104
private boolean contains(String userId) {
79105
if (this.userIdCache.containsKey(userId)) {
80106
return true;
@@ -88,12 +114,29 @@ private boolean contains(String userId) {
88114
}
89115

90116
private void jwtLogin(Jwt jwt) {
91-
if (this.contains(jwt.getSubject())) {
117+
String subject = jwt.getSubject();
118+
String userDisplayName = OidcUserInfoUtil.getJwtUserDisplayName(jwt,
119+
this.oidcExtendProperties);
120+
121+
this.logJwt(jwt, subject, userDisplayName);
122+
123+
if (this.contains(subject)) {
92124
return;
93125
}
94126
UserInfo newUserInfo = new UserInfo();
95-
newUserInfo.setUserId(jwt.getSubject());
96-
newUserInfo.setName(OidcUserInfoUtil.getJwtUserDisplayName(jwt, this.oidcExtendProperties));
127+
newUserInfo.setUserId(subject);
128+
newUserInfo.setName(userDisplayName);
97129
this.oidcLocalUserService.createLocalUser(newUserInfo);
98130
}
131+
132+
private void logJwt(Jwt jwt, String subject, String userDisplayName) {
133+
jwtLog.debug("jwt authentication success, sub=[{}] userDisplayName=[{}]", subject,
134+
userDisplayName);
135+
if (jwtLog.isTraceEnabled()) {
136+
Map<String, Object> claims = jwt.getClaims();
137+
for (Entry<String, Object> entry : claims.entrySet()) {
138+
jwtLog.trace("jwt authentication claims [{}={}]", entry.getKey(), entry.getValue());
139+
}
140+
}
141+
}
99142
}

0 commit comments

Comments
 (0)