Springboot安全框架整合SpringSecurity實(shí)現(xiàn)方式
1.工業(yè)級(jí)安全框架介紹
Spring Security基于Spring開發(fā),項(xiàng)目中如果使用Spring作為基礎(chǔ),配合Spring Security做權(quán)限更加方便,而Shiro需要和Spring進(jìn)行整合開發(fā)。因此作為spring全家桶中的Spring Security在java領(lǐng)域很常用。
2.建議搭建Spring Security環(huán)境
2.1在pom.xml中添加相關(guān)依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>springsecurityReview</artifactId> <version>1.0-SNAPSHOT</version> <parent> <artifactId>spring-boot-dependencies</artifactId> <groupId>org.springframework.boot</groupId> <version>2.5.4</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> </project>
2.2創(chuàng)建Handler類
package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class Handler { @GetMapping("/index") public String index(){ return "index"; } }
2.3創(chuàng)建簡(jiǎn)單的html和配置相關(guān)thymeleaf的路徑
2.4最后再加個(gè)啟動(dòng)類,那么我們的整合測(cè)試就完成勒
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
2.5成果展示 用戶名默認(rèn)user,密碼則隨機(jī)生成的這串?dāng)?shù)字
3.進(jìn)階版使用
3.1用戶名和密碼自定義
3.2在config包下創(chuàng)建Encoder進(jìn)行密碼的校驗(yàn)和轉(zhuǎn)碼操作
將密碼轉(zhuǎn)成字符串形式,并通過match方法驚醒校驗(yàn)。
3.3賦予賬號(hào)角色權(quán)限
package com.example.config; import org.springframework.context.annotation.Configuration; 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.WebSecurityConfigurerAdapter; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { //角色和資源的關(guān)系 @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/admin").hasRole("ADMIN") .antMatchers("/index").access("hasRole('ADMIN') or hasRole('USER') ") .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll() .and() .csrf() .disable(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()) .withUser("user").password(new MyPasswordEncoder() .encode("000")).roles("USER") .and() .withUser("admin").password(new MyPasswordEncoder() .encode("123")).roles("ADMIN","USER"); } }
最后達(dá)到admin賬號(hào)能訪問admin.html和index.html
user只能訪問index.html的操作
以上就是Springboot安全框架整合SpringSecurity實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于Springboot整合SpringSecurity的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
flowable動(dòng)態(tài)創(chuàng)建多級(jí)流程模板實(shí)現(xiàn)demo
這篇文章主要為大家介紹了flowable動(dòng)態(tài)創(chuàng)建多級(jí)流程模板實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05利用java實(shí)現(xiàn)中獎(jiǎng)概率詳情
這篇文章主要介紹了利用java實(shí)現(xiàn)中獎(jiǎng)概率詳情,根據(jù)概率將獎(jiǎng)品劃分區(qū)間,每個(gè)區(qū)間代表一個(gè)獎(jiǎng)品,然后抽取???隨機(jī)數(shù)??,反查落在那個(gè)區(qū)間上,即為所抽取的獎(jiǎng)品,需要的朋友可以參考一下2022-07-07java數(shù)據(jù)結(jié)構(gòu)與算法之奇偶排序算法完整示例
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之奇偶排序算法,較為詳細(xì)的分析了奇偶算法的原理并結(jié)合完整示例形式給出了實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08使用maven?shade插件解決項(xiàng)目版本沖突詳解
這篇文章主要為大家介紹了使用maven?shade插件解決項(xiàng)目版本沖突詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09hashMap擴(kuò)容時(shí)應(yīng)該注意這些死循環(huán)問題
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著hashMap擴(kuò)容時(shí)的死循環(huán)問題展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06skywalking分布式服務(wù)調(diào)用鏈路追蹤APM應(yīng)用監(jiān)控
這篇文章主要為大家介紹了skywalking分布式服務(wù)調(diào)用鏈路追蹤APM應(yīng)用監(jiān)控的功能使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03Spring事件監(jiān)聽器@EventListener與publishEvent的使用
Spring可以通過事件監(jiān)聽器機(jī)制來處理應(yīng)用程序中的事件,本文主要介紹了Spring事件監(jiān)聽器@EventListener與publishEvent的使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06Java類加載之Class對(duì)象到Klass模型詳解
這篇文章主要介紹了Java類加載之Class對(duì)象到Klass模型詳解,每一個(gè)Java類在JVM中都會(huì)對(duì)應(yīng)創(chuàng)建一個(gè)C++類實(shí)例,我們稱這個(gè)C++類為Klass實(shí)例,Klass實(shí)例里面存儲(chǔ)了java類中所描述的方法、字段、屬性等,需要的朋友可以參考下2023-08-08