org.apache.shiro.cache.CacheManager Java Examples

The following examples show how to use org.apache.shiro.cache.CacheManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ShiroConfig.java    From MeetingFilm with Apache License 2.0 6 votes vote down vote up
/**
 * session管理器(单机环境)
 */
@Bean
@ConditionalOnProperty(prefix = "guns", name = "spring-session-open", havingValue = "false")
public DefaultWebSessionManager defaultWebSessionManager(CacheManager cacheShiroManager, GunsProperties gunsProperties) {
    DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    sessionManager.setCacheManager(cacheShiroManager);
    sessionManager.setSessionValidationInterval(gunsProperties.getSessionValidationInterval() * 1000);
    sessionManager.setGlobalSessionTimeout(gunsProperties.getSessionInvalidateTime() * 1000);
    sessionManager.setDeleteInvalidSessions(true);
    sessionManager.setSessionValidationSchedulerEnabled(true);
    Cookie cookie = new SimpleCookie(ShiroHttpSession.DEFAULT_SESSION_ID_NAME);
    cookie.setName("shiroCookie");
    cookie.setHttpOnly(true);
    sessionManager.setSessionIdCookie(cookie);
    return sessionManager;
}
 
Example #2
Source File: ShiroConfig.java    From frpMgr with MIT License 6 votes vote down vote up
/**
 * 定义Shiro安全管理配置
 */
@Bean
public WebSecurityManager securityManager(AuthorizingRealm authorizingRealm,
		CasAuthorizingRealm casAuthorizingRealm, SessionManager sessionManager,
		CacheManager shiroCacheManager) {
	WebSecurityManager bean = new WebSecurityManager();
	Collection<Realm> realms = ListUtils.newArrayList();
	realms.add(authorizingRealm); // 第一个为权限授权控制类
	realms.add(casAuthorizingRealm);
	bean.setRealms(realms);
	bean.setSessionManager(sessionManager);
	bean.setCacheManager(shiroCacheManager);
	// 设置支持CAS的subjectFactory
	bean.setSubjectFactory(new CasSubjectFactory());
	return bean;
}
 
Example #3
Source File: ShiroConfig.java    From WebStack-Guns with MIT License 6 votes vote down vote up
/**
 * session管理器(单机环境)
 */
@Bean
@ConditionalOnProperty(prefix = "guns", name = "spring-session-open", havingValue = "false")
public DefaultWebSessionManager defaultWebSessionManager(CacheManager cacheShiroManager, GunsProperties gunsProperties) {
    DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    sessionManager.setCacheManager(cacheShiroManager);
    sessionManager.setSessionValidationInterval(gunsProperties.getSessionValidationInterval() * 1000);
    sessionManager.setGlobalSessionTimeout(gunsProperties.getSessionInvalidateTime() * 1000);
    sessionManager.setDeleteInvalidSessions(true);
    sessionManager.setSessionValidationSchedulerEnabled(true);
    Cookie cookie = new SimpleCookie(ShiroHttpSession.DEFAULT_SESSION_ID_NAME);
    cookie.setName("shiroCookie");
    cookie.setHttpOnly(true);
    sessionManager.setSessionIdCookie(cookie);
    return sessionManager;
}
 
Example #4
Source File: ApiKeyRealm.java    From emodb with Apache License 2.0 6 votes vote down vote up
public ApiKeyRealm(String name, CacheManager cacheManager, AuthIdentityReader<ApiKey> authIdentityReader,
                   PermissionReader permissionReader, @Nullable String anonymousId) {
    super(null, AnonymousCredentialsMatcher.anonymousOrMatchUsing(new SimpleCredentialsMatcher()));


    _authIdentityReader = checkNotNull(authIdentityReader, "authIdentityReader");
    _permissionReader = checkNotNull(permissionReader, "permissionReader");
    _anonymousId = anonymousId;

    setName(checkNotNull(name, "name"));
    setAuthenticationTokenClass(ApiKeyAuthenticationToken.class);
    setPermissionResolver(permissionReader.getPermissionResolver());
    setRolePermissionResolver(createRolePermissionResolver());
    setCacheManager(prepareCacheManager(cacheManager));
    setAuthenticationCachingEnabled(true);
    setAuthorizationCachingEnabled(true);

    // By default Shiro calls clearCache() for each user when they are logged out in order to prevent stale
    // credentials from being cached.  However, if the cache manager implements InvalidatingCacheManager then it has
    // its own internal listeners that will invalidate the cache on any updates, making this behavior unnecessarily
    // expensive.
    _clearCaches = cacheManager != null && !(cacheManager instanceof InvalidatableCacheManager);
    _log.debug("Clearing of caches for realm {} is {}", name, _clearCaches ? "enabled" : "disabled");
}
 
Example #5
Source File: ShiroManager.java    From shiro-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Bean(name = "securityManager")
	@ConditionalOnMissingBean
	public DefaultSecurityManager securityManager(CacheManager shiroCacheManager) {
        DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();

        // 用自己的Factory实现替换默认
        // 用于关闭session功能
        dwsm.setSubjectFactory(new StatelessSubjectFactory());
        dwsm.setSessionManager(defaultSessionManager());
        // 关闭session存储
        ((DefaultSessionStorageEvaluator) ((DefaultSubjectDAO)dwsm.getSubjectDAO()).getSessionStorageEvaluator()).setSessionStorageEnabled(false);

//      <!-- 用户授权/认证信息Cache, 采用EhCache 缓存 -->
        dwsm.setCacheManager(shiroCacheManager);

        SecurityUtils.setSecurityManager(dwsm);
        return dwsm;
	}
 
Example #6
Source File: CacheManagerBuilder.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
private CacheManager decision(org.springframework.cache.CacheManager springCacheManager) {
	if (springCacheManager instanceof EhCacheCacheManager) {
		EhCacheManager ehCacheManager = new EhCacheManager();
		ehCacheManager.setCacheManager(((EhCacheCacheManager) springCacheManager).getCacheManager());
		return ehCacheManager;
	}
	
	if (springCacheManager instanceof org.springframework.data.redis.cache.RedisCacheManager) {
		GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
		RedisTemplate rt = new RedisTemplate<Object, Object>();
		rt.setConnectionFactory(SpringContextUtils.getBean(RedisConnectionFactory.class));
		rt.setKeySerializer(jsonSerializer);
		rt.setHashKeySerializer(jsonSerializer);
		rt.setBeanClassLoader(this.getClass().getClassLoader());
		rt.afterPropertiesSet();
		RedisCacheManager redisCacheManager = new RedisCacheManager();
		redisCacheManager.setRedisTemplate(rt);
		return redisCacheManager;
	}
	return new SpringCacheManager(springCacheManager);
}
 
Example #7
Source File: ShiroConfiguration.java    From spring-boot-shiro with Apache License 2.0 5 votes vote down vote up
/**
 * (基于内存的)用户授权信息Cache
 */
@Bean(name = "cacheManager")
@ConditionalOnMissingBean(name = "cacheManager")
@ConditionalOnMissingClass(value = {"org.apache.shiro.cache.ehcache.EhCacheManager"})
public CacheManager cacheManager() {
    return new MemoryConstrainedCacheManager();
}
 
Example #8
Source File: CacheManagerBuilder.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
public CacheManager build() {
	CacheManager cacheManager = null;
	org.springframework.cache.CacheManager springCacheManager = springCachePvd.getIfAvailable();
	if(Objects.isNull(springCacheManager)) {
		cacheManager = new MapCacheManager();
	}else {
		cacheManager = this.decision(springCacheManager);
	}
	return cacheManager;
}
 
Example #9
Source File: ShiroAutoConfiguration.java    From utils with Apache License 2.0 5 votes vote down vote up
@Bean(name = "cacheManager")
@ConditionalOnClass(name = {"org.apache.shiro.cache.ehcache.EhCacheManager"})
@ConditionalOnMissingBean(name = "cacheManager")
public CacheManager ehcacheManager() {
    EhCacheManager ehCacheManager = new EhCacheManager();

    ShiroProperties.Ehcache ehcache = shiroProperties.getEhcache();
    if (ehcache.getConfigFile() != null) {
        ehCacheManager.setCacheManagerConfigFile(ehcache.getConfigFile());
    }

    return ehCacheManager;
}
 
Example #10
Source File: SecurityModule.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
protected void configureShiro() {
   bindSessionDAO(bind(SessionDAO.class));
   bindCacheManager(bind(CacheManager.class));
   bindAuthenticationDAO(bind(AuthenticationDAO.class));
   bindAuthorizationDAO(bind(AuthorizationDAO.class));
   bindPrincipalResolver(bind(PrincipalResolver.class));
   bindCredentialsHashingStrategy(bind(CredentialsHashingStrategy.class));
   expose(CredentialsHashingStrategy.class);
   bindRealm().to(GuicedIrisRealm.class);
}
 
Example #11
Source File: ShiroAutoConfiguration.java    From utils with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public SessionDAO sessionDAO(CacheManager cacheManager) {
    EnterpriseCacheSessionDAO dao = new EnterpriseCacheSessionDAO();
    dao.setActiveSessionsCacheName(shiroSessionProperties.getActiveSessionsCacheName());
    Class<? extends SessionIdGenerator> idGenerator = shiroSessionProperties.getIdGenerator();
    if (idGenerator != null) {
        SessionIdGenerator sessionIdGenerator = BeanUtils.instantiate(idGenerator);
        dao.setSessionIdGenerator(sessionIdGenerator);
    }
    dao.setCacheManager(cacheManager);

    return dao;
}
 
Example #12
Source File: ShiroConfig.java    From WebStack-Guns with MIT License 5 votes vote down vote up
/**
 * 缓存管理器 使用Ehcache实现
 */
@Bean
public CacheManager getCacheShiroManager(EhCacheManagerFactoryBean ehcache) {
    EhCacheManager ehCacheManager = new EhCacheManager();
    ehCacheManager.setCacheManager(ehcache.getObject());
    return ehCacheManager;
}
 
Example #13
Source File: ExtendedPropertiesRealm.java    From tapestry-security with Apache License 2.0 5 votes vote down vote up
@Override
public void setCacheManager(CacheManager authzInfoCacheManager)
{
	if (created && getCacheManager() != null)
	{
		return;
	}
	super.setCacheManager(authzInfoCacheManager);
}
 
Example #14
Source File: SimpleAuthorizingRealm.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
public SimpleAuthorizingRealm(CacheManager cacheManager, CredentialsMatcher matcher) {
	super(cacheManager, matcher);
	HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
	hashedCredentialsMatcher.setHashAlgorithmName("SHA-256");
	hashedCredentialsMatcher.setHashIterations(1024);
	// 这一行决定hex还是base64
	hashedCredentialsMatcher.setStoredCredentialsHexEncoded(false);
	// 设置token类型是关键!!!
	setCredentialsMatcher(hashedCredentialsMatcher);
	setAuthenticationTokenClass(UsernamePasswordToken.class);
}
 
Example #15
Source File: SecurityGuiceConfigurer.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
public void configure(Binder binder) {
    // Subject
    SecurityConfig.SubjectConfig subjectConfig = securityConfig.subject();
    Optional.ofNullable(subjectConfig.getContext()).ifPresent(c -> binder.bind(SubjectContext.class).to(c));
    Optional.ofNullable(subjectConfig.getFactory()).ifPresent(f -> binder.bind(SubjectFactory.class).to(f));
    Class<? extends SubjectDAO> subjectDao = subjectConfig.getDao();
    binder.bind(SubjectDAO.class).to(subjectDao != null ? subjectDao : DefaultSubjectDAO.class);

    // Authentication
    SecurityConfig.AuthenticationConfig authenticationConfig = securityConfig.authentication();
    binder.bind(Authenticator.class).to(authenticationConfig.getAuthenticator());
    binder.bind(AuthenticationStrategy.class).to(authenticationConfig.getStrategy());
    binder.bind(CredentialsMatcher.class).to(authenticationConfig.getCredentialsMatcher());

    // Cache configuration
    SecurityConfig.CacheConfig cacheConfig = securityConfig.cache();
    binder.bind(CacheManager.class).to(cacheConfig.getManager());

    // Sessions
    SecurityConfig.SessionConfig sessionConfig = securityConfig.sessions();
    binder.bind(SessionStorageEvaluator.class).to(sessionConfig.getStorageEvaluator());
    Optional.ofNullable(sessionConfig.getValidationScheduler())
            .ifPresent(s -> binder.bind(SessionValidationScheduler.class).to(s));
    binder.bindConstant()
            .annotatedWith(Names.named("shiro.sessionValidationInterval"))
            .to(sessionConfig.getValidationInterval() * 1000);
    binder.bindConstant()
            .annotatedWith(Names.named("shiro.globalSessionTimeout"))
            .to(sessionConfig.getTimeout() * 1000);
}
 
Example #16
Source File: ShiroJwtConfig.java    From hdw-dubbo with Apache License 2.0 5 votes vote down vote up
/**
 * 缓存管理器
 * @return
 */
@Bean
public CacheManager shiroRedisCacheManager(){
    ShiroRedisCacheManager redisCacheManager=new ShiroRedisCacheManager(cacheLive*1000,cacheKeyPrefix+"-shiro-cache-",redisTemplate);
    return redisCacheManager;

}
 
Example #17
Source File: EhcacheShiroManager.java    From ehcache-shiro with Apache License 2.0 5 votes vote down vote up
private org.ehcache.CacheManager ensureCacheManager() throws MalformedURLException {
  if (manager == null) {
    manager = CacheManagerBuilder.newCacheManager(getConfiguration());
    manager.init();

    cacheManagerImplicitlyCreated = true;
  }

  return manager;
}
 
Example #18
Source File: EhcacheShiroManager.java    From ehcache-shiro with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the wrapped {@link org.ehcache.CacheManager} instance
 *
 * @param cacheManager the {@link org.ehcache.CacheManager} to be used
 */
public void setCacheManager(org.ehcache.CacheManager cacheManager) {
  try {
    destroy();
  } catch (Exception e) {
    log.warn("The Shiro managed CacheManager threw an Exception while closing", e);
  }
  manager = cacheManager;
  cacheManagerImplicitlyCreated = false;
}
 
Example #19
Source File: Realm.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public Realm( CacheManager cacheManager ) {
    super( cacheManager );
    setCredentialsMatcher( new AllowAllCredentialsMatcher() );
    setPermissionResolver(new CustomPermissionResolver());
    setCachingEnabled(true);
    setAuthenticationCachingEnabled(true);
}
 
Example #20
Source File: GenericOAuth2ApiBinding.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
public GenericOAuth2ApiBinding(C config, RestTemplate restTemplate, CacheManager cacheManager) {
	notNull(config, "'config' is null, please check the configure");
	notNull(restTemplate, "'restTemplate' is null, please check the configure");
	notNull(cacheManager, "'cacheManager' is null, please check the configure");
	this.config = config;
	this.restTemplate = restTemplate;
	Object cacheObject = cacheManager.getCache(DEFAULT_CACHE_NAME);
	notNull(cacheObject, "'cacheObject' is null, please check the configure");
	isInstanceOf(IamCache.class, cacheObject);
	this.cache = (IamCache) cacheObject;
}
 
Example #21
Source File: ShiroAutoConfiguration.java    From spring-boot-shiro with Apache License 2.0 5 votes vote down vote up
@Bean(name = "cacheManager")
@ConditionalOnClass(name = {"org.apache.shiro.cache.ehcache.EhCacheManager"})
@ConditionalOnMissingBean(name = "cacheManager")
public CacheManager ehcacheManager() {
    EhCacheManager ehCacheManager = new EhCacheManager();
    ShiroProperties.Ehcache ehcache = properties.getEhcache();
    if (ehcache.getCacheManagerConfigFile() != null) {
        ehCacheManager.setCacheManagerConfigFile(ehcache.getCacheManagerConfigFile());
    }
    return ehCacheManager;
}
 
Example #22
Source File: ShiroConfiguration.java    From spring-boot-shiro with Apache License 2.0 5 votes vote down vote up
@Bean(name = "securityManager")
@DependsOn(value = {"cacheManager", "rememberMeManager", "mainRealm"})
public DefaultSecurityManager securityManager(Realm realm, RememberMeManager rememberMeManager,
                                              CacheManager cacheManager, SessionManager sessionManager) {
    DefaultSecurityManager sm = new DefaultWebSecurityManager();
    sm.setRealm(realm);
    sm.setCacheManager(cacheManager);
    sm.setSessionManager(sessionManager);
    sm.setRememberMeManager(rememberMeManager);
    return sm;
}
 
Example #23
Source File: ShiroConfiguration.java    From easyweb with Apache License 2.0 5 votes vote down vote up
@Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(
            SystemAuthorizingRealm myShiroRealm,
            DefaultWebSessionManager sessionManager,
            CacheManager shiroCacheManager) {
        DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
        dwsm.setRealm(myShiroRealm);
        dwsm.setSessionManager(sessionManager);
//      <!-- 用户授权/认证信息Cache, 采用redis 缓存 -->
        dwsm.setCacheManager(shiroCacheManager);
        return dwsm;
    }
 
Example #24
Source File: ShiroCustomizer.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
public CacheManager getCacheManager() {
	return cacheManager;
}
 
Example #25
Source File: kickoutSessionControlFilter.java    From songjhh_blog with Apache License 2.0 4 votes vote down vote up
public void setCacheManager(CacheManager cacheManager) {
    this.cache = cacheManager.getCache("shiro-kickout-session");
}
 
Example #26
Source File: Realm.java    From usergrid with Apache License 2.0 4 votes vote down vote up
public Realm( CacheManager cacheManager, CredentialsMatcher matcher ) {
    super(cacheManager, new AllowAllCredentialsMatcher());
    setPermissionResolver( new CustomPermissionResolver() );
    setCachingEnabled(true);
    setAuthenticationCachingEnabled(true);
}
 
Example #27
Source File: SpringCacheManager.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
public SpringCacheManager(org.springframework.cache.CacheManager cacheManager){
	this.delegator = cacheManager;
}
 
Example #28
Source File: RetryLimitHashedCredentialsMatcher.java    From VideoMeeting with Apache License 2.0 4 votes vote down vote up
public RetryLimitHashedCredentialsMatcher(CacheManager cacheManager) {
	passwordRetryCache = cacheManager.getCache("passwordRetryCache");
}
 
Example #29
Source File: ShiroConfig.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
public CacheManager getCacheManager() {
	return this.cacheManager;
}
 
Example #30
Source File: CacheManagerBuilder.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
public CacheManagerBuilder(ObjectProvider<org.springframework.cache.CacheManager> springCachePvd) {
	this.springCachePvd = springCachePvd;
}