forked from zalando/nakadi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create RequestRejectedFilter to handle RequestRejectedException
Spring firewall returns 500 when RequestRejectedException is thrown. The correct status code is 400. This is going to be addressed by spring-projects/spring-security#7568
- Loading branch information
1 parent
a108f05
commit 907f94f
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
acceptance-test/src/acceptance-test/java/org/zalando/nakadi/webservice/InvalidRequestAT.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,18 @@ | ||
package org.zalando.nakadi.webservice; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.junit.Test; | ||
|
||
import static com.jayway.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
|
||
public class InvalidRequestAT { | ||
@Test(timeout = 10000) | ||
public void whenRequestRejectedExceptionThrownThenResponseIs400() { | ||
given() | ||
.when() | ||
.get("//") | ||
.then() | ||
.statusCode(HttpStatus.SC_BAD_REQUEST); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/org/zalando/nakadi/filters/RequestRejectedFilter.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,23 @@ | ||
package org.zalando.nakadi.filters; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.springframework.security.web.firewall.RequestRejectedException; | ||
import org.springframework.stereotype.Component; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@Aspect | ||
@Component | ||
public class RequestRejectedFilter { | ||
|
||
@Around("execution(public void org.springframework.security.web.FilterChainProxy.doFilter(..))") | ||
public void handleRequestRejectedException(ProceedingJoinPoint pjp) throws Throwable { | ||
try { | ||
pjp.proceed(); | ||
} catch (RequestRejectedException exception) { | ||
HttpServletResponse response = (HttpServletResponse) pjp.getArgs()[1]; | ||
response.sendError(HttpServletResponse.SC_BAD_REQUEST); | ||
} | ||
} | ||
} |