Spring Boot 整合 Shiro+Thymeleaf過(guò)程解析
這篇文章主要介紹了Spring Boot 整合 Shiro+Thymeleaf過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1.導(dǎo)包
<!-- springboot 與 shiro 的集成--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.1</version> </dependency> <!-- thymeleaf 與 shiro 集成--> <dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>thymeleaf-extras-shiro</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
2. 編寫配置類
@Configuration
@ConfigurationProperties(prefix = "shiro")
@Data
public class ShiroConfig {
private String loginUrl;
private String unauthorizedUrl;
private String successUrl;
private String logoutUrl;
private String[] anons;
private String[] authcs;
/**
* 配置securityManager
* @param userRealm
* @return
*/
@Bean
public SecurityManager securityManager(UserRealm userRealm){
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(userRealm);
return securityManager;
}
/**
* 配置shiroFilter
* @param securityManager
* @return
*/
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
shiroFilterFactoryBean.setLoginUrl(loginUrl);
shiroFilterFactoryBean.setUnauthorizedUrl(unauthorizedUrl);
shiroFilterFactoryBean.setSuccessUrl(successUrl);
Map<String,String> filterMap = new HashMap<>();
if(null != logoutUrl){
filterMap.put(loginUrl,"logout");
}
if(anons!=null && anons.length>0){
for(String anon:anons){
filterMap.put(anon,"anon");
}
}
if(authcs!=null && authcs.length>0){
for(String authc:authcs){
filterMap.put(authc,"authc");
}
}
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
return shiroFilterFactoryBean;
}
/**
* 配置自定義Realm
* @return
*/
@Bean
public UserRealm userRealm(CredentialsMatcher credentialsMatcher){
UserRealm userRealm = new UserRealm();
userRealm.setCredentialsMatcher(credentialsMatcher);
return userRealm;
}
/**
* 配置憑證匹配器
* @return
*/
@Bean
public HashedCredentialsMatcher hashedCredentialsMatcher(){
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
hashedCredentialsMatcher.setHashAlgorithmName("MD5");
hashedCredentialsMatcher.setHashIterations(10);
return hashedCredentialsMatcher;
}
/**
* 配置ShiroDialect,用于Thymeleaf和shiro標(biāo)簽的使用
* @return
*/
@Bean
public ShiroDialect shiroDialect(){
return new ShiroDialect();
}
}
3. application.yml 配置 攔截鏈
# shiro shiro: login-url: /login.html anons: - /login.html - /index.html - doLogin authcs: - /**
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java如何用遞歸生成樹(shù)形結(jié)構(gòu)
作者分享了自己在使用腳本之家資源進(jìn)行編程時(shí)的經(jīng)驗(yàn),包括準(zhǔn)備實(shí)體對(duì)象、測(cè)試數(shù)據(jù)、構(gòu)造樹(shù)形結(jié)構(gòu)遞歸函數(shù)、測(cè)試以及輸出結(jié)果等步驟,作者希望這些經(jīng)驗(yàn)?zāi)軐?duì)大家有所幫助,并鼓勵(lì)大家支持腳本之家2025-03-03
SpringSecurity集成第三方登錄過(guò)程詳解(最新推薦)
在ThirdAuthenticationFilter 類的attemptAuthentication()方法中,我們通過(guò)authType類型,然后創(chuàng)建對(duì)應(yīng)的Authentication實(shí)現(xiàn)來(lái)實(shí)現(xiàn)不同方式的登錄,下面給大家分享SpringSecurity集成第三方登錄過(guò)程,感興趣的朋友一起看看吧2024-05-05
SpringSecurity實(shí)現(xiàn)權(quán)限認(rèn)證與授權(quán)的使用示例
本文主要介紹了SpringSecurity實(shí)現(xiàn)權(quán)限認(rèn)證與授權(quán)的使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
Java實(shí)現(xiàn)排球比賽計(jì)分系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)排球比賽計(jì)分系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
SpringSecurity+jwt+redis基于數(shù)據(jù)庫(kù)登錄認(rèn)證的實(shí)現(xiàn)
本文主要介紹了SpringSecurity+jwt+redis基于數(shù)據(jù)庫(kù)登錄認(rèn)證的實(shí)現(xiàn),其中也涉及到自定義的過(guò)濾器和處理器,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
Java使用正則表達(dá)式進(jìn)行匹配且對(duì)匹配結(jié)果逐個(gè)替換
這篇文章主要介紹了Java使用正則表達(dá)式進(jìn)行匹配且對(duì)匹配結(jié)果逐個(gè)替換,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Java實(shí)現(xiàn)中國(guó)象棋的示例代碼
中國(guó)象棋是起源于中國(guó)的一種棋,屬于二人對(duì)抗性游戲的一種,在中國(guó)有著悠久的歷史。由于用具簡(jiǎn)單,趣味性強(qiáng),成為流行極為廣泛的棋藝活動(dòng)。本文將利用Java實(shí)現(xiàn)這一經(jīng)典游戲,需要的可以參考一下2022-02-02

