Description
为解决每次调用接口都执行shiro授权操作,影响效率,因此引入缓存概念
STEP
- 导入依赖
- 配置ehcache-shieo.xml文件
- ShiroConfig类中添加相关Bean
- EhCacheManager
- DefaultWebSecurityManager(设置缓存管理器,即a)
- UserRealm中启用验证,名字为xml文件中的
- 启动类开启缓存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.10.0</version> </dependency>
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| <?xml version="1.0" encoding="UTF-8"?> <ehcache name="es">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
<cache name="authorizationCache" maxEntriesLocalHeap="2000" eternal="false" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" statistics="true"> </cache>
<cache name="authenticationCache" maxEntriesLocalHeap="2000" eternal="false" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" statistics="true"> </cache>
</ehcache>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| package com.jc_demo.shiro;
import com.jc_demo.entity.entities.User; import com.jc_demo.interceptor.MyFormAuthenticationFilter; import org.apache.shiro.cache.ehcache.EhCacheManager; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter; import java.util.LinkedHashMap; import java.util.Map;
@Configuration public class ShiroConfig {
@Bean public UserRealm userRealm() { UserRealm ur= new UserRealm(); ur.setCachingEnabled(true); ur.setAuthenticationCachingEnabled(true); ur.setAuthenticationCacheName("authenticationCache"); ur.setAuthorizationCachingEnabled(true); ur.setAuthorizationCacheName("authorizationCache");
return ur; }
@Bean(name = "filterShiroFilterRegistrationBean") public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("SecurityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
Map<String, Filter> filters = new LinkedHashMap<>(); filters.put("MyFormAuthenticationFilter", new MyFormAuthenticationFilter()); bean.setFilters(filters); bean.setSecurityManager(defaultWebSecurityManager);
Map<String, String> filterMap = new LinkedHashMap<>(); filterMap.put("/api/user/login", "anon");
filterMap.put("/api/**", "MyFormAuthenticationFilter");
bean.setFilterChainDefinitionMap(filterMap);
return bean; }
@Bean(name = "SecurityManager") public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setCacheManager(ehCacheManager()); securityManager.setRealm(userRealm); return securityManager; }
@Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) { AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor(); advisor.setSecurityManager(securityManager); return advisor; }
@Bean public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() { DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator(); defaultAAP.setProxyTargetClass(true); return defaultAAP; }
@Bean public EhCacheManager ehCacheManager(){ EhCacheManager cacheManager = new EhCacheManager(); cacheManager.setCacheManagerConfigFile("classpath:\\shiro\\ehcache-shiro.xml"); return cacheManager; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.jc_demo;
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import springfox.documentation.oas.annotations.EnableOpenApi;
@EnableCaching
@EnableOpenApi @SpringBootApplication @MapperScan("com.jc_demo.mapper") public class JcDemoApplication {
public static void main(String[] args) { SpringApplication.run(JcDemoApplication.class, args); }
}
|