22
33import cn .hutool .core .util .StrUtil ;
44import io .github .opensabre .authorization .entity .ScopeWithDescription ;
5+ import io .github .opensabre .authorization .entity .User ;
6+ import io .github .opensabre .authorization .service .IUserService ;
57import jakarta .annotation .Resource ;
8+ import org .springframework .security .core .Authentication ;
9+ import org .springframework .security .core .GrantedAuthority ;
610import org .springframework .security .oauth2 .core .endpoint .OAuth2ParameterNames ;
711import org .springframework .security .oauth2 .core .oidc .OidcScopes ;
812import org .springframework .security .oauth2 .server .authorization .OAuth2AuthorizationConsent ;
@@ -32,6 +36,9 @@ public class AuthorizationController {
3236 @ Resource
3337 private OAuth2AuthorizationConsentService authorizationConsentService ;
3438
39+ @ Resource
40+ private IUserService userService ;
41+
3542 /**
3643 * 登陆页面
3744 *
@@ -42,6 +49,28 @@ public String login() {
4249 return "login" ;
4350 }
4451
52+ /**
53+ * 用户个人主页
54+ *
55+ * @param principal 认证信息
56+ * @param model 页面model
57+ * @return 用户个人主页
58+ */
59+ @ GetMapping ({"/" , "/profile" })
60+ public String profile (Authentication principal , Model model ) {
61+ User user = userService .getByUniqueId (principal .getName ());
62+ String displayName = user != null && StrUtil .isNotBlank (user .getName ()) ? user .getName () : principal .getName ();
63+ Set <String > authorities = principal .getAuthorities ().stream ()
64+ .map (GrantedAuthority ::getAuthority )
65+ .collect (Collectors .toSet ());
66+ model .addAttribute ("user" , user );
67+ model .addAttribute ("principalName" , principal .getName ());
68+ model .addAttribute ("displayName" , displayName );
69+ model .addAttribute ("avatarText" , StrUtil .subPre (displayName , 1 ).toUpperCase ());
70+ model .addAttribute ("authorities" , authorities );
71+ return "profile" ;
72+ }
73+
4574 /**
4675 * 授权确认页面
4776 *
@@ -116,4 +145,4 @@ public String activated() {
116145 private static Set <ScopeWithDescription > withDescription (Set <String > scopes ) {
117146 return scopes .stream ().map (ScopeWithDescription ::new ).collect (Collectors .toSet ());
118147 }
119- }
148+ }
0 commit comments