Last active
April 9, 2016 18:13
Star
You must be signed in to star a gist
A JAX-RS authentication (request) filter which executes (custom) token based authentication and creates a custom SecurityContext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Priority(Priorities.AUTHENTICATION) | |
public class AuthFilterWithCustomSecurityContext implements ContainerRequestFilter { | |
@Context | |
UriInfo uriInfo; | |
@Override | |
public void filter(ContainerRequestContext requestContext) throws IOException { | |
String authHeaderVal = requestContext.getHeaderString("Auth-Token"); | |
String subject = validateToken(authHeaderVal); //execute custom authentication | |
if (subject!=null) { | |
final SecurityContext securityContext = requestContext.getSecurityContext(); | |
requestContext.setSecurityContext(new SecurityContext() { | |
@Override | |
public Principal getUserPrincipal() { | |
return new Principal() { | |
@Override | |
public String getName() { | |
return subject; | |
} | |
}; | |
} | |
@Override | |
public boolean isUserInRole(String role) { | |
List<Role> roles = findUserRoles(subject); | |
return roles.contains(role); | |
} | |
@Override | |
public boolean isSecure() { | |
return uriInfo.getAbsolutePath().toString().startsWith("https"); | |
} | |
@Override | |
public String getAuthenticationScheme() { | |
return "Token-Based-Auth-Scheme"; | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment