1. 引入SpringSecurity;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.編寫SpringSecurity的配置類
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
}
3.控制請求的訪問權限(記住我、註銷、定製登錄頁);
後臺配置:
package com.atguigu.security.config;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// super.configure(http);
//定製請求的授權規則
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("VIP1")
.antMatchers("/level2/**").hasRole("VIP2")
.antMatchers("/level3/**").hasRole("VIP3");
//開啓自動配置的登陸功能,如果沒有登陸,沒有權限就會來到登陸界面
http.formLogin().usernameParameter("user").passwordParameter("pwd").loginPage("/userlogin");
//1、/login來到登錄頁
//2、重定向到/login?error表示登錄失敗
//3、更多詳細規定
//4、默認POST形式的/login代表處理登錄
//5、一旦定製loginPage:loginPage的post請求就是登錄
//開啓自動配置的註銷功能
http.logout().logoutSuccessUrl("/"); //註銷成功以後來到首頁
//1、訪問 /logout 表示用戶註銷,清空session
//2. 註銷成功會返回 /login?logout頁面
//開啓記住我功能
http.rememberMe().rememberMeParameter("remeber");
//登陸成功以後,將cookie發給瀏覽器保存,以後登錄帶上這個cookie,只要通過檢查就可以免登錄
//點擊註銷會刪除cookie
}
//定義認證規則
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//super.configure(auth);
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1", "VIP2")
.and()
.withUser("lisi").password(new BCryptPasswordEncoder().encode("123")).roles("VIP2", "VIP3")
.and()
.withUser("wangwu").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1", "VIP3");
}
}
前端登錄頁面:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">歡迎登陸武林祕籍管理系統</h1>
<hr>
<div align="center">
<form th:action="@{/userlogin}" method="post">
用戶名:<input name="user"/><br>
密碼:<input name="pwd"><br/>
<input type="checkbox" name="remeber"> 記住我 <br/>
<input type="submit" value="登陸">
</form>
</div>
</body>
</html>
前端主頁面:
基於spring2.x版本的,需導入springsecurity5的依賴和名稱空間,好像5的名稱空間沒有代碼提示
依賴:
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
<!--thymeleaf整合SpringSecurity的依賴導入-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">歡迎光臨武林祕籍管理系統</h1>
<div sec:authorize="!isAuthenticated()">
<h2 align="center">遊客您好,如果想查看武林祕籍 <a th:href="@{/userlogin}">請登錄</a></h2>
</div>
<div sec:authorize="isAuthenticated()">
<h2><span sec:authentication="name"></span>,您好,你的角色有
<span sec:authentication="principal.authorities"></span></h2>
<form th:action="@{/logout}" method="post">
<input type="submit" value="註銷">
</form>
</div>
<hr>
<div sec:authorize="hasRole('VIP1')">
<h3>普通武功祕籍</h3>
<ul>
<li><a th:href="@{/level1/1}">羅漢拳</a></li>
<li><a th:href="@{/level1/2}">武當長拳</a></li>
<li><a th:href="@{/level1/3}">全真劍法</a></li>
</ul>
</div>
<div sec:authorize="hasRole('VIP2')">
<h3>高級武功祕籍</h3>
<ul>
<li><a th:href="@{/level2/1}">太極拳</a></li>
<li><a th:href="@{/level2/2}">七傷拳</a></li>
<li><a th:href="@{/level2/3}">梯雲縱</a></li>
</ul>
</div>
<div sec:authorize="hasRole('VIP3')">
<h3>絕世武功祕籍</h3>
<ul>
<li><a th:href="@{/level3/1}">葵花寶典</a></li>
<li><a th:href="@{/level3/2}">龜派氣功</a></li>
<li><a th:href="@{/level3/3}">獨孤九劍</a></li>
</ul>
</div>
</body>
</html>