Skip to content

Commit d8f1aed

Browse files
author
Olivier Chédru
authored
Merge pull request #70 from adamhoward/unauthorized_handler
Make UnauthorizedHandler overridable
2 parents f64c314 + 3cf6e4c commit d8f1aed

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/main/java/org/dhatim/dropwizard/jwt/cookie/authentication/JwtCookieAuthBundle.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import io.dropwizard.auth.AuthFilter;
2525
import io.dropwizard.auth.AuthValueFactoryProvider;
2626
import io.dropwizard.auth.Authorizer;
27+
import io.dropwizard.auth.DefaultUnauthorizedHandler;
28+
import io.dropwizard.auth.UnauthorizedHandler;
2729
import io.dropwizard.jersey.setup.JerseyEnvironment;
2830
import io.dropwizard.setup.Bootstrap;
2931
import io.dropwizard.setup.Environment;
@@ -59,6 +61,7 @@ public class JwtCookieAuthBundle<C extends Configuration, P extends JwtCookiePri
5961
private final Function<Claims, P> deserializer;
6062
private Function<C, JwtCookieAuthConfiguration> configurationSupplier;
6163
private BiFunction<C, Environment, Key> keySuppplier;
64+
private UnauthorizedHandler unauthorizedHandler;
6265

6366
/**
6467
* Get a bundle instance that will use DefaultJwtCookiePrincipal
@@ -85,6 +88,7 @@ public JwtCookieAuthBundle(Class<P> principalType, Function<P, Claims> serialize
8588
this.serializer = serializer;
8689
this.deserializer = deserializer;
8790
this.configurationSupplier = c -> new JwtCookieAuthConfiguration();
91+
this.unauthorizedHandler = new DefaultUnauthorizedHandler();
8892
}
8993

9094
/**
@@ -109,6 +113,17 @@ public JwtCookieAuthBundle<C, P> withConfigurationSupplier(Function<C, JwtCookie
109113
return this;
110114
}
111115

116+
/**
117+
* If you want to use a different unauthorized handler, specify it here
118+
*
119+
* @param unauthorizedHandler an UnauthorizedHandler that will be used whenever a request fails to authenticate
120+
* @return this
121+
*/
122+
public JwtCookieAuthBundle<C, P> withUnauthorizedHandler(UnauthorizedHandler unauthorizedHandler) {
123+
this.unauthorizedHandler = unauthorizedHandler;
124+
return this;
125+
}
126+
112127

113128
@Override
114129
public void initialize(Bootstrap<?> bootstrap) {
@@ -147,6 +162,7 @@ public AuthFilter<String, P> getAuthRequestFilter(Key key, String cookieName) {
147162
.setAuthenticator(new JwtCookiePrincipalAuthenticator(key, deserializer))
148163
.setPrefix(JWT_COOKIE_PREFIX)
149164
.setAuthorizer((Authorizer<P>) (P::isInRole))
165+
.setUnauthorizedHandler(unauthorizedHandler)
150166
.buildAuthFilter();
151167
}
152168

src/main/java/org/dhatim/dropwizard/jwt/cookie/authentication/JwtCookieAuthRequestFilter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import javax.annotation.Priority;
2424
import javax.ws.rs.InternalServerErrorException;
2525
import javax.ws.rs.Priorities;
26-
import javax.ws.rs.WebApplicationException;
2726
import javax.ws.rs.container.ContainerRequestContext;
2827
import javax.ws.rs.core.Cookie;
2928

@@ -54,7 +53,7 @@ public void filter(ContainerRequestContext crc) throws IOException {
5453
}
5554
}
5655
}
57-
throw new WebApplicationException(unauthorizedHandler.buildResponse(prefix, realm));
56+
throw unauthorizedHandler.buildException(prefix, realm);
5857
}
5958

6059
public static class Builder<P extends JwtCookiePrincipal> extends AuthFilterBuilder<String, P, JwtCookieAuthRequestFilter<P>> {

0 commit comments

Comments
 (0)