Former-commit-id: 9daab64a5f2256f72db8477ff29ebfecce07a16d
master
wangbing 5 years ago
parent 86fbeeac45
commit 35fe7d28e9

@ -41,17 +41,26 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring 缓存注解起效 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- mybatis 二级缓存 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
</dependency>
<!-- mybatis 分页插件 -->
<!-- mybatis 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>

@ -514,7 +514,6 @@ 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");
@ -532,10 +531,6 @@ 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/")) {

@ -1,16 +1,43 @@
package ${basePackage}.config;
import net.sf.ehcache.Cache;
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 TokenCacheManager tokenCacheManager() {
return new TokenCacheManager();
public static final String TOKEN_CACHE = "tokenCache";
@Bean(name = TOKEN_CACHE)
public EhCacheCacheManager getCacheManager() {
net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration();
configuration.setMaxBytesLocalHeap("1G");
configuration.updateCheck(false);
configuration.addDiskStore(new DiskStoreConfiguration().path("java.io.tmpdir"));
CacheManager cacheManager = CacheManager.create(configuration);
// 添加token缓存
cacheManager.addCache(buildTokenCache());
return new EhCacheCacheManager(cacheManager);
}
/**
* TokenCache
*
* @return
*/
private Cache buildTokenCache() {
CacheConfiguration config = new CacheConfiguration();
config.setMemoryStoreEvictionPolicy("LFU");//最少使用
config.setTimeToLiveSeconds(60);//最长有效时间
config.setTimeToIdleSeconds(60);//无访问最长有效时间
config.setName(TOKEN_CACHE);
return new Cache(config);
}
}

@ -13,7 +13,6 @@ 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;
@ -88,20 +87,15 @@ 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()) {
tokenObj = LocalData.getTempToken();
} else {
tokenObj = tokensBuildResponse.getToken();
}
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());
} else {
LocalData.setToken(tokensBuildResponse.getToken());
}
LocalData.setToken(tokenObj);
}
// Action

@ -1,51 +0,0 @@
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;
}
}

@ -5,8 +5,10 @@ import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ${basePackage}.config.CacheConfig;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.base.Token;
import ${basePackage}.frame.utils.IDgenerator;
@ -14,7 +16,6 @@ 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;
@ -44,8 +45,6 @@ public class TokensManagerImpl implements TokensManager {
private String admin;
@Autowired
private TokensMapper tokensMapper;
@Autowired
private TokenCacheManager tokenCacheManager;
/**
*
@ -189,6 +188,7 @@ public class TokensManagerImpl implements TokensManager {
* @return
*/
@Override
@Cacheable(value = CacheConfig.TOKEN_CACHE, key = "#request.token")
public TokensBuildResponse build(TokensBuildRequest request, Token token) {
TokensBuildResponse response = new TokensBuildResponse();
@ -225,7 +225,6 @@ public class TokensManagerImpl implements TokensManager {
newToken.setUserName(tokens.getUserName());
}
tokenCacheManager.addToken(tokens.getToken(),newToken);
response.setToken(newToken);
}

Loading…
Cancel
Save

Powered by TurnKey Linux.