Former-commit-id: bcb1ff8923d59dd83128f056d29fd265324426b6
master
wangbing 5 years ago
parent 1a69d2feee
commit 86fbeeac45

@ -514,6 +514,7 @@ public class SpringBootCallable implements Callable {
File auth = Tool.createPath(root.getAbsolutePath(), "auth");
File base = Tool.createPath(root.getAbsolutePath(), "base");
File cache = Tool.createPath(root.getAbsolutePath(), "cache");
File excel = Tool.createPath(root.getAbsolutePath(), "excel");
File excelannotation = Tool.createPath(excel.getAbsolutePath(), "annotation");
File excelconverter = Tool.createPath(excel.getAbsolutePath(), "converter");
@ -531,6 +532,10 @@ public class SpringBootCallable implements Callable {
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/base/")) {
freeMarkerManager.outputTemp(Tool.createFile(base.getAbsolutePath(), name), option + "/java/frame/base/" + name, ctx);
}
//cache
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/cache/")) {
freeMarkerManager.outputTemp(Tool.createFile(cache.getAbsolutePath(), name), option + "/java/frame/cache/" + name, ctx);
}
//excel
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/excel/")) {

@ -16,7 +16,6 @@ import org.springframework.cache.annotation.EnableCaching;
<#else>
@EnableAutoConfiguration
</#if>
@EnableCaching
public class Application extends SpringBootServletInitializer {
@Override

@ -1,28 +1,16 @@
package ${basePackage}.config;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.config.CacheConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.wbsite.frame.cache.TokenCacheManager;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean(name = "tokenCacheManager")
public EhCacheCacheManager tokenCacheManager() {
EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration();
configuration.setMaxBytesLocalHeap("2G");
configuration.setUpdateCheck(false);//设置不检查更新
CacheConfiguration cacheConfiguration = new CacheConfiguration();
cacheConfiguration.setTimeToLiveSeconds(60 * 60);//一个小时有效缓存时间
cacheConfiguration.setTimeToIdleSeconds(30 * 60);//30分钟不使用eternal为true会失效
configuration.addDefaultCache(cacheConfiguration);
CacheManager cacheManager = CacheManager.create(configuration);
ehCacheCacheManager.setCacheManager(cacheManager);
return ehCacheCacheManager;
public TokenCacheManager tokenCacheManager() {
return new TokenCacheManager();
}
}

@ -13,6 +13,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.Token;
import ${basePackage}.frame.cache.TokenCacheManager;
import ${basePackage}.frame.utils.CookieUtil;
import ${basePackage}.module.system.mgr.TokensManager;
import ${basePackage}.module.system.req.TokensBuildRequest;
@ -87,16 +88,20 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
LocalData.setToken(LocalData.getTempToken());
} else {
// 组装Token ~ 这边根据实际的业务组装Token
TokenCacheManager tokenCacheManager = LocalData.getBean(TokenCacheManager.class);
Token tokenObj = tokenCacheManager.getToken(token);
if (tokenObj == null) {
TokensManager tokensManager = LocalData.getBean(TokensManager.class);
TokensBuildRequest tokensBuildRequest = new TokensBuildRequest();
tokensBuildRequest.setToken(token);
TokensBuildResponse tokensBuildResponse = tokensManager.build(tokensBuildRequest, LocalData.getSysToken());
if (tokensBuildResponse.hasError()) {
LocalData.setToken(LocalData.getTempToken());
tokenObj = LocalData.getTempToken();
} else {
Token token_ = tokensBuildResponse.getToken();
LocalData.setToken(token_);
tokenObj = tokensBuildResponse.getToken();
}
}
LocalData.setToken(tokenObj);
}
// Action

@ -0,0 +1,51 @@
package ${basePackage}.frame.cache;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import xyz.wbsite.frame.base.Token;
public class TokenCacheManager extends EhCacheCacheManager {
private CacheManager cacheManager;
private Cache cache;
private static final String TOKEN_CACHE = "tokenCache";
public TokenCacheManager() {
initCacheManager();
initCache();
}
private void initCacheManager() {
Configuration configuration = new Configuration();
configuration.setMaxBytesLocalHeap("1G");
configuration.setUpdateCheck(false);//设置不检查更新
cacheManager = CacheManager.create(configuration);
this.setCacheManager(cacheManager);
}
private void initCache() {
CacheConfiguration cacheConfiguration = new CacheConfiguration();
cacheConfiguration.setTimeToLiveSeconds(60 * 60);//一个小时有效缓存时间
cacheConfiguration.setTimeToIdleSeconds(30 * 60);//30分钟不使用eternal为true会失效
cacheConfiguration.setName(TOKEN_CACHE);
cache = new Cache(cacheConfiguration);
this.getCacheManager().addCache(cache);
}
public void addToken(String token, Token tokenObj) {
Element element = new Element(token, tokenObj);
this.cache.put(element);
}
public Token getToken(String token) {
Element element = this.cache.get(token);
if (element != null) {
LogUtil.d("find cache token:" + token);
return (Token) element.getObjectValue();
}
return null;
}
}

@ -14,6 +14,7 @@ import ${basePackage}.frame.utils.MapperUtil;
import ${basePackage}.frame.utils.Message;
import ${basePackage}.frame.utils.ValidationUtil;
import ${basePackage}.module.system.ent.Tokens;
import ${basePackage}.frame.cache.TokenCacheManager;
import ${basePackage}.module.system.mpr.TokensMapper;
import ${basePackage}.module.system.req.TokensBuildRequest;
import ${basePackage}.module.system.req.TokensCreateRequest;
@ -43,6 +44,8 @@ public class TokensManagerImpl implements TokensManager {
private String admin;
@Autowired
private TokensMapper tokensMapper;
@Autowired
private TokenCacheManager tokenCacheManager;
/**
*
@ -222,6 +225,7 @@ public class TokensManagerImpl implements TokensManager {
newToken.setUserName(tokens.getUserName());
}
tokenCacheManager.addToken(tokens.getToken(),newToken);
response.setToken(newToken);
}

@ -50,6 +50,11 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
</dependency>
<!-- mybatis 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>

Loading…
Cancel
Save

Powered by TurnKey Linux.