-
Notifications
You must be signed in to change notification settings - Fork 28
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 #51 from jimmyjames/add-www-authenticate-header
Add WWW-Authenticate header for 401 and 403 requests
- Loading branch information
Showing
5 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
lib/src/main/java/com/auth0/spring/security/api/JwtAccessDeniedHandler.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,22 @@ | ||
package com.auth0.spring.security.api; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandlerImpl; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Custom handler for access denied exceptions. | ||
*/ | ||
class JwtAccessDeniedHandler extends AccessDeniedHandlerImpl { | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { | ||
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Bearer error=\"Insufficient scope\""); | ||
super.handle(request, response, accessDeniedException); | ||
} | ||
} |
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
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
28 changes: 28 additions & 0 deletions
28
lib/src/test/java/com/auth0/spring/security/api/JwtAccessDeniedHandlerTest.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,28 @@ | ||
package com.auth0.spring.security.api; | ||
|
||
import org.junit.Test; | ||
import org.springframework.security.access.AccessDeniedException; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class JwtAccessDeniedHandlerTest { | ||
|
||
@Test | ||
public void shouldReturnForbidden() throws Exception { | ||
JwtAccessDeniedHandler handler = new JwtAccessDeniedHandler(); | ||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
HttpServletResponse response = mock(HttpServletResponse.class); | ||
AccessDeniedException exception = new AccessDeniedException("Forbidden"); | ||
|
||
handler.handle(request, response, exception); | ||
verify(response).addHeader( | ||
"WWW-Authenticate", | ||
"Bearer error=\"Insufficient scope\"" | ||
); | ||
verify(response).sendError(403, "Forbidden"); | ||
} | ||
} |
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