![4e07ceab5fbf9fb30e185b7b0187c1c2.png](https://i-blog.csdnimg.cn/blog_migrate/5364b57c37283993ec7a114229fa5c0e.jpeg)
当进行登录时会执行
UsernamePasswordAuthenticationFilter 过滤器。
usernamePasrameter:账户参数名
passwordParameter:密码参数名
postOnly=true:默认情况下只允许 POST 请求。
默认情况下 登录名/密码和默认请求
![438c58d6f003e1f33f7cc01056d802ed.png](https://i-blog.csdnimg.cn/blog_migrate/5fd2a45646b1a32dd50589ae8727a96b.jpeg)
表单也必须是同名
![ddef740c1526c20c05b2c9844a29257d.png](https://i-blog.csdnimg.cn/blog_migrate/173e173cd1cb9f72a1e8f2962c61be90.png)
如果要修改默认的请求参数,就需要这么做
1.修改配置类
![549defc42dc191a7fdf768ac6e148f1d.png](https://i-blog.csdnimg.cn/blog_migrate/9058d8d6c39131881de5050431ff3d24.jpeg)
2.修改表单name属性
![73ab3140c4c5ed9fbfc88e9ed4d1a209.png](https://i-blog.csdnimg.cn/blog_migrate/2988b5ca33f9b9074a3a2dc3317a6554.png)
package com.lin.springsecurity.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* @Author ccgg
* @Date 2020/8/18 22:56
* 创建 PasswordEncoder 实例,在spring启动时,注入容器中
*/
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
System.out.println(1);
//表单认证
http.formLogin()
//当发现login时认为是登录需要执行我们自定义的登录逻辑 >里面的url是登录页面表单的提交地址
.loginProcessingUrl("/login")
//登录成功后请求地址 请求方法必须是post的 这里是跳转控制器
.successForwardUrl("/toMain")
//登录失败后请求访问的地址 >这里访问的是控制器
.failureForwardUrl("/failLogin")
//设置登录页面
.loginPage("/login.html")
.usernameParameter("uuname")
.passwordParameter("ppword")
;
//url拦截认证 >所有请求都必须被认证 必须登录后才可以访问
http.authorizeRequests()
//设置不需要拦截的页面
.antMatchers("/login.html").permitAll()
.antMatchers("/fail.html").permitAll()
//所有请求都必须被认真,必须登录后才能访问
.anyRequest().authenticated();
//关闭csrf防护 >只有关闭了,来自表单的请求
http.csrf().disable();
}
/**
* 创建加密对象
* @return
*/
@Bean
public PasswordEncoder getPE(){
return new BCryptPasswordEncoder();
}
}