Skip to content

Commit c367fc4

Browse files
committed
Add WWW-Authenticate header for 401 and 403 requests
1 parent 5e1fbdc commit c367fc4

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.auth0.spring.security.api;
2+
3+
import org.springframework.http.HttpHeaders;
4+
import org.springframework.security.access.AccessDeniedException;
5+
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
6+
7+
import javax.servlet.ServletException;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
12+
/**
13+
* Custom handler for access denied exceptions.
14+
*/
15+
class JwtAccessDeniedHandler extends AccessDeniedHandlerImpl {
16+
17+
@Override
18+
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
19+
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Bearer error=\"Insufficient scope\"");
20+
super.handle(request, response, accessDeniedException);
21+
}
22+
}

lib/src/main/java/com/auth0/spring/security/api/JwtAuthenticationEntryPoint.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.auth0.spring.security.api;
22

3+
import org.springframework.http.HttpHeaders;
34
import org.springframework.security.core.AuthenticationException;
45
import org.springframework.security.web.AuthenticationEntryPoint;
56

@@ -9,8 +10,14 @@
910
import java.io.IOException;
1011

1112
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
13+
1214
@Override
1315
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
16+
response.addHeader(
17+
HttpHeaders.WWW_AUTHENTICATE,
18+
"Bearer error=\"Invalid access token\""
19+
);
20+
1421
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
1522
}
1623
}

lib/src/main/java/com/auth0/spring/security/api/JwtWebSecurityConfigurer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public HttpSecurity configure(HttpSecurity http) throws Exception {
103103
.and()
104104
.exceptionHandling()
105105
.authenticationEntryPoint(new JwtAuthenticationEntryPoint())
106+
.accessDeniedHandler(new JwtAccessDeniedHandler())
106107
.and()
107108
.httpBasic().disable()
108109
.csrf().disable()

lib/src/test/java/com/auth0/spring/security/api/JwtAuthenticationEntryPointTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ public void shouldReturnUnauthorized() throws Exception {
1919
AuthenticationException exception = mock(AuthenticationException.class);
2020

2121
entryPoint.commence(request, response, exception);
22+
verify(response).addHeader(
23+
"WWW-Authenticate",
24+
"Bearer error=\"Invalid access token\""
25+
);
2226
verify(response).sendError(401, "Unauthorized");
2327
}
24-
2528
}

0 commit comments

Comments
 (0)