Skip to content

Commit f9ff7e9

Browse files
author
Adam Ostrožlík
committed
Set RequestRejectedHandler via WebSecurity
1 parent de5bfa1 commit f9ff7e9

File tree

1 file changed

+21
-5
lines changed
  • config/src/main/java/org/springframework/security/config/annotation/web/builders

1 file changed

+21
-5
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/builders/WebSecurity.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@
5656
import org.springframework.web.filter.DelegatingFilterProxy;
5757

5858
import java.util.ArrayList;
59-
import java.util.Arrays;
60-
import java.util.Collection;
6159
import java.util.List;
6260

6361
/**
@@ -95,6 +93,8 @@ public final class WebSecurity extends AbstractConfiguredSecurityBuilder<Filter,
9593

9694
private HttpFirewall httpFirewall;
9795

96+
private RequestRejectedHandler requestRejectedHandler;
97+
9898
private boolean debugEnabled;
9999

100100
private WebInvocationPrivilegeEvaluator privilegeEvaluator;
@@ -266,6 +266,18 @@ public WebSecurity postBuildAction(Runnable postBuildAction) {
266266
return this;
267267
}
268268

269+
/**
270+
* Sets the handler to handle {@link org.springframework.security.web.firewall.RequestRejectedException}
271+
* @param requestRejectedHandler
272+
* @return the {@link WebSecurity} for further customizations
273+
* @since 5.7
274+
*/
275+
public WebSecurity requestRejectedHandler(RequestRejectedHandler requestRejectedHandler) {
276+
Assert.notNull(this.requestRejectedHandler, "requestRejectedHandlers cannot be null");
277+
this.requestRejectedHandler = requestRejectedHandler;
278+
return this;
279+
}
280+
269281
@Override
270282
protected Filter performBuild() throws Exception {
271283
Assert.state(!this.securityFilterChainBuilders.isEmpty(),
@@ -286,8 +298,8 @@ protected Filter performBuild() throws Exception {
286298
if (this.httpFirewall != null) {
287299
filterChainProxy.setFirewall(this.httpFirewall);
288300
}
289-
if (this.requestRejectedHandlers != null) {
290-
filterChainProxy.setRequestRejectedHandlers(this.requestRejectedHandlers);
301+
if (this.requestRejectedHandler != null) {
302+
filterChainProxy.setRequestRejectedHandler(this.requestRejectedHandler);
291303
}
292304
filterChainProxy.afterPropertiesSet();
293305

@@ -324,7 +336,11 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
324336
}
325337
catch (NoSuchBeanDefinitionException ex) {
326338
}
327-
this.requestRejectedHandlers = applicationContext.getBeansOfType(RequestRejectedHandler.class).values();
339+
try {
340+
this.requestRejectedHandler = applicationContext.getBean(RequestRejectedHandler.class);
341+
}
342+
catch (NoSuchBeanDefinitionException ex) {
343+
}
328344
}
329345

330346
/**

0 commit comments

Comments
 (0)