1.springSecurity的搭建
新建一個springboot的web項目,我這邊只選中了web,建立后如下:
pom依賴:
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
<!--配置支持jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--添加static和templates的依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<!-- 由於我使用的spring boot所以我是引入spring-boot-starter-security而且我使用了spring io所以不需要填寫依賴的版本號 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
以上的jsp依賴如果用不上可以不加哦
2.編寫SecurityConfiguration來繼承WebSecurityConfigurerAdapter
WebSecurityConfigurerAdapter是security中瀏覽器登錄設置的主類 這里我們繼承后重寫以下的三個方法:
- HttpSecurity(HTTP請求安全處理)
- AuthenticationManagerBuilder(身份驗證管理生成器)
- WebSecurity(WEB安全)
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
super.configure(auth);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/hello","/login.html").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
//指定登錄頁的路徑
.loginPage("/hello")
//指定自定義form表單請求的路徑
.loginProcessingUrl("/authentication/form")
.failureUrl("/login?error")
.defaultSuccessUrl("/success")
//必須允許所有用戶訪問我們的登錄頁(例如未驗證的用戶,否則驗證流程就會進入死循環)
//這個formLogin().permitAll()方法允許所有用戶基於表單登錄訪問/login這個page。
.permitAll();
//默認都會產生一個hiden標簽 里面有安全相關的驗證 防止請求偽造 這邊我們暫時不需要 可禁用掉
http .csrf().disable();
}
@Override
public void configure(WebSecurity web) throws Exception {
super.configure(web);
}
}
這邊我們指定的登錄頁的訪問方法為/Hello的方法,這邊我們來編寫這個Controller層:
@Controller
public class LoginController {
@RequestMapping("/hello")
public String hello() {
//這邊我們,默認是返到templates下的login.html
return "login";
}
}
login.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>第一個HTML頁面</title>
</head>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
自定義表單驗證:
<!--<form name="f" action="/login" method="post">-->
<form name="f" action="/authentication/form" method="post">
<br/>
用戶名:
<input type="text" name="username" placeholder="name"><br/>
密碼:
<input type="password" name="password" placeholder="password"><br/>
<input name="submit" type="submit" value="提交">
</form>
</body>
</html>
這里值的注意的是表單的用戶名name和password輸入框的name=""要和security里面的驗證的對應:
name="username";name="password",否則無法識別,另外action="/authentication/form"要與.loginProcessingUrl("/authentication/form")相對應,原因為:
由於security是由UsernamePasswordAuthenticationFilter這個類定義登錄的,里面默認是/login路徑,我們要讓他用我們的/authentication/form路徑,就需要配置.loginProcessingUrl("/authentication/form")
3.項目啟動
我們現在啟動項目 無論進入哪個網址都會被攔截返回到登錄頁面,如下所示:
這時我們用戶名:user(默認) password:會在啟動時候生成 如下:
這個時候我們登錄就成功了 ,否則不正確會返回到error頁面