# 第一步 配置視圖
~~~
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.formLogin()//設置自定義的登錄頁面
.loginPage("/login.html")//登錄頁面設置
.loginProcessingUrl("/user/login")//登錄訪問的路徑Security 的
.defaultSuccessUrl("/edu").permitAll() //登錄成功,跳轉路徑
.and().authorizeRequests()//設置被保護的路徑
.antMatchers("/").permitAll()//設置不需要驗證的路徑
.anyRequest().authenticated()//設置所有路徑都可以訪問
.and().csrf().disable();//關閉csrf防護
}
}
~~~
# 第二步編寫試圖
~~~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/user/login" method="post">
用戶名:
<input type="text" name="username">
<br>
密碼:
<input type="text" name="password">
<br>
<input type="submit" value="登錄">
</form>
</body>
</html>
~~~
