Skip to content

Commit 8b0cd59

Browse files
committed
Add HttpStatusAccessDeniedHandler
1 parent 174f17e commit 8b0cd59

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.springframework.security.web.access;
2+
3+
import jakarta.servlet.ServletException;
4+
import jakarta.servlet.http.HttpServletRequest;
5+
import jakarta.servlet.http.HttpServletResponse;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.security.access.AccessDeniedException;
8+
import org.springframework.util.Assert;
9+
10+
import java.io.IOException;
11+
12+
public class HttpStatusAccessDeniedHandler implements AccessDeniedHandler {
13+
private final HttpStatus httpStatus;
14+
15+
public HttpStatusAccessDeniedHandler(HttpStatus httpStatus) {
16+
Assert.notNull(httpStatus, "httpStatus cannot be null");
17+
this.httpStatus = httpStatus;
18+
}
19+
20+
@Override
21+
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
22+
response.sendError(this.httpStatus.value(), accessDeniedException.getMessage());
23+
}
24+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.springframework.security.web.access;
2+
3+
import jakarta.servlet.ServletException;
4+
import jakarta.servlet.http.HttpServletRequest;
5+
import jakarta.servlet.http.HttpServletResponse;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.mockito.Mock;
9+
import org.mockito.junit.jupiter.MockitoExtension;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.mock.web.MockHttpServletResponse;
12+
import org.springframework.security.access.AccessDeniedException;
13+
14+
import java.io.IOException;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
18+
19+
@ExtendWith(MockitoExtension.class)
20+
public class HttpStatusAccessDeniedHandlerTests {
21+
@Mock
22+
private HttpServletRequest request;
23+
24+
@Mock
25+
private HttpServletResponse response;
26+
27+
private HttpStatus httpStatus = HttpStatus.FORBIDDEN;
28+
29+
private HttpStatusAccessDeniedHandler handler = new HttpStatusAccessDeniedHandler(this.httpStatus);
30+
31+
private AccessDeniedException exception = new AccessDeniedException("Forbidden");
32+
33+
@Test
34+
public void constructorHttpStatusWhenNullThenException() {
35+
assertThatIllegalArgumentException().isThrownBy(() -> new HttpStatusAccessDeniedHandler(null));
36+
}
37+
38+
@Test
39+
public void commenceThenStatusSet() throws IOException, ServletException {
40+
this.response = new MockHttpServletResponse();
41+
this.handler.handle(this.request, this.response, this.exception);
42+
assertThat(this.response.getStatus()).isEqualTo(this.httpStatus.value());
43+
}
44+
}

0 commit comments

Comments
 (0)