I created a filter with ContainerRequestFilter interface and try to assign custom roles that returning user entity.
@Override
public ContainerRequest filter(ContainerRequest request) {
User user = authenticate(request);
if (user != null) {
request.setSecurityContext(new Authorizer(user));
} else {
throw new WebApplicationException(400);
}
return request;
}
private User authenticate(ContainerRequest request) {
user = new User("erhan", "customRole");
return user;
}
public class Authorizer implements SecurityContext {
private User user;
private Principal principal;
public Authorizer(final User user) {
this.user = user;
this.principal = new Principal() {
public String getName() {
return user.username;
}
};
}
public Principal getUserPrincipal() {
return this.principal;
}
public boolean isUserInRole(String role) {
return (role.equals(user.role));
}
public boolean isSecure() {
return "https".equals(uriInfo.getRequestUri().getScheme());
}
public String getAuthenticationScheme() {
return SecurityContext.BASIC_AUTH;
}
}
public class User {
public String username;
public String role;
public User(String username, String role) {
this.username = username;
this.role = role;
}
}
Everything is fine with that filter, but when it goes to web service
@GET
@RolesAllowed({"customRole"})
@Path("/test")
public String getByType(@Context HttpHeaders headers,@Context SecurityContext sc,
@Context HttpServletRequest request) {
return null;
}
it reaches the webservice but when i change role,still reached the same webservice. How can i provide different custom roles in Jersey ?