We normally create a filter chain in spring boot using the annotation below
` @Bean
public SecurityFilterChain securityFilterChainDefault(HttpSecurity http) throws Exception {
AuthClient authClient = new AuthClient();
authClient.setHosts(Arrays.asList("/NOT-APPLICABLE-ROUTE"));
authClient.setPaths(Arrays.asList("/NOT-APPLICABLE"));
http.securityMatcher(new HostRequestMatcher()
EndpointRequest.toLinks()).permitAll()
.anyRequest().authenticated())
.csrf().disable();
return http.build();
}`
We have a requirement where we want to create different filter chains dynamically at runtime programmatically instead of the build time @Bean annotation
For example we want to go a configuration of clients (each client will have it own client id) and bring a different filter chain per client (we have the PCKE flow where we want to pick different clients based on a host using the HostRequestMatcher)
For example we fetch a list of clients from configuration service which can change dynamically at runtime. So we poll the configuration and want to do something like below
`for(Client client : clients) {
HttpSecurity http = context.getBean(HttpSecurity.class);
context.registerBean(beanName, SecurityFilterChain.class, () -> {
try {
return getDynamicFilterChainBuilder(http, client);
catch (Exception e) {
throw new RuntimeException(e);
}
});
}
private DefaultSecurityFilterChain getDynamicFilterChainBuilder(HttpSecurity http, Client client) {
http.securityMatcher(new HostRequestMatcher(client))
.apply(ConfigUtils.getCustomConfigurer(client))
.and()
.addFilterAfter(new JWTFilter(cbxCookieUtil), AuthorizationFilter.class)
.authorizeHttpRequests(auth -> {
auth.requestMatchers(
EndpointRequest.to( "health"), EndpointRequest.toLinks()).permitAll();
auth.anyRequest().authenticated();
}).csrf().disable().headers()
return http.build();
}`
If this possible using spring boot and spring security ?
We are trying to see if we can build the filter chain dynamically instead of build time @Bean annotation
Trinoy Hazarika is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.