新增框架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>
<version>3.32.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<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
public class DruidConfig implements WebMvcConfigurer {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
@ConfigurationProperties(prefix = "druid")
@Bean

View File

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

View File

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

View File

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