Skip to content

Commit 750e135

Browse files
committed
Add HttpStatusAccessDeniedHandler
1 parent 174f17e commit 750e135

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
14+
private final HttpStatus httpStatus;
15+
16+
public HttpStatusAccessDeniedHandler(HttpStatus httpStatus) {
17+
Assert.notNull(httpStatus, "httpStatus cannot be null");
18+
this.httpStatus = httpStatus;
19+
}
20+
21+
@Override
22+
public void handle(HttpServletRequest request, HttpServletResponse response,
23+
AccessDeniedException accessDeniedException) throws IOException, ServletException {
24+
response.sendError(this.httpStatus.value(), accessDeniedException.getMessage());
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
22+
@Mock
23+
private HttpServletRequest request;
24+
25+
@Mock
26+
private HttpServletResponse response;
27+
28+
private HttpStatus httpStatus = HttpStatus.FORBIDDEN;
29+
30+
private HttpStatusAccessDeniedHandler handler = new HttpStatusAccessDeniedHandler(this.httpStatus);
31+
32+
private AccessDeniedException exception = new AccessDeniedException("Forbidden");
33+
34+
@Test
35+
public void constructorHttpStatusWhenNullThenException() {
36+
assertThatIllegalArgumentException().isThrownBy(() -> new HttpStatusAccessDeniedHandler(null));
37+
}
38+
39+
@Test
40+
public void commenceThenStatusSet() throws IOException, ServletException {
41+
this.response = new MockHttpServletResponse();
42+
this.handler.handle(this.request, this.response, this.exception);
43+
assertThat(this.response.getStatus()).isEqualTo(this.httpStatus.value());
44+
}
45+
46+
}

0 commit comments

Comments
 (0)