SpringBoot 整合 ehcache缓存框架 和 shiro授权框架
一、整合 Ehcache 缓存框架
首先第一步,添加 pom.xml 依赖:
org.springframework.boot
spring-boot-starter-cache
net.sf.ehcache
ehcache
2.10.6
pom.xml
依赖添加完成之后,在 resource 文件夹下新建 ehcache.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>ehecache.xml<defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"/> maxElementsOnDisk="0" maxElementsInMemory="2000" eternal="true" overflowToDisk="true" diskPersistent="false"/> maxElementsOnDisk="20000" maxElementsInMemory="2000" eternal="true" overflowToDisk="true" diskPersistent="true"/>
之后我们在 applicationContext.xml 添加:
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
applicationContext.xml
最后在入口文件添加 @EnableCaching 注解
编写一点调用内存的程序,之后就可以重启测试:
可以看到我们配置的 ehcache.xml 内容已经读取完成
通缓存读取的数据取出,整合完成!!!
二、整合 shiro 授权框架
老规矩,先修改 pom.xml 依赖文件 :
org.apache.shiro
shiro-core
1.4.0
org.apache.shiro
shiro-web
1.4.0
org.apache.shiro
shiro-spring
1.4.0
org.apache.shiro
shiro-ehcache
1.4.0
net.mingsoft
shiro-freemarker-tags
1.0.0
pom.xml
依赖导入完成之后,新建 spring-shiro.xml 配置文件:
<?xml version="1.0" encoding="UTF-8"?>spring-shiro.xmlxmlns: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"> class="com.seventeen.core.shiro.filter.CustomerShiroFilterFactoryBean"> /static/**=anon /=anon
这里要注意一个问题,因为我们需要设置拦截规则,所以涉及到静态资源路径的问题。
静态资源 一般都是放在 resource/static 文件夹下,而 SpringBoot 在我们引用静态资源的时候会自动把我们的 /static 路径去掉。这里需要注意,如果习惯了 static 我们可以在 application.properties 配置文件中添加下面代码:
#设置静态文件路径 spring.web.resources.static-locations=classpath:/application.properties
自定义的 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); } }入口文件
最后我们就可以测试编写一个登录测试了:
输入账号、密码、验证码,弹出提示登录成功!