(1)初步配置自定义表单登录页
首先覆盖config方法
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .loginPage("/myLogin.html") // 使登录页不设限访问 .permitAll() .and() .csrf().disable(); } }
(2)认识HttpSecurity
HttpSecurity 实际上对应了 =Spring Security 命名空间配置方式中 XML 文件内的标签,允许我们为特定的 HTTP 请求配置安全策略。
在 XML 文件中,声明大量配置早已司空见惯;但在 Java 配置中,按照传统的方式,我们需要这样来调用。
HttpSecurity 首先被设计为链式调用,在执行每个方法后,都会返回一个预期的上下文,便于连续调用。我们不需要关心每个方法究竟返回了什么、如何进行下一个配置等细节。
HttpSecurity 提供了很多配置相