Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2841 from cyberdelia/json-1.3
Add a JSONUnauthorizedHandler
- Loading branch information
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
dropwizard-auth/src/main/java/io/dropwizard/auth/JSONUnauthorizedHandler.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |