I am trying to add custom authorization in dropwizard but not able to successed.
I have a custom authentication added for dropwizard by binding it to authFactory
Authenticator ssoAuthenticator = createSSOAuthenticator(configuration.getSsoGrantClientConfiguration());
environment.jersey().register(AuthFactory.binder(
new SSOTokenAuthFactory<SSOGrant>(
ssoAuthenticator,
SYSTEM_PREFIX,
SSOGrant.class))
);
and adding a dynamicfeature for authorization
environment.jersey().register(PermissionDynamicFeature.class);
Below is the annotation created
@Documented
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER,java.lang.annotation.ElementType.TYPE, java.lang.annotation.ElementType.METHOD})
public @interface PermissionsAllowed {
String[] value();
}
I am checking whether the annotation is present on the method and then registering a filter
public class PermissionDynamicFeature implements DynamicFeature {
@Override
public void configure(final ResourceInfo resourceInfo, final FeatureContext configuration) {
final AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod());
final Annotation[][] parameterAnnotations = am.getParameterAnnotations();
for (Annotation[] annotations : parameterAnnotations) {
for (Annotation annotation : annotations) {
if (annotation instanceof PermissionsAllowed) {
configuration.register(new RolesAllowedRequestFilter(((PermissionsAllowed)annotation).value()));
return;
}
}
}
}
//@Priority(Priorities.USER) // authorization filter - should go after any authentication filters
private static class RolesAllowedRequestFilter implements ContainerRequestFilter {
private final boolean denyAll;
private final String[] rolesAllowed;
RolesAllowedRequestFilter() {
this.denyAll = true;
this.rolesAllowed = null;
}
RolesAllowedRequestFilter(final String[] rolesAllowed) {
this.denyAll = false;
this.rolesAllowed = (rolesAllowed != null) ? rolesAllowed : new String[] {};
}
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
if (!denyAll) {
if (rolesAllowed.length > 0 && !isAuthenticated(requestContext)) {
throw new ForbiddenException(LocalizationMessages.USER_NOT_AUTHORIZED());
}
for (final String role : rolesAllowed) {
if (requestContext.getSecurityContext().isUserInRole(role)) {
return;
}
}
}
throw new ForbiddenException(LocalizationMessages.USER_NOT_AUTHORIZED());
}
private static boolean isAuthenticated(final ContainerRequestContext requestContext) {
return requestContext.getSecurityContext().getUserPrincipal() != null;
}
}
}
I am just trying to build my authorization based on the same lines as RolesAllowed filter.
The issue that i am facing is that the Authorization filter is called before the authentication.
What i am missing so that the authentication happens first and the authorization filter is called later?
The same happens when we register RolesAllowedDynamicFeature
environment.jersey().register(RolesAllowedDynamicFeature.class);
RolesAllowedDynamicFeature is called even before the authentication happens.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…