Skip to content

Commit

Permalink
Merge pull request #2841 from cyberdelia/json-1.3
Browse files Browse the repository at this point in the history
Add a JSONUnauthorizedHandler
  • Loading branch information
jplock committed Jul 15, 2019
2 parents bb92ab5 + 4b79d4e commit bad7391
Showing 1 changed file with 24 additions and 0 deletions.
@@ -0,0 +1,24 @@
package io.dropwizard.auth;

import io.dropwizard.jersey.errors.ErrorMessage;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class JSONUnauthorizedHandler implements UnauthorizedHandler {
private static final String CHALLENGE_FORMAT = "%s realm=\"%s\"";

@Override
public Response buildResponse(String prefix, String realm) {
ErrorMessage errorMessage = new ErrorMessage(
Response.Status.UNAUTHORIZED.getStatusCode(),
"Credentials are required to access this resource."
);
return Response.status(errorMessage.getCode())
.header(HttpHeaders.WWW_AUTHENTICATE, String.format(CHALLENGE_FORMAT, prefix, realm))
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(errorMessage)
.build();
}
}

0 comments on commit bad7391

Please sign in to comment.