-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Add RequestRejectedHandler #7052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
36 changes: 36 additions & 0 deletions
36
...rc/main/java/org/springframework/security/web/firewall/DefaultRequestRejectedHandler.java
This file contains hidden or 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,36 @@ | ||
| /* | ||
| * Copyright 2002-2018 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.security.web.firewall; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| /** | ||
| * Default implementation of {@link RequestRejectedHandler} that simply rethrows the exception. | ||
leonard84 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @author Leonard Brünings | ||
| * @since 5.2 | ||
| */ | ||
| public class DefaultRequestRejectedHandler implements RequestRejectedHandler { | ||
leonard84 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Override | ||
| public void handle(HttpServletRequest request, HttpServletResponse response, | ||
| RequestRejectedException requestRejectedException) throws IOException, ServletException { | ||
| throw requestRejectedException; | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
...main/java/org/springframework/security/web/firewall/HttpStatusRequestRejectedHandler.java
This file contains hidden or 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,61 @@ | ||
| /* | ||
| * Copyright 2002-2018 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.security.web.firewall; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
|
|
||
| /** | ||
| * A simple implementation of {@link RequestRejectedHandler} that sends an error with configurable status code. | ||
| * | ||
| * @author Leonard Brünings | ||
| * @since 5.2 | ||
| */ | ||
| public class HttpStatusRequestRejectedHandler implements RequestRejectedHandler { | ||
| private static final Log logger = LogFactory.getLog(HttpStatusRequestRejectedHandler.class); | ||
|
|
||
| private final int httpError; | ||
|
|
||
| /** | ||
| * Constructs an instance which uses {@code 400} as response code. | ||
| */ | ||
| public HttpStatusRequestRejectedHandler() { | ||
| httpError = HttpServletResponse.SC_BAD_REQUEST; | ||
| } | ||
|
|
||
| /** | ||
| * Constructs an instance which uses a configurable http code as response. | ||
| * @param httpError http status code to use | ||
| */ | ||
| public HttpStatusRequestRejectedHandler(int httpError) { | ||
| this.httpError = httpError; | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(HttpServletRequest request, HttpServletResponse response, | ||
| RequestRejectedException requestRejectedException) throws IOException { | ||
| if (logger.isDebugEnabled()) { | ||
| logger.debug("Rejecting request due to: " + requestRejectedException.getMessage(), | ||
| requestRejectedException); | ||
| } | ||
| response.sendError(httpError); | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
web/src/main/java/org/springframework/security/web/firewall/RequestRejectedHandler.java
This file contains hidden or 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,48 @@ | ||
| /* | ||
| * Copyright 2002-2018 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.security.web.firewall; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| /** | ||
| * Used by {@link org.springframework.security.web.FilterChainProxy} to handle an | ||
| * <code>RequestRejectedException</code>. | ||
| * | ||
| * @author Leonard Brünings | ||
| * @since 5.2 | ||
| */ | ||
leonard84 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public interface RequestRejectedHandler { | ||
| // ~ Methods | ||
| // ======================================================================================================== | ||
|
|
||
| /** | ||
| * Handles an request rejected failure. | ||
| * | ||
| * @param request that resulted in an <code>RequestRejectedException</code> | ||
| * @param response so that the user agent can be advised of the failure | ||
| * @param requestRejectedException that caused the invocation | ||
| * | ||
| * @throws IOException in the event of an IOException | ||
| * @throws ServletException in the event of a ServletException | ||
| */ | ||
| void handle(HttpServletRequest request, HttpServletResponse response, | ||
| RequestRejectedException requestRejectedException) throws IOException, | ||
| ServletException; | ||
| } | ||
This file contains hidden or 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
46 changes: 46 additions & 0 deletions
46
...est/java/org/springframework/security/web/firewall/DefaultRequestRejectedHandlerTest.java
This file contains hidden or 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,46 @@ | ||
| /* | ||
| * Copyright 2002-2016 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.security.web.firewall; | ||
|
|
||
|
|
||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import org.hamcrest.CoreMatchers; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| public class DefaultRequestRejectedHandlerTest { | ||
|
|
||
| @Test | ||
| public void defaultRequestRejectedHandlerRethrowsTheException() throws Exception { | ||
| // given: | ||
| RequestRejectedException requestRejectedException = new RequestRejectedException("rejected"); | ||
| DefaultRequestRejectedHandler sut = new DefaultRequestRejectedHandler(); | ||
|
|
||
| //when: | ||
| try { | ||
| sut.handle(mock(HttpServletRequest.class), mock(HttpServletResponse.class), requestRejectedException); | ||
| } catch (RequestRejectedException exception) { | ||
| //then: | ||
| Assert.assertThat(exception.getMessage(), CoreMatchers.is("rejected")); | ||
| return; | ||
| } | ||
| Assert.fail("Exception was not rethrown"); | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
.../java/org/springframework/security/web/firewall/HttpStatusRequestRejectedHandlerTest.java
This file contains hidden or 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,61 @@ | ||
| /* | ||
| * Copyright 2002-2016 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.security.web.firewall; | ||
|
|
||
|
|
||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class HttpStatusRequestRejectedHandlerTest { | ||
|
|
||
| @Test | ||
| public void httpStatusRequestRejectedHandlerUsesStatus400byDefault() throws Exception { | ||
| //given: | ||
| HttpStatusRequestRejectedHandler sut = new HttpStatusRequestRejectedHandler(); | ||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||
|
|
||
| //when: | ||
| sut.handle(mock(HttpServletRequest.class), response, mock(RequestRejectedException.class)); | ||
|
|
||
| // then: | ||
| verify(response).sendError(400); | ||
| } | ||
|
|
||
| @Test | ||
| public void httpStatusRequestRejectedHandlerCanBeConfiguredToUseStatus() throws Exception { | ||
| httpStatusRequestRejectedHandlerCanBeConfiguredToUseStatusHelper(400); | ||
| httpStatusRequestRejectedHandlerCanBeConfiguredToUseStatusHelper(403); | ||
| httpStatusRequestRejectedHandlerCanBeConfiguredToUseStatusHelper(500); | ||
| } | ||
|
|
||
| private void httpStatusRequestRejectedHandlerCanBeConfiguredToUseStatusHelper(int status) throws Exception { | ||
|
|
||
| //given: | ||
| HttpStatusRequestRejectedHandler sut = new HttpStatusRequestRejectedHandler(status); | ||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||
|
|
||
| //when: | ||
| sut.handle(mock(HttpServletRequest.class), response, mock(RequestRejectedException.class)); | ||
|
|
||
| // then: | ||
| verify(response).sendError(status); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.