??wei_shuo的個(gè)人主頁
??wei_shuo的學(xué)習(xí)社區(qū)
??Hello World !
Security
Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架;提供一組可以在Spring應(yīng)用上下文中配置的Bean,充分利用Spring IoC,DI(控制反轉(zhuǎn)Inversion of Control ,DI:Dependency Injection 依賴注入)和AOP(面向切面編程)功能,為應(yīng)用系統(tǒng)提供聲明式的安全訪問控制功能,減少了為企業(yè)系統(tǒng)安全控制編寫大量重復(fù)代碼的工作;
認(rèn)證和授權(quán)
- 依賴導(dǎo)入
<!--security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
- 認(rèn)證和授權(quán)config/SecurityConfig.java
thymeleafpackage com.wei.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 SecurityConfig extends WebSecurityConfigurerAdapter { //授權(quán) @Override protected void configure(HttpSecurity http) throws Exception { //首頁所有人可以訪問,功能業(yè)只有有對應(yīng)權(quán)限的人才能訪問 //請求授權(quán)規(guī)則 http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); //沒有權(quán)限默認(rèn)跳到登錄頁面 http.formLogin(); } //認(rèn)證 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("weishuo").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3") .and() .withUser("root").password(new BCryptPasswordthymeleafEncoder().encode("123456")).roles("vip1","vip2","vip3") .and() .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1"); } }
注銷和權(quán)限控制
- Config/SecurityConfig.java
//開啟注銷功能,注銷成功跳轉(zhuǎn)到首頁 http.csrf().disable(); //關(guān)閉csrf功能 http.logout().logoutSuccessUrl("/");
security-thymeleaf整合
- 導(dǎo)入依賴
<!--security-thymeleaf整合包--> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> <version>3.1.1.RELEASE</version> </dependency>
- index.html:命名空間導(dǎo)入
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
- index.html:配置登錄、未登錄顯示,隱藏
<!--登錄、注銷--> <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> <!--獲取用戶權(quán)限--> 角色:<span sec:authentication="principal.authorities"></span> </a> </div> <div sec:authorize="isAuthenticated()"> <a class="item" th:href="@{/logout}"> <i class="sign-out icon"></i>注銷 </a> </div> </div>
- index.html:配置角色頁面動(dòng)態(tài)顯示
<div class="column" sec:authorize="hasRole('vip1')">……</div> <div class="column" sec:authorize="hasRole('vip2')">……</div> <div class="column" sec:authorize="hasRole('vip3')">……</div>
Remember me
- Config/SecurityConfig.java
//記住我功能,自定義接受參數(shù) http.rememberMe().rememberMeParameter("remember");
首頁定制
沒有權(quán)限默認(rèn)跳到登錄頁面,登錄頁定制
.loginPage("/toLogin")
:將用戶重定向到指定的登錄頁面進(jìn)行登錄;.usernameParameter("username")
:從用戶提交的表單中獲取用戶名時(shí)使用的參數(shù)名;.passwordParameter("password")
:從用戶提交的表單中獲取密碼時(shí)使用的參數(shù)名;.loginProcessingUrl("/login")
:指定表單登錄提交的 URL,用于處理用戶提交的登錄表單數(shù)據(jù)//沒有權(quán)限默認(rèn)跳到登錄頁面,登錄頁定制 http.formLogin().loginPage("/toLogin").usernameParameter("username").passwordParameter("password") .loginProcessingUrl("/login");
?? 結(jié)語:創(chuàng)作不易,如果覺得博主的文章賞心悅目,還請——
點(diǎn)贊
??收藏
??評論
??文章來源:http://www.zghlxwxcb.cn/news/detail-541379.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-541379.html
到了這里,關(guān)于SpringBoot原理分析 | 安全框架:Security的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!