When we develop, we sometimes need to convert traditional spring shiro to spring boot projects, or directly integrate them. We need to figure out a knowledge of name, which is the relationship between xml configuration and spring bean code configuration. This is very important because spring boot does not have an xml configuration file (not absolute, spring boot can also reference xml configuration)
Introducing dependencies:
<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> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.3</version> </dependency>
If you get an error, cannot find slf4j, import dependencies
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version></dependency>
The traditional xml configuration is as follows:
<?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"/> <!--Definition realm--> <bean id="myRealm"> <property name="credentialsMatcher" 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"/> <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> <!-- /** = 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> </property> </bean> <!--Security Manager--> <bean id="securityManager"> <property name="realm" ref="myRealm"/> <property name="cacheManager" ref="cacheManager" /> <!--<property name="rememberMeManager" ref="rememberMeManager" />--> </bean> <!-- Credential Matcher--> <bean id="credentialsMatcher" > <constructor-arg index="0" ref="cacheManager"/> <constructor-arg index="1" value="5"/> <property name="hashAlgorithmName" value="md5"/> <property name="hashIterations" value="4"/> </bean> <!--Cache--> <bean id="cacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache.xml"/> </bean> <!--Open annotation--> <bean> <property name="securityManager" ref="securityManager" /> </bean> <bean id="lifecycleBeanPostProcessor" /></beans>
Convert to bean Create new class ShiroConfig
@Configurationpublic 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; }Some of the categories are custom interceptors and realm. At this time, spring can inject shiro, that is, spring boot integrates shiro
If you fail due to other configurations, you can refer to the open source project Lenos Quick Development Scaffolding Spring Boot Version, which has integration with Shiro, which can be used to learn
Address: https://gitee.com/bweird/lenosp
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.