# 1. 编写配置类
```
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()// 自定义的登录页面
.loginPage("/login.html")// 登录页面
.loginProcessingUrl("/user/login") // 登录的路径地址 不需要自己写
.defaultSuccessUrl("/index")// 登录成功跳转的路径
// .usernameParameter("username") // 登录页面用户名参数名
// .passwordParameter("password") // 登录密码 参数名
// .failureForwardUrl("/sb") // 登录失败跳转页面 因为请求是post前端不会跳转
.failureUrl("/sb") // 登录失败跳转
.permitAll()// 允许操作
.and()
.authorizeRequests() // 授权
.antMatchers("/login.html", "/user/login", "/sb") // 设置那些路径可以直接访问 不需要认证
.permitAll() // 允许操作
.anyRequest() // 任何请求
.authenticated() // 任何请求 都需要认证
.and()
.csrf() // 跨域请求
.disable();// 关闭
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
```
# 2.编写页面
(在resources/static) 创建 login.html
```
<form action="/user/login" method="post">
用户名: <input type="text" name="username">
<br/>
密码: <input type="password" name="password">
<br/>
<input type="submit" value="登录">
</form>
```
