一、需求
在上一章 “SpringSecurity之HttpBasic登录验证” 的基础之上,完成自定义 登录页面 和 权限鉴定 的功能
二、新增SpringSecurity的配置类,修改默认配置
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 此方法做了3件事:
* 1、关闭csrf跨站攻击防御
* 2、设置使用表单登录验证模式
* 3、设置鉴权相关信息(即访问哪些资源需要什么样的角色或权限)
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable() // 禁用 csrf跨站攻击防御
.formLogin()
.loginPage("/login.html") //设置登录页面
.loginProcessingUrl("/login") // 设置登录请求,即登录表单填的 <form action="/login" method="post">
.and()
.authorizeRequests()
.antMatchers("/login.html","/login").permitAll() // 放行的请求,即不需要登录认证就可以访问的资源