spring boot 整合shiro詳細教程
我們開發時候有時候要把傳統spring shiro轉成spring boot專案,或者直接整合,那麼我們要搞清楚一個知識,就是 xml配置和spring bean程式碼配置的關係,這一點很重要,因為spring boot是沒有xml配置檔案的(也不絕對,spring boot也是可以引用xml配置的)
引入依賴:
<dependency> <artifactId>ehcache-core</artifactId> <groupId>net.sf.ehcache</groupId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.3</version> </dependency>
如果你出現錯誤 找不到slf4j,引入依賴
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
傳統xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.len"/> <!--定義realm--> <bean id="myRealm" class="com.len.core.shiro.LoginRealm"> <property name="credentialsMatcher" ref="credentialsMatcher"/> </bean> <bean id="permissionFilter" class="com.len.core.filter.PermissionFilter"/> <!--目前去掉自定義攔截驗證 由個人處理登入業務--> <!--<bean id="customAdvicFilter" class="com.len.core.filter.CustomAdvicFilter"> <property name="failureKeyAttribute" value="shiroLoginFailure"/> </bean>--> <bean id="verfityCodeFilter" class="com.len.core.filter.VerfityCodeFilter"> <property name="failureKeyAttribute" value="shiroLoginFailure"/> <property name="jcaptchaParam" value="code"/> <property name="verfitiCode" value="true"/> </bean> <!-- Shiro過濾器 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/login"/> <property name="unauthorizedUrl" value="/goLogin" /> <property name="filters"> <map> <entry key="per" value-ref="permissionFilter"/> <entry key="verCode" value-ref="verfityCodeFilter"/> <!--<entry key="main" value-ref="customAdvicFilter"/>--> </map> </property> <property name="filterChainDefinitions"> <value> <!-- /** = anon所有url都可以匿名訪問 --> /login = verCode,anon /getCode = anon /logout = logout /plugin/** = anon <!-- /** = authc 所有url都必須認證通過才可以訪問--> /user/**=per <!-- /login = main--> /** = authc </value> </property> </bean> <!--安全管理器--> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="myRealm"/> <property name="cacheManager" ref="cacheManager" /> <!--<property name="rememberMeManager" ref="rememberMeManager" />--> </bean> <!-- 憑證匹配器 --> <bean id="credentialsMatcher" class="com.len.core.shiro.RetryLimitCredentialsMatcher"> <constructor-arg index="0" ref="cacheManager"/> <constructor-arg index="1" value="5"/> <property name="hashAlgorithmName" value="md5"/> <property name="hashIterations" value="4"/> </bean> <!--快取--> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache.xml"/> </bean> <!--開啟註解--> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager" /> </bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> </beans>
轉換成bean 新建類ShiroConfig
@Configuration public class ShiroConfig { //自定義密碼 @Bean public RetryLimitCredentialsMatcher getRetryLimitCredentialsMatcher(){ RetryLimitCredentialsMatcher rm=new RetryLimitCredentialsMatcher(getCacheManager(),"5"); rm.setHashAlgorithmName("md5"); rm.setHashIterations(4); return rm; }
@Bean
public LoginRealm getLoginRealm(){
LoginRealm realm= new LoginRealm();
realm.setCredentialsMatcher(getRetryLimitCredentialsMatcher());
return realm;
}
@Bean
public EhCacheManager getCacheManager(){
EhCacheManager ehCacheManager=new EhCacheManager();
ehCacheManager.setCacheManagerConfigFile("classpath:ehcache/ehcache.xml");
return ehCacheManager;
}
@Bean
public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
//會話
@Bean(name="securityManager")
public DefaultWebSecurityManager getDefaultWebSecurityManager(){
DefaultWebSecurityManager dwm=new DefaultWebSecurityManager();
dwm.setRealm(getLoginRealm());
dwm.setCacheManager(getCacheManager());
return dwm;
}
@Bean
public PermissionFilter getPermissionFilter(){
PermissionFilter pf=new PermissionFilter();
return pf;
}
@Bean
public VerfityCodeFilter getVerfityCodeFilter(){
VerfityCodeFilter vf= new VerfityCodeFilter();
vf.setFailureKeyAttribute("shiroLoginFailure");
vf.setJcaptchaParam("code");
vf.setVerfitiCode(true);
return vf;
}
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean getShiroFilterFactoryBean(){
ShiroFilterFactoryBean sfb = new ShiroFilterFactoryBean();
sfb.setSecurityManager(getDefaultWebSecurityManager());
sfb.setLoginUrl("/login");
sfb.setUnauthorizedUrl("/goLogin");
Map<String, Filter> filters=new HashMap<>();
filters.put("per",getPermissionFilter());
filters.put("verCode",getVerfityCodeFilter());
sfb.setFilters(filters);
Map<String, String> filterMap = new LinkedHashMap<>();
filterMap.put("/login","verCode,anon");
//filterMap.put("/login","anon");
filterMap.put("/getCode","anon");
filterMap.put("/logout","logout");
filterMap.put("/plugin/**","anon");
filterMap.put("/user/**","per");
filterMap.put("/**","authc");
sfb.setFilterChainDefinitionMap(filterMap);
return sfb;
}
@Bean
public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
advisorAutoProxyCreator.setProxyTargetClass(true);
return advisorAutoProxyCreator;
}
@Bean
public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(){
AuthorizationAttributeSourceAdvisor as=new AuthorizationAttributeSourceAdvisor();
as.setSecurityManager(getDefaultWebSecurityManager());
return as;
}
@Bean
public FilterRegistrationBean delegatingFilterProxy(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
DelegatingFilterProxy proxy = new DelegatingFilterProxy();
proxy.setTargetFilterLifecycle(true);
proxy.setTargetBeanName("shiroFilter");
filterRegistrationBean.setFilter(proxy);
return filterRegistrationBean;
}
其中有個別類是自定義的攔截器和 realm,此時spring 即能注入shiro 也就是 spring boot整合上了 shiro
如果你因為其他配置引起一些失敗,可以參考開源專案 lenos快速開發腳手架 spring boot版本,其中有對shiro的整合,可以用來學習
相關推薦
spring boot 整合shiro詳細教程
我們開發時候有時候要把傳統spring shiro轉成spring boot專案,或者直接整合,那麼我們要搞清楚一個知識,就是 xml配置和spring bean程式碼配置的關係,這一點很重要,因為spring boot是沒有xml配置檔案的(也不絕對,spring boot
Spring boot整合shiro使用Ajax方式,最詳細教程
最近一直在自己的個人專案中整合進shiro這個許可權控制框架,踩了不少的坑,sb(允許我這麼叫他把,方便簡潔)整合shiro的教程不少,但是使用ajax方式的還真的不是很多,下面把我自己的經驗分享給大家。 1、在pom中加入shiro的包 <
spring boot整合shiro 簡單許可權控制
package me.config; import javax.annotation.Resource; import me.domain.entity.CmsUser; import me.service.UserService; import me.utils.MD5Util
Spring Boot 整合 Shiro實現許可權控制,親測可用,附帶sql
前提: 本文主要講解Spring Boot 與 Shiro的整合 與許可權控制的實現方式(主要以程式碼實現功能為主),主要用到的技術Spring Boot+Shiro+Jpa(通過Maven構建),並不會涉及到Shiro框架的原始碼分析 如果有想要學習Shiro框架的小夥伴可以去http://shiro.
spring-boot整合shiro
概述 許可權體系在現代任何IT系統中都是很基礎但又非常重要的部分,無論是傳統MIS系統還是網際網路系統,出於保護業務資料和應用自身的安全,都會設計自己的授權鑑權策略。 最近專案中也需要用到許可權驗證功能,專案為spring-boot工程,現成的許可權驗證框架有shiro和spri
Spring Boot整合shiro
shiro面世已經有很長時間了,相比Spring security更加精簡一些,也更容易上手,因此就想分享下這段時間的學習成果,就當是學習筆記了1 pom檔案如下<project xmlns="http://maven.apache.org/POM/4.0.0" xml
spring boot 整合shiro(使用者授權和許可權控制)
(1) pom.xml中新增Shiro依賴 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId>
spring boot整合Shiro實現單點登入
前面的部落格中,我們說道了Shiro的兩個最大的特點,認證和授權,而單點登入也是屬於認證的一部分,預設情況下,Shiro已經為我們實現了和Cas的整合,我們加入整合的一些配置就ok了。 1、加入shiro-cas包 <!-- shiro整合cas單點 -->
Spring Boot整合shiro,出現UnavailableSecurityManagerException 異常
問題描述:由於在java過濾器中通過User token = (User) SecurityUtils.getSubject().getPrincipal();獲取當前的登入使用者的資訊,報UnavailableSecurityManagerException這個異常。spr
spring boot 整合shiro 配置類報錯問題
這裡傳入的是下邊返回的自定義Realm,下邊是報錯,報的是該類不能被裝載,由jdk自己裝載,這裡不是太懂,記錄一下,由於錯誤太長,不好截圖,直接複製錯誤內容 >>>>>>>>>>>>>>&
Spring Boot 整合Shiro給Swagger&Druid放行資源
shiro的過濾器鏈配置: //放行靜態資源 filterChainDefinitionMap.put("/static/**", "anon");
spring boot整合shiro之shiro過濾器介紹
過濾器鏈條配置說明 1、一個URL可以配置多個Filter,使用逗號分隔 2、當設定多個過濾器時,全部驗證通過,才視為通過 3、部分過濾器可指定引數,如perms,roles Shiro內建的FilterChain anon(org.apac
Spring Boot 整合 Shiro 進行登入認證
安全無處不在,趁著放假讀了一下 Shiro 文件,並記錄一下 Shiro 整合 Spring Boot 在資料庫中根據角色控制訪問許可權 簡介 Apache Shiro是一個功能強大、靈活的,開源的安全框架。它可以乾淨利落地處理身份驗證、授權、企業會話管理和
[Spring Boot] Spring Boot 整合 LDAP 開發教程
Spring Boot 整合 LDAP 開發教程 目錄 Spring Boot 整合 LDAP 開發教程 簡介 LDAP 名詞解釋 配置依賴 連線 查詢 總結 REFRENCES 微信公眾號
Spring Boot 整合Shiro攔截Ajax請求
上一篇文章:Spring Boot 整合Shiro實現登陸認證和許可權控制,我們對shiro進行了整合。這一次我們具體來講一下shiro中的攔截器。 Shiro在處理非法請求比如沒有通過登入認證的請求
spring boot 整合shiro,swagger 加入攔截器後 swagger 不能訪問
在shiro的配置檔案中放行swagger: filterMap.put("/swagger-ui.html", "anon"); filterMap.put("/
spring boot 整合shiro的配置
spring boot提供了一個自帶的認證框架,同時也提供自定義的javaconfig配置擴充套件,spring-sercurity同樣也是優秀的框架,但是習慣了用apache shiro框架,而且原專案就是整合的shiro框架,到網上找了一下配置方式,沒找到完
spring boot 整合 shiro
版本說明: spring boot : 1.5.8.RELEASE shiro: 1.4.0 在沒有shiro-spring-boot-web-stater之前,我們通常的做法是把xx-shiro.xml中bean在configuration中定義,或者就是xml中進行定義
Spring Boot 整合Shiro和CAS
請大家在看本文之前,先了解如下知識點: 1、Shiro 是什麼?怎麼用? 2、Cas 是什麼?怎麼用? 3、最好有Spring基礎 首先看一下下面這張圖: 第一個流程是單純使用Shiro的流程。 第二個流程是單純使用Cas的流程。 第三個圖是Shiro整合Cas後的流程。 PS:流程圖急急
spring-boot整合graphql入門教程
本文介紹一個spring-boot + graphql, 是一個 graphql java 入門專案 graphql 到底是什麼 graphql 是一種 API 查詢語言, 用於伺服器端執行按已定義型別系統的查詢. GraphQL 不與任何特定的資料庫或儲存引擎進行繫結, 而是由您的程式碼