seb*_*enz 26
您可以从安全上下文中获取所有用户信息.
例:
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 + "\" }";
}
Run Code Online (Sandbox Code Playgroud)
要传播安全上下文,您必须按照以下内容配置安全域: JBoss/Wildfly Adapter配置
小智 17
您还可以将Web应用程序principal-attribute的keycloak.json文件中的属性设置为preferred_username.
| 归档时间: |
|
| 查看次数: |
13126 次 |
| 最近记录: |