How to: enable and customize the current-user endpoint
The jEAP Security Starter's current-user endpoint returns user information and authorizations from the bearer token as JSON, for frontends that want this data without parsing the token themselves. See Current-User Endpoint for the full attribute reference.
Steps
- Enable the endpoint, which is inactive by default:
jeap:
security:
oauth2:
current-user-endpoint:
enabled: true
The endpoint is then accessible via the default path /api/current-user.
- (Optional) Configure a custom path instead:
jeap:
security:
oauth2:
current-user-endpoint:
path: /api/custom-path
-
(Optional) To add or remove attributes from the response, define a custom DTO. It must extend the
JeapCurrentUserDtoclass, introducing new attributes while calling the superclass constructor. Existing attributes can be removed by setting them tonull, except for the requiredsubjectattribute, which cannot be null.In this example, a new attribute
myCustomAttributeis added, whileadminDirUidandpamsLoginLevelare set tonull, excluding them from the response.
@Data
public class JmeCurrentUserDto extends JeapCurrentUserDto {
String myCustomAttribut;
public JmeCurrentUserDto(JeapCurrentUser jeapCurrentUser, String myCustomAttribut) {
super(jeapCurrentUser);
this.myCustomAttribut= myCustomAttribut;
setAdminDirUid(null);
setPamsLoginLevel(null);
}
}
- Construct the custom DTO in a Customizer class. This class must implement the
JeapCurrentUserCustomizerinterface and be instantiated as a Spring bean.
public interface JeapCurrentUserCustomizer<T extends JeapCurrentUser> {
T customize(JeapCurrentUser jeapCurrentUser);
}
Example of an implementation:
@org.springframework.stereotype.Component
public class JmeCurrentUserCustomizer implements JeapCurrentUserCustomizer<JmeCurrentUserDto> {
@Override
public JmeCurrentUserDto customize(JeapCurrentUser jeapCurrentUser) {
return new JmeCurrentUserDto(jeapCurrentUser, "fooBar");
}
}
Related
- Current-User Endpoint — the full response attribute reference and example JSON response.