Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the construction process of SecurityFilterChain?

Shulou Source: shulou.com Published: 2022-06-02 23:53:21 09月27日 Update

This article introduces the relevant knowledge of "how is the construction process of SecurityFilterChain". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The core Bean that Spring Security embeds into Servlet is a filter called springSecurityFilterChain. This filter is the core entry of Spring Security.

No matter what kind of Web application, Spring Security obtains "springSecurityFilterChain" from IOC. There is only one registration entry for this Bean. In the class WebSecurityConfiguration, the configuration object is opened by the annotation @ EnableWebSecurity.

@ Bean (name = AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME) public Filter springSecurityFilterChain () throws Exception {... return webSecurity.build ();}

Builder mode: WebSecurity, built product is: Filter, specific type is FilterChainProxy

Public final class WebSecurity extends AbstractConfiguredSecurityBuilder implements SecurityBuilder, ApplicationContextAware {...}

Template method mode: AbstractConfiguredSecurityBuilder. The template method is the builder's build method. After templating, the doBuild method is called. DoBuild is still a template method.

Overrideprotected final O doBuild () throws Exception {synchronized (configurers) {buildState = BuildState.INITIALIZING; / /-initialization-beforeInit (); init (); buildState = BuildState.CONFIGURING; / /-configuration properties-beforeConfigure (); configure (); buildState = BuildState.BUILDING; / /-built products-O result = performBuild (); buildState = BuildState.BUILT Return result;}}

The core construction method, the performBuild method, is implemented in the subclass, where the build class is WebSecurity

@ Overrideprotected Filter performBuild () throws Exception {/ / chain length int chainSize = ignoredRequests.size () + securityFilterChainBuilders.size (); / / Construction of SecurityFilterChain List securityFilterChains = new ArrayList (chainSize); / / part I: from requests that do not require security: ignoredRequest for (RequestMatcher ignoredRequest: ignoredRequests) {securityFilterChains.add (new DefaultSecurityFilterChain (ignoredRequest)); part II: from requests for security: securityFilterChainBuilders for (SecurityBuilder)

Tags: Configuration function method security source information filter authentication processing rules that is core path protection login procedure entry decision instance object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Apple Xiaomi Microsoft macOS