这篇文章主要讲解了“春季安全中的默认登录页源码分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“spring security中的默认登录页源码分析”吧!
springboot项目依赖
& lt; dependency> ,,& lt; groupId> org.springframework.boot ,,& lt; artifactId> spring-boot-starter-web & lt;/dependency> & lt; dependency> ,,& lt; groupId> org.springframework.boot ,,& lt; artifactId> spring-boot-starter-security & lt;/dependency>
在项目中随意编写一个接口,然后进行访问
@GetMapping (“/? public String  hello (), { ,,return “你好,,spring security"; }
在tomcat默认端口8080,localhost: 8080下访问该接口,spring security会帮我们将路径重定向到默认的登录页
那么这个默认页是怎么来的呢?
原来spring security有一个默认的WebSecurityConfigurerAdapter,发现其中有一个初始化方法,于是在这个方法打了断点,在应用启动的时候进行跟踪。
跟踪getHttp()方法,this.disableDefaults变量默认为false,意味着将会执行<代码> applyDefaultConfiguration (this.http); 代码>方法。查看<代码> applyDefaultConfiguration 代码>方法
public void init (WebSecurity 网络),throws Exception { ,,,//,首先配置安全要拦截的哪些http请求 ,,HttpSecurity http =, getHttp (); ,,web.addSecurityFilterChainBuilder (http) .postBuildAction ((),→, { ,,,,,FilterSecurityInterceptor securityInterceptor =, http.getSharedObject (FilterSecurityInterceptor.class); ,,,,,web.securityInterceptor (securityInterceptor); ,,}); } protected final  HttpSecurity getHttp (), throws Exception { if (this.http !=, null), { return this.http; } AuthenticationEventPublisher eventPublisher =, getAuthenticationEventPublisher (); this.localConfigureAuthenticationBldr.authenticationEventPublisher (eventPublisher); AuthenticationManager AuthenticationManager =, authenticationManager (); this.authenticationBuilder.parentAuthenticationManager (authenticationManager); Map, sharedObjects =, createSharedObjects (); 时间=this.http new HttpSecurity (this.objectPostProcessor, this.authenticationBuilder,, sharedObjects); if (! this.disableDefaults), { ,,,,,,,,,,,//,默认的配置将会走这个分支 applyDefaultConfiguration (this.http); ClassLoader ClassLoader =, this.context.getClassLoader (); List 查看<代码> applyDefaultConfiguration (this.http) 代码>方法,发现http对象新了一个<代码> DefaultLoginPageConfigurer> 代码对象属性,
private void applyDefaultConfiguration (HttpSecurity http), throws Exception { ,,http.csrf (); ,,http.addFilter (new WebAsyncManagerIntegrationFilter ()); ,,http.exceptionHandling (); ,,http.headers (); ,,http.sessionManagement (); ,,http.securityContext (); ,,http.requestCache (); ,,http.anonymous (); ,,http.servletApi (); ,,http.apply (new DefaultLoginPageConfigurer<祝辞()); ,,http.logout (); }查看<代码> DefaultLoginPageConfigurer> 代码类定义,发现它在初始化的同时,它也初始化了自己的2个私有成员变量,分别是<代码> DefaultLoginPageGeneratingFilter 代码>默认登录页面生成过滤器,<代码> DefaultLogoutPageGeneratingFilter 代码>默认登录页面过滤器,名字起得很好,见名知意,我们马山知道这两个类的含义。
查看<代码> DefaultLoginPageGeneratingFilter> 代码的类成员变量,发现定义了一系列跟登录有关的成员变量,包括登录,登录等路径,默认的登录页面路径是<代码>“/login" 代码>
public class DefaultLoginPageGeneratingFilter extends GenericFilterBean { ,,public static  final String DEFAULT_LOGIN_PAGE_URL =,“/login"; ,,public static  final String ERROR_PARAMETER_NAME =,“error"; ,,private String  loginPageUrl; ,,private String  logoutSuccessUrl; ,,private String  failureUrl; ,,private boolean  formLoginEnabled; ,,.....null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullspring security中的默认登录页源码分析