# 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>
```