SpringBoot-08 SpringSecurity
- 2021 年 4 月 2 日
- 筆記
- springboot
SpringBoot-08 SpringSecurity
創建了一個新項目,創建時選擇導入starter-web
1.環境搭建
1.1 導入thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.2 導入靜態資源
- css、js這樣的靜態資源導入到static文件夾下
- 前端頁面導入到templates文件夾下
如果需要靜態資源,可以私信我或者發郵件 [email protected]
1.3 關閉thymeleaf快取
spring.thymeleaf.cache=false
1.4 測試運行
2.用戶認證和授權
2.1 導入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.2 創建Config
創建一個config文件夾:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll() //對於主頁面都可以登錄
.antMatchers("/level1/**").hasRole("vip1") //對於level1文件夾下的頁面需要vip1才能登錄
.antMatchers("/level2/**").hasRole("vip2") //對於level2文件夾下的頁面需要vip2才能登錄
.antMatchers("/level3/**").hasRole("vip3"); //對於level3文件夾下的頁面需要vip3才能登錄
//如果沒有許可權,進入登錄頁面,這是Security內部自帶的
http.formLogin();
}
}
2.3 認證
就是給予下面這些用戶相應的許可權。
//認證
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zc").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("test").password(new BCryptPasswordEncoder().encode("123456")).roles("vip3");
}
大家可以自行測試。
3.註銷及許可權控制
3.1 註銷
1.開啟註銷功能
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll() //對於主頁面都可以登錄
.antMatchers("/level1/**").hasRole("vip1") //對於level1文件夾下的頁面需要vip1才能登錄
.antMatchers("/level2/**").hasRole("vip2") //對於level2文件夾下的頁面需要vip2才能登錄
.antMatchers("/level3/**").hasRole("vip3"); //對於level3文件夾下的頁面需要vip3才能登錄
//如果沒有許可權,進入登錄頁面,這是Security內部自帶的
http.formLogin();
//註銷功能
http.logout().logoutSuccessUrl("/");
}
2.添加註銷按鈕
<!--登錄註銷-->
<div class="right menu">
<!--未登錄-->
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登錄
</a>
<a class="item" th:href="@{/logout}">
<i class="address card icon"></i> 註銷
</a>
</div>
3.2 許可權控制
springboot 2.1.x版本以上不兼容這個標籤,最好使用2.0.7及其以下的
1.加入thymeleaf、springsecurity整合依賴
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
2.增加對應的頭部文件
xmlns:th="//www.thymeleaf.org"
xmlns:sec="//www.thymeleaf.org/thymeleaf-extras-springsecurity4">
3.修改前端頁面
<!--登錄註銷-->
<div class="right menu">
<!--未登錄-->
<div sec:authorize="!isAuthenticated()">
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登錄
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a class="item" >
用戶名:<span sec:authentication="name"></span>
</a>
<a class="item" th:href="@{/logout}">
<i class="address card icon"></i> 註銷
</a>
</div>
</div>
4.首頁訂製
4.1 登錄頁面
1.修改Config
http.formLogin().loginPage("/toLogin");
2.修改login頁面的路徑
<form th:action="@{/toLogin}" method="post">
如果想要自定義action,可以使用:
http.formLogin().loginPage("/toLogin").loginProcessingUrl("xxx");
如果前端form表單中的name與後端不一一對應,可以使用:
http.formLogin().loginPage("/toLogin").usernameParameter("xxx").passwordParameter("xxx");
4.2 記住我
1.前端添加記住我選框
<div class="field">
<input type="checkbox" name="remember">
</div>
2.修改Config
http.rememberMe().rememberMeParameter("remember");
個人部落格為:
MoYu’s HomePage
MoYu’s Gitee Blog