2424import io .dropwizard .auth .AuthFilter ;
2525import io .dropwizard .auth .AuthValueFactoryProvider ;
2626import io .dropwizard .auth .Authorizer ;
27+ import io .dropwizard .auth .DefaultUnauthorizedHandler ;
28+ import io .dropwizard .auth .UnauthorizedHandler ;
2729import io .dropwizard .jersey .setup .JerseyEnvironment ;
2830import io .dropwizard .setup .Bootstrap ;
2931import 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
0 commit comments