您从安全上下文中获取所有用户信息。
例:
public class Greeter { @Context SecurityContext sc; @GET @Produces(MediaType.APPLICATION_JSON) public String sayHello() { // this will set the user id as userName String userName = sc.getUserPrincipal().getName(); if (sc.getUserPrincipal() instanceof KeycloakPrincipal) { KeycloakPrincipal<KeycloakSecurityContext> kp = (KeycloakPrincipal<KeycloakSecurityContext>) sc.getUserPrincipal(); // this is how to get the real userName (or rather the login name) userName = kp.getKeycloakSecurityContext().getIdToken().getPreferredUsername(); } return "{ message : "Hello " + userName + "" }";}为了传播安全上下文,您必须具有如下配置的安全域: JBoss /
Wildfly适配器配置



