SpringBoot Security安裝配置及Thymeleaf整合
功能:解決web站點(diǎn)的登錄,權(quán)限驗(yàn)證,授權(quán)等功能
優(yōu)點(diǎn):在不影響站點(diǎn)業(yè)務(wù)代碼,可以權(quán)限的授權(quán)與驗(yàn)證橫切到業(yè)務(wù)中
1、要添加的依賴
<!--thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--security 和 thymeleaf 整合包--> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> <!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2、Security 下授權(quán)與驗(yàn)證的簡單配置(Security下有登錄,注銷,記住我等功能,可以快速集成到自己的login頁上)Tis:如果template頁中使用了 Frame頁,默認(rèn)是不能訪問的,需要添加 http.headers().frameOptions().sameOrigin();
@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter { //授權(quán) @Override protected void configure(HttpSecurity http) throws Exception { //請求授權(quán)的規(guī)則 http.authorizeRequests()//.antMatchers('/tologin').permitAll() //登錄頁所有人都可以訪問//.antMatchers('/admin/**').hasRole('admin1').antMatchers('/admin/list').hasRole('admin1').antMatchers('/admin/role').hasRole('admin1').antMatchers('/admin/cate').hasRole('admin2').antMatchers('/admin/rule').hasRole('admin2'); // 項(xiàng)目里面使用了springSecurity spring Security下,X-Frame-Options默認(rèn)為DENY,非spring Security環(huán)境下,X-Frame-Options的默認(rèn)大多也是DENY,這種情況下,瀏覽器拒絕當(dāng)前頁面加載任何Frame頁面 http.headers().frameOptions().sameOrigin(); //登錄頁(Security默認(rèn)有一個(gè)登錄頁) http.formLogin().permitAll().loginPage('/tologin') //指定自定義的登錄頁地址.successForwardUrl('/admin/index') //登錄成功跳轉(zhuǎn)地址.usernameParameter('username').passwordParameter('password');//匹配自定義登錄頁的name元素名稱 // 開啟注銷功能,跳轉(zhuǎn)到登錄頁 http.csrf().disable(); //退出失敗可能能的原因 http.logout().logoutSuccessUrl('/tologin'); //開啟記住我功能,cookie 默認(rèn)保存14天 http.rememberMe().rememberMeParameter('remember');//匹配自定義登錄頁的name元素名稱 } //認(rèn)證 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())//密碼加密方式(有些版本的Security必須要指定).withUser('root').password(new BCryptPasswordEncoder().encode('123')).roles('admin1','admin2','admin3').and().withUser('yeqiu').password(new BCryptPasswordEncoder().encode('123')).roles('admin1').and().withUser('admin').password(new BCryptPasswordEncoder().encode('123')).roles('admin2'); }}
3、Security 和 Thymeleaf 頁面整合(添加依賴:thymeleaf-extras-springsecurity)
<!--加入約束--><html xmlns:th='http://www.thymeleaf.org' xmlns:sec='http://www.thymeleaf.org/extras/spring-security'><!-- sec:authorize='isAuthenticated()' 用戶是否登錄 sec:authorize='hasAnyRole(’admin1’)' 是否具有某個(gè)角色 sec:authentication='name' 當(dāng)前登錄用戶 sec:authentication='principal.authorities' 當(dāng)前用戶全部角色--><div sec:authorize='isAuthenticated()'> <h2><span sec:authentication='name'></span>,您好 您的身份是 <span sec:authentication='principal.authorities'></span> </h2></div><li sec:authorize='hasRole(’admin2’)'> <a onclick='xadmin.add_tab(’權(quán)限管理’,’admin/rule’)'> <cite>權(quán)限管理</cite> </a></li>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. el-input無法輸入的問題和表單驗(yàn)證失敗問題解決2. 不要在HTML中濫用div3. react腳手架配置代理的實(shí)現(xiàn)4. JavaScript中顏色模型的基礎(chǔ)知識與應(yīng)用詳解5. XML入門的常見問題(三)6. JavaScript快速實(shí)現(xiàn)一個(gè)顏色選擇器7. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)8. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂代碼9. Jquery使用原生AJAX方法請求數(shù)據(jù)10. React實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)hook組件實(shí)戰(zhàn)示例
