這篇文章給大家分享的是有關(guān)springboot集成shiro的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括順平網(wǎng)站建設(shè)、順平網(wǎng)站制作、順平網(wǎng)頁(yè)制作以及順平網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,順平網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到順平省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
我們開(kāi)發(fā)時(shí)候有時(shí)候要把傳統(tǒng)spring shiro轉(zhuǎn)成spring boot項(xiàng)目,或者直接集成,name我們要搞清楚一個(gè)知識(shí),就是 xml配置和spring bean代碼配置的關(guān)系,這一點(diǎn)很重要,因?yàn)閟pring boot是沒(méi)有xml配置文件的(也不絕對(duì),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>
如果你出現(xiàn)錯(cuò)誤 找不到slf4j,引入依賴
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency>
傳統(tǒng)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"/> <!--目前去掉自定義攔截驗(yàn)證 由個(gè)人處理登錄業(yè)務(wù)--> <!--<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過(guò)濾器 --> <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都可以匿名訪問(wèn) --> /login = verCode,anon /getCode = anon /logout = logout /plugin/** = anon <!-- /** = authc 所有url都必須認(rèn)證通過(guò)才可以訪問(wèn)--> /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> <!--開(kāi)啟注解--> <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>
轉(zhuǎn)換成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; }
其中有個(gè)別類是自定義的攔截器和 realm,此時(shí)spring 即能注入shiro 也就是 spring boot集成上了 shiro
如果你因?yàn)槠渌渲靡鹨恍┦?,可以參考開(kāi)源項(xiàng)目 lenos快速開(kāi)發(fā)腳手架 spring boot版本,其中有對(duì)shiro的集成。
感謝各位的閱讀!關(guān)于“springboot集成shiro的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
當(dāng)前文章:springboot集成shiro的示例分析
分享地址:http://muchs.cn/article46/igheeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站維護(hù)、網(wǎng)頁(yè)設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站、靜態(tài)網(wǎng)站、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)