Ketika kita berkembang, kadang -kadang kita perlu mengonversi Proyek Spring Shiro tradisional menjadi Spring Boot, atau secara langsung mengintegrasikannya. Kita perlu mencari tahu pengetahuan, yang merupakan hubungan antara konfigurasi XML dan konfigurasi kode kacang pegas. Ini sangat penting karena Spring Boot tidak memiliki file konfigurasi XML (bukan absolut, Spring Boot juga dapat merujuk konfigurasi XML)
Memperkenalkan dependensi:
<dependency> <ArtifactId> ehcache-core </stifactid> <groupid> net.sf.ehcache </groupid> <version> 2.5.0 </version> </dependency> <dependency> <groupid> org.apache.shiro </groupid> <ArTifacTid> Shiro-ehcache </art/version </groupId> <ArTifacTID> Shiro-ehcache </art/version </groupId> <ArTifacTID> Shiro-ehcache </art/version </groupid> <ArTifacTID> Shiro-ehcache </art/version </groupId> <ArTifacTid> Shiro-ehcache </Articachence <groupId> org.slf4j </groupid> <ArTifactId> slf4j-api </stifactid> <version> 1.7.25 </version> </dependency> <dependency> <groupid> org.apache.shiro </groupid> <ArTifactid> Shiro-core </artifacache. <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> <dependency> <GroupId> org.apache.shiro </groupid> <ArTifactId> Shiro-Spring </artifactid> <version> 1.2.3 </version> </gandendency>
Jika Anda mendapatkan kesalahan, tidak dapat menemukan SLF4J, impor dependensi
<dependency> <GroupId> org.slf4j </groupid> <ArTifactId> slf4j-log4j12 </artifactid> <version> 1.7.25 </version> </dependency>
Konfigurasi XML tradisional adalah sebagai berikut:
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/schema/aop" xmlns: tx = "http:/schema.aop" XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-leans.xsd http://www.springframework.orger.orga http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop:/www.spramework.org/schema/aop http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <konteks: component-scan base-package =" com.len "/<! ref="credentialsMatcher"/> </bean> <bean id="permissionFilter"/> <!--Currently remove custom interception verification and handle login services by individuals--> <!--<bean id="customAdvicFilter"> <property name="failureKeyAttribute" value="shiroLoginFailure"/> </bean>--> <bean id="verfityCodeFilter"> <property name="failureKeyAttribute" value="shiroLoginFailure"/> <property name="jcaptchaParam" value="code"/> <property name="verfitiCode" value="true"/> </bean> <!-- Shiro filter--> <bean id="shiroFilter"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value = "/login"/> <name properti = "unafhorizedUrl" value = "/gologin"/> <name properti = "filter"> <peta> <entri key = "per" value-ref = "izinfilter"/> <entry key = "vercode" value-ref = "verFityCodefilter"/<entri-!-<entri = "value =" value = "verFityCodefilter"/<certy-!-<entri = "value =" value = "verfitycodefilter"//<!-<entri = "value =" value = "verfitycodefilter"//<!-<entri = "value =" value = "verFityCodefilter"//<! "value =" value = "value =" valueCodefilter "//<! </map> </property> <property name="filterChainDefinitions"> <value> <!-- /** = anonAny URLs can be accessed anonymously --> /login = verCode,anon /getCode = anon /logout = logout /plugin/** = anon <!-- /** = authc All urls must be authenticated to access--> /user/**=per <!-- /login = Main->/** = authc </ value> </preate> </ bean> <!-Manajer Keamanan-> <bean id = "SecurityManager"> <nama properti = "ranah" ref = "myrealm"/> <name properti = "cacheManager" ref = "cacheManager"/> <!-<nama properti = "RememberManager"! <bean id = "credentialsmatcher"> <constructor-arg index = "0" ref = "cacheManager"/> <constructor-arg index = "1" value = "5"/> <name properti = "hashalgorithmname" value = "md5"/> <nama properti = "hashiterations" value = "4"//"cean> <! <property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache.xml"/> </bean> <!--Open annotation--> <bean> <property name="securityManager" ref="securityManager" /> </bean> <bean id="lifecycleBeanPostProcessor" /></beans>
Konversi ke Bean Buat Kelas Baru ShiroConfig
@ConfigurationPublic kelas ShiroConfig {@Bean RetryLimItCredentialScatcher GetRetryLimitCredentialScatcher () {RetryLimItCredentialScatcher RM = RetryLimitCredentialScatcher baru (getCacheManager (), "5"); rm.sethashalgorithmname ("md5"); rm.sethashiterations (4); mengembalikan RM; } @Bean Public LoginRealm getLoginRealM () {LoginRealM realm = new LoginRealM (); realm.setCredentialSmatcher (getRetryLimitCredentialSmatcher ()); Return Realm; } @Bean ehcachemanager getCacheManager () {ehcachemanager ehcachemanager = ehcachemanager baru (); ehcachemanager.setcachemanagerconfigfile ("classpath: ehcache/ehcache.xml"); return ehcachemanager; } @Bean Public LifeCycleBeanPostProcessor getlifecycleBeanPostProcessor () {return baru LifeCycleBeanPostProcessor (); } @Bean (name = "SecurityManager") Public DefaultWebSecurityManager GetDefaultWebSecurityManager () {DefaultWebSecurityManager DWM = New DefaultWebSecurityManager (); dwm.setrealm (getLoginrealm ()); dwm.setCachemanager (getCachemanager ()); return dwm; } @Bean Public IzinFilter getPerMissionFilter () {permissionfilter pf = new IzinFilter (); mengembalikan PF; } @Bean Public VerfityCodefilter getverFityCodefilter () {verfitycodefilter vf = verfitycodefilter baru (); VF.SetFailureKeyAttribute ("ShirologInfailure"); vf.setjcaptchaparam ("kode"); vf.setverfiticode (true); mengembalikan VF; } @Bean (name = "shirofilter") public shirofilterfactorybean getshirofilterfactorybean () {shirofilterfactorybean sfb = shirofilterfactorybean () baru (); sfb.setsecurityManager (getDefaultWebSecurityManager ()); sfb.setloginurl ("/login"); sfb.setunauthorizedUrl ("/gologin"); Peta <string, filter> filter = hashmap baru <> (); filter.put ("per", getPerMissionFilter ()); filter.put ("vercode", getverfitycodefilter ()); SFB.SetFilters (filter); Peta <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); mengembalikan SFB; } @Bean DefaultAdvisorAutoproxyCreator AdvisorAutOproxyCreator () {defaultAdvisorAutoproxyCreator AdvisorAutoproxyCreator = New DefaultAdvisorAutoproxyCreator (); AdvisorAutoproxyCreator.SetProxyTargetClass (true); Return AdvisorAutoproxyCreator; } @Bean Otorisasi PublikTributeSourceadVisor GetAuthorizationAttributeSourCeadVisor () {OtorationAttributeSourCeAdvisor as = Otorisasi baruTributSourceadVisor (); as.setsecurityManager (getDefaultWebSecurityManager ()); kembali sebagai; } @Bean Public FilterRegistrationBean DelegatingFilterProxy () {filterRegistrationBean filterregistrationBean = new filterregistrationBean (); DelegatingFilterproxy proxy = delegatingfilterproxy baru (); proxy.setargetFilterlifecycle (true); proxy.setargetBeanName ("shirofilter"); filterregistrationBean.setFilter (proxy); return filterregistrationBean; }Beberapa kategori adalah pencegat dan ranah khusus. Saat ini, musim semi dapat menyuntikkan Shiro, yaitu, boot musim semi mengintegrasikan Shiro
Jika Anda gagal karena konfigurasi lain, Anda dapat merujuk pada Proyek Open Source Lenos Versi Perancah Perancah Pengembangan Cepat, yang memiliki integrasi dengan Shiro, yang dapat digunakan untuk belajar
Alamat: https://gitee.com/bweird/lenosp
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.