堆疊條形圖
阿新 • • 發佈:2020-12-15
技術標籤:SpringBoot
簡介
Spring Security是針對Spring專案的安全框架,也是SpringBoot底層安全模組的預設技術選型,他可以實現強大的Web安全控制,對於安全控制,我們僅需要引入spring-boot-start-security模組,進行少量的配置,即可實現強大的安全管理
記住幾個類:
WebSecurityConfigurerAdapter:自定義Security策略
AuthenticationManagerBuilder:自定義認證策略
@EnableWebSecurity:開啟WebSecurity模式
SpringSecurity的兩個主要目標是“認證”和“授權”(訪問控制)
簡單使用
首先在pom.xml檔案中匯入如下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
在專案中建立config資料夾和config類並繼承需要用到的類
下面是從記憶體中授權:
@EnableWebSecurity public class Security extends WebSecurityConfigurerAdapter { //授權 @Override protected void configure(HttpSecurity http) throws Exception { //所有人可以訪問,功能只有對應許可權的人才能訪問 //請求授權的規則 http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); //沒有許可權預設會到登入頁面 //登入 http.formLogin(); //spring預設開啟防止網站攻擊,這裡先關閉 http.csrf().disable();//關閉csrf功能 //登出 http.logout().logoutSuccessUrl("/");//該程式碼需要配合前端的一個登出按鈕使用,登出按鈕需要跳轉的url為"/logout" } //認證 //密碼編碼: PasswordEncoder //在springsecurity5.0 + 中增加了很多加密方法 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("zhouyi").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3") .and() .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3") .and() .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1"); } }
要從資料庫認證程式碼就用:auth.jdbcAuthentication().xxx.xxx
整合thymeleaf
pom.xml檔案匯入如下依賴:
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> <version>3.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
在html檔案中加入名稱空間並使用
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<!--使用-->
<!--未登入-->
<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>
角色:<span sec:authentication="principal.getAuthorities()"></span>
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a class="item" th:href="@{/logout}">
<i class="address card icon"></i> 登出
</a>
</div>
可能頁面顯示用不了,那麼就應該是版本問題,新版不匹配
可以把spring降級或者該依賴為如下即可:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
在想被不同許可權的人才能看到的模組上面新增這個就能實現不同許可權看到不同的模組:
<div class="column" sec:authorize="hasRole('vip1')"></div>