spring-security icon indicating copy to clipboard operation
spring-security copied to clipboard

Support ServerExchangeRejectedHandler @Bean

Open rwinch opened this issue 1 year ago • 0 comments

Spring Security does not use the ServerExchangeRejectedHandler Bean when exposed.

We should fix this, but in the meantime users can leverage a BeanPostProcessor approach.

@Bean
BeanPostProcessor beanPostProcessor() {
	return new BeanPostProcessor() {
		@Override
		public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
			if (bean instanceof WebFilterChainProxy) {
				WebFilterChainProxy springSecurity = (WebFilterChainProxy) bean;
				springSecurity.setExchangeRejectedHandler((exchange, ex) -> Mono.fromRunnable(() -> exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE)));
			}
			return bean;
		}
	};
}

rwinch avatar Oct 22 '24 23:10 rwinch