新增框架yml统一配置,修复编译器不识别yml问题,修复redis登录问题

This commit is contained in:
lhc
2021-01-27 17:55:08 +08:00
parent ec588e4226
commit 63f444c81f
7 changed files with 61 additions and 28 deletions

View File

@@ -139,6 +139,11 @@
<artifactId>sqlite-jdbc</artifactId> <artifactId>sqlite-jdbc</artifactId>
<version>3.32.3.2</version> <version>3.32.3.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>

View File

@@ -0,0 +1,14 @@
package com.common.base.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "frame")
public class FrameConfig {
private Boolean isRedisLogin = false;
private Integer loginTimeout = 4;
private Boolean showControllerLog = true;
}

View File

@@ -19,11 +19,6 @@ import java.util.Map;
@Configuration @Configuration
public class DruidConfig implements WebMvcConfigurer { public class DruidConfig implements WebMvcConfigurer {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
@ConfigurationProperties(prefix = "druid") @ConfigurationProperties(prefix = "druid")
@Bean @Bean

View File

@@ -1,6 +1,8 @@
package com.common.base.log; package com.common.base.log;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.common.base.config.FrameConfig;
import com.common.base.druid.DruidAuth;
import com.common.base.log.model.RequestErrorInfo; import com.common.base.log.model.RequestErrorInfo;
import com.common.base.log.model.RequestInfo; import com.common.base.log.model.RequestInfo;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
@@ -12,7 +14,10 @@ import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
@@ -30,12 +35,16 @@ import java.util.*;
public class RequestLogAspect { public class RequestLogAspect {
private final static Logger LOGGER = LoggerFactory.getLogger(RequestLogAspect.class); private final static Logger LOGGER = LoggerFactory.getLogger(RequestLogAspect.class);
public RequestLogAspect(FrameConfig frameConfig) {
this.frameConfig = frameConfig;
}
@Pointcut("execution(public * com..*.controller..*(..))") @Pointcut("execution(public * com..*.controller..*(..))")
public void requestServer() { public void requestServer() {
} }
@Value("${show-controller-log}") final
public boolean flag; FrameConfig frameConfig;
@Around("requestServer()") @Around("requestServer()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
@@ -45,7 +54,7 @@ public class RequestLogAspect {
HttpServletRequest request = attributes.getRequest(); HttpServletRequest request = attributes.getRequest();
Object result = proceedingJoinPoint.proceed(); Object result = proceedingJoinPoint.proceed();
if (flag) { if (frameConfig.getShowControllerLog()) {
RequestInfo requestInfo = new RequestInfo(); RequestInfo requestInfo = new RequestInfo();
requestInfo.setIp(request.getRemoteAddr()); requestInfo.setIp(request.getRemoteAddr());
requestInfo.setUrl(request.getRequestURL().toString()); requestInfo.setUrl(request.getRequestURL().toString());

View File

@@ -1,6 +1,7 @@
package com.common.base.shiro; package com.common.base.shiro;
import com.common.base.config.FrameConfig;
import com.common.base.shiro.service.ShiroService; import com.common.base.shiro.service.ShiroService;
import com.common.base.shiro.service.SystemRealm; import com.common.base.shiro.service.SystemRealm;
import com.common.base.utils.RedisUtil; import com.common.base.utils.RedisUtil;
@@ -8,6 +9,7 @@ import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Date; import java.util.Date;
@@ -21,6 +23,9 @@ public class CustomRealm extends AuthorizingRealm {
@Resource @Resource
private RedisUtil redisUtil; private RedisUtil redisUtil;
@Autowired
FrameConfig frameConfig;
@Resource @Resource
private SystemRealm systemRealm; private SystemRealm systemRealm;
@@ -35,8 +40,8 @@ public class CustomRealm extends AuthorizingRealm {
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) { protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
String accessToken = (String) token.getPrincipal(); String accessToken = (String) token.getPrincipal();
String userId; String userId;
if (RedisUtil.isUseful) { if (frameConfig.getIsRedisLogin()) {
Map<Object,Object> hashMap = redisUtil.hmget(accessToken); Map<Object,Object> hashMap = (Map<Object, Object>) redisUtil.hget("tokenSession",accessToken);
userId = (String) hashMap.get("userId"); userId = (String) hashMap.get("userId");
if (userId == null) { if (userId == null) {
throw new IncorrectCredentialsException("token失效请重新登录"); throw new IncorrectCredentialsException("token失效请重新登录");

View File

@@ -2,10 +2,12 @@ package com.common.base.shiro.service;
import com.common.base.common.ResultVO; import com.common.base.common.ResultVO;
import com.common.base.common.ServiceException; import com.common.base.common.ServiceException;
import com.common.base.config.FrameConfig;
import com.common.base.dao.FtTokenDao; import com.common.base.dao.FtTokenDao;
import com.common.base.shiro.FtToken; import com.common.base.shiro.FtToken;
import com.common.base.utils.RedisUtil; import com.common.base.utils.RedisUtil;
import com.common.base.utils.TokenProccessor; import com.common.base.utils.TokenProccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.entity.Example;
@@ -24,11 +26,13 @@ public class ShiroServiceImpl implements ShiroService {
*/ */
private static int EXPIRE; private static int EXPIRE;
@Value("${login-time-out}") private static Boolean isRedisLogin;
public void setHost(int hours) {
ShiroServiceImpl.EXPIRE = hours * 3600 * 1000;
}
@Autowired
public void setHost(FrameConfig frameConfig) {
ShiroServiceImpl.EXPIRE = frameConfig.getLoginTimeout() * 3600 * 1000;
isRedisLogin = frameConfig.getIsRedisLogin();
}
@Resource @Resource
FtTokenDao tokenMapper; FtTokenDao tokenMapper;
@@ -45,19 +49,19 @@ public class ShiroServiceImpl implements ShiroService {
*@param [userId] *@param [userId]
*@return Result *@return Result
*/ */
public ResultVO createToken(String userId,String token,Date expireTime) { public ResultVO createToken(String userId, String token, Date expireTime) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
FtToken tokenEntity = new FtToken(); FtToken tokenEntity = new FtToken();
Date now = new Date(); Date now = new Date();
// 是否使用redis存入token // 是否使用redis存入token
if (RedisUtil.isUseful) { if (isRedisLogin) {
boolean flag = redisUtil.hset("session", String.valueOf(userId), token,EXPIRE / 1000); boolean flag = redisUtil.hset("session", String.valueOf(userId), token, EXPIRE / 1000);
if (flag) { if (flag) {
Map<String, Object> map = new HashMap<>(2); Map<String, Object> map = new HashMap<>(2);
map.put("userId", userId); map.put("userId", userId);
map.put("expireTime",expireTime); map.put("expireTime", expireTime);
flag = redisUtil.hmset(token, map, EXPIRE / 1000); flag = redisUtil.hset("tokenSession",token, map, EXPIRE / 1000);
if (!flag) { if (!flag) {
throw new ServiceException("登陆失败"); throw new ServiceException("登陆失败");
} }
@@ -70,7 +74,7 @@ public class ShiroServiceImpl implements ShiroService {
tokenEntity = tokenMapper.selectOne(tokenEntity); tokenEntity = tokenMapper.selectOne(tokenEntity);
if (tokenEntity == null) { if (tokenEntity == null) {
tokenEntity = new FtToken(); tokenEntity = new FtToken();
tokenEntity.setTokenId(UUID.randomUUID().toString()+System.currentTimeMillis()); tokenEntity.setTokenId(UUID.randomUUID().toString() + System.currentTimeMillis());
tokenEntity.setUserId(userId); tokenEntity.setUserId(userId);
tokenEntity.setToken(token); tokenEntity.setToken(token);
tokenEntity.setUpdateTime(now); tokenEntity.setUpdateTime(now);
@@ -93,11 +97,11 @@ public class ShiroServiceImpl implements ShiroService {
@Override @Override
public ResultVO logout(String accessToken) { public ResultVO logout(String accessToken) {
if (RedisUtil.isUseful) { if (isRedisLogin) {
Map<Object,Object> map = redisUtil.hmget(accessToken); Map<Object, Object> map = (Map<Object, Object>) redisUtil.hget("tokenSession",accessToken);
Integer userId = (Integer) map.get("userId"); String userId = (String) map.get("userId");
redisUtil.del(accessToken); redisUtil.hdel("tokenSession",accessToken);
redisUtil.hdel("session", String.valueOf(userId)); redisUtil.hdel("session", userId);
return ResultVO.getSuccess(); return ResultVO.getSuccess();
} else { } else {
//生成一个token //生成一个token

View File

@@ -5,6 +5,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.common.base.config.FrameConfig;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -27,9 +28,9 @@ public final class RedisUtil {
public static boolean isUseful; public static boolean isUseful;
// 通过yml文件获取host // 通过yml文件获取host
@Value("${isRedisLogin}") @Autowired
public void setHost(boolean isUseful) { public void setHost(FrameConfig config) {
RedisUtil.isUseful = isUseful; RedisUtil.isUseful = config.getIsRedisLogin();
} }
private final static Logger logger = LoggerFactory.getLogger(RedisUtil.class); private final static Logger logger = LoggerFactory.getLogger(RedisUtil.class);