一、整合 Ehcache 缓存框架
首先第一步,添加 pom.xml 依赖:
<!-- Spring Boot 缓存支持启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <!--配置ehcache --> <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.6</version> </dependency>
依赖添加完成之后,在 resource 文件夹下新建 ehcache.xml 文件:
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <!--<ehcache>--> <!-- 指定一个文件目录,当EhCache把数据写到硬盘上时,将把数据写到这个文件目录下 --> <diskStore path="java.io.tmpdir"/> <!-- 设定缓存的默认数据过期策略 --> <defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"/> <cache name="cache1" maxElementsOnDisk="0" maxElementsInMemory="2000" eternal="true" overflowToDisk="true" diskPersistent="false"/> <cache name="cachelocal" maxElementsOnDisk="20000" maxElementsInMemory="2000" eternal="true" overflowToDisk="true" diskPersistent="true"/> </ehcache>
之后我们在 applicationContext.xml 添加:
<!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->
<cache:annotation-driven cache-manager="cacheManager" />
<!-- Spring提供的基于Ehcache实现的缓存管理器-->
<bean id="ehCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:/config/ehcache.xml" />
<property name="shared" value="true" />
</bean>
最后在入口文件添加 @EnableCaching 注解
编写一点调用内存的程序,之后就可以重启测试:
可以看到我们配置的 ehcache.xml 内容已经读取完成
通缓存读取的数据取出,整合完成!!!
二、整合 shiro 授权框架
老规矩,先修改 pom.xml 依赖文件 :
<!-- shiro相关 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>net.mingsoft</groupId> <artifactId>shiro-freemarker-tags</artifactId> <version>1.0.0</version> </dependency>
依赖导入完成之后,新建 spring-shiro.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置filter --> <!-- org.apache.shiro.spring.web.ShiroFilterFactoryBean --> <bean id="shiroFilter" class="com.seventeen.core.shiro.filter.CustomerShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <!-- <property name="loginUrl" value="/admin/base/login/index"></property>--> <property name="filters"> <map> <entry key="adminFilter" value-ref="AdminFilter"/> <entry key="teacherFilter" value-ref="TeacherFilter"/> </map> </property> <property name="filterChainDefinitions"> <value> /static/**=anon /=anon </value> </property> </bean> <!-- 配置securityManager --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!-- 引入ModularRealmAuthenticator类 --> <property name="authenticator" ref="CustomerModularRealmAuthenticator"/> <!-- 引入自定义sessionManager --> <property name="sessionManager" ref="sessionManager"/> <!-- 配置缓存 <property name="cacheManager" ref="shiroCacheManager"/> --> <!-- rememberMeManager配置 --> <property name="rememberMeManager" ref="rememberMeManager"/> <!-- 配置多个realm对象 --> <property name="realms"> <list> <ref bean="AdminRealm"/> <ref bean="TeacherRealm"/> </list> </property> </bean> <!-- 自定义realm --> <bean id="AdminRealm" class="com.seventeen.core.shiro.realm.AdminRealm"/> <bean id="TeacherRealm" class="com.seventeen.core.shiro.realm.TeacherRealm"/> <!-- 配置filter对应的登陆页面 --> <bean id="AdminFilter" class="com.seventeen.core.shiro.authc.CustomerFormAuthenticationFilter"> <property name="loginUrl" value="/"/> </bean> <bean id="TeacherFilter" class="com.seventeen.core.shiro.authc.CustomerFormAuthenticationFilter"> <property name="loginUrl" value="/"/> </bean> <!-- 自定义ModularRealmAuthenticator类,选则当前要使用的realm --> <bean id="CustomerModularRealmAuthenticator" class="com.seventeen.core.shiro.authc.CustomerRealmAuthenticator"> <property name="authenticationStrategy"> <!-- 配置认证策略,只要有一个Realm认证成功即可,并且返回所有认证成功信息 --> <bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"/> </property> </bean> <!-- 配置 Bean 后置处理器: 会自动的调用和 Spring 整合后各个组件的生命周期方法. --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- sessionManager --> <!-- org.apache.shiro.web.session.mgt.DefaultWebSessionManager --> <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <!-- 设置超时时间 (半小时)43200000--> <property name="globalSessionTimeout" value="43200000"/> <property name="sessionDAO" ref="sessionDAO"/> <property name="sessionValidationInterval" value="60000"/> </bean> <!-- sessionDAO --> <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.MemorySessionDAO"/> <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager"> <property name="cipherKey" value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}"/> <property name="cookie" ref="rememberMeCookie"/> </bean> <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie"> <constructor-arg value="remember"/> <property name="httpOnly" value="true"/> <property name="maxAge" value="2592000"/><!-- 30天 --> </bean> </beans>
这里要注意一个问题,因为我们需要设置拦截规则,所以涉及到静态资源路径的问题。
静态资源 一般都是放在 resource/static 文件夹下,而 SpringBoot 在我们引用静态资源的时候会自动把我们的 /static 路径去掉。这里需要注意,如果习惯了 static 我们可以在 application.properties 配置文件中添加下面代码:
#设置静态文件路径 spring.web.resources.static-locations=classpath:/
自定义的 realm、filter这里就不展示了,网上有很多可以 ctrl+c 、ctrl+v。
到这里基本的整合就已经结束了,接下来最重要的在入口文件加上 shiro 配置文件:
@SpringBootApplication(exclude= HibernateJpaAutoConfiguration.class) @ImportResource(locations={ "classpath:/config/applicationContext.xml", "classpath:/config/spring-shiro.xml" }) @EnableCaching public class KfyyxtApplication { public static void main(String[] args) { SpringApplication.run(KfyyxtApplication.class, args); } }
最后我们就可以测试编写一个登录测试了:
输入账号、密码、验证码,弹出提示登录成功!