How REST call token can be injected in the RunContext as Principal [message #1841320] |
Wed, 12 May 2021 03:21  |
Eclipse User |
|
|
|
Hi all,
We need to provide some REST api for our Scout application. We are using a token based authentication. Token is got by first authenticating the user (login, password) and then generated a token to be used in the next calls. So getting detail of a user API method is as follow:
@GET
@Path("get/{id}")
@Produces(MediaType.APPLICATION_JSON)
public UserEntityDo getUserDetail(@HeaderParam("token") String token, @PathParam("id") String id) {
// Check token validity and return corresponding user name if any
String username = checkToken(token);
if (username == null) {
Response.status(Response.Status.FORBIDDEN).build();
}
Subject subject = new Subject();
subject.getPrincipals().add(new SimplePrincipal(username));
subject.setReadOnly();
RunContext runContext = RunContexts.empty().withSubject(subject);
Map<String, Object> data = runContext.call(new Callable<Map<String, Object>>() {
@Override
public Map<String, Object> call() throws Exception {
return BEANS.get(IUserService.class).getData(UUID.fromString(id));
}
});
// Build user entity data
UserEntityDo user = BEANS.get(UserEntityDo.class).withLogin((String) data.get("login"))
.withLastLoginDate((Date) data.get("lastLoginDate").withFirstName((String) data.get("firstName").withLastName((String) data.get("lastName"));
return user;
}
The code work perfectly but we have to use the portion of code from //Check... till //Build... in all our method.
I'am thinking about putting this code within a ServletFilter that will inject the user principal in the RunContext based on the header token, but have no idea how to implement it.
Does anyone have an idea ?
Thanks in advance.
[Updated on: Wed, 12 May 2021 03:46] by Moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03293 seconds