我可以使用以下方式获取用户名:
@Autowired
private HttpServletRequest request;
Principal user = request.getUserPrincipal();
mqMessage.setUserName(user.getName());
但我想获取登录用户的firstName和lastName。如何获得ff。 userinfo使用SpringBoot keycloak适配器?
KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) request.getUserPrincipal();
KeycloakPrincipal principal=(KeycloakPrincipal)token.getPrincipal();
KeycloakSecurityContext session = principal.getKeycloakSecurityContext();
AccessToken accessToken = session.getToken();
username = accessToken.getPreferredUsername();
emailID = accessToken.getEmail();
lastname = accessToken.getFamilyName();
firstname = accessToken.getGivenName();
realmName = accessToken.getIssuer();
Access realmAccess = accessToken.getRealmAccess();
roles = realmAccess.getRoles();
您可以使用上面的代码片段来获取名字,这是从2.4.0开始的