Commit 2b35e38b by yangyang

湖南用户10分钟未操作处理(湖南)

parent 86987cb8
......@@ -21,17 +21,23 @@ public class Token {
* 密码
*/
private String password;
/**
* 证件号码
*/
private String identitycard;
public Token(){}
public Token(String username,String ip,String password){
public Token(String username,String ip,String password,String identitycard){
this.username=username;
this.ip=ip;
this.password=password;
this.identitycard=identitycard;
}
public Token(JSONObject object) {
this.username= (String) object.get("username");
this.ip=(String) object.get("ip");
this.password=(String) object.get("password");
this.identitycard=(String) object.get("identitycard");
}
}
package com.founder.servicebase.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
@Configuration
public class RedisConfig {
/**
* 创建redis连接工厂
*/
public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int index) {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName(hostName);
configuration.setPort(port);
configuration.setPassword(RedisPassword.of(password));
configuration.setDatabase(index);
LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration);
factory.afterPropertiesSet();
return factory;
}
}
package com.founder.servicebase.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
//@EnableCaching
@Configuration
public class TwoRedisConfig extends RedisConfig{
@Value("${spring.redis2.database}")
private int dbIndex;
@Value("${spring.redis2.host}")
private String host;
@Value("${spring.redis2.port}")
private int port;
@Value("${spring.redis2.password}")
private String password;
/**
* 配置redisTemplate 注入方式使用@Resource(name="") 方式注入
*/
@Bean(name = "twoRedisTemplate")
public StringRedisTemplate redisTemplate() {
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(
connectionFactory(host, port, password,dbIndex));
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
......@@ -12,6 +12,8 @@ import com.founder.servicebase.logs.OperationType;
import com.founder.servicebase.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -23,6 +25,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.founder.commonutils.util.AesEncryptUtil.desEncrypt;
......@@ -41,7 +44,8 @@ public class SysUserController extends ApiController {
*/
@Resource
private SysUserService sysUserService;
@Resource(name = "twoRedisTemplate")
private StringRedisTemplate redisTemplate;
@ApiOperation(value = "登录")
@OperLog(message = "登录",operation = OperationType.LOGIN)
......@@ -79,7 +83,7 @@ public class SysUserController extends ApiController {
JwtUser util = new JwtUser();
String ip = request.getRemoteAddr();
//密码加密与刑专一致
Token tokenEntity=new Token(one.getUsername(),one.getIp(), DigestUtil.encrypt(password));
Token tokenEntity=new Token(one.getUsername(),one.getIp(), DigestUtil.encrypt(password),one.getIdentitycard());
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(tokenEntity);
//用户JWT生成token
String token = util.createJWT(jsonObject.toString());
......@@ -121,11 +125,13 @@ public class SysUserController extends ApiController {
if (one == null) {
return MapRestResult.build(500, "该用户不存在", null);
}
// 十分钟未操作
String key = "userIdleTime:" + identityCard;
redisTemplate.opsForValue().set(key, "1", 10, TimeUnit.MINUTES);
// 拿到真实ip
one.setIp(NetworkUtil.getIpAddr(request));
JwtUser util = new JwtUser();
String ip = request.getRemoteAddr();
Token tokenEntity=new Token(one.getUsername(),one.getIp(),one.getPassword());
Token tokenEntity=new Token(one.getUsername(),one.getIp(),one.getPassword(),one.getIdentitycard());
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(tokenEntity);
// 用户JWT生成token
String token = util.createJWT(jsonObject.toString());
......
......@@ -10,6 +10,7 @@ import com.founder.commonutils.model.newPublicEntity.Token;
import com.founder.commonutils.util.JwtUser;
import com.founder.commonutils.util.KeyUtil;
import com.founder.commonutils.util.NetworkUtil;
import com.founder.commonutils.util.ResultUtil;
import com.founder.servicebase.logs.mapper.mysqlMapper.SkSysLogMapper;
import io.jsonwebtoken.Claims;
import lombok.SneakyThrows;
......@@ -23,17 +24,21 @@ import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.founder.commonutils.util.AesEncryptUtil.desEncrypt;
......@@ -49,7 +54,8 @@ public class SysLogAspect {
@Autowired
private SkSysLogMapper sysLogMapper;
private static Logger LOG = LoggerFactory.getLogger(SysLogAspect.class);
@Resource(name = "twoRedisTemplate")
private StringRedisTemplate redisTemplate;
//定义切点 @Pointcut
//在注解的位置切入代码
//@Pointcut("@within(OperLog)")
......@@ -138,6 +144,15 @@ public class SysLogAspect {
System.out.println("token===>" + token);
sysLog.setUserid(token.getUsername());
sysLog.setLx("1");
String key = "userIdleTime:" + token.getIdentitycard();
System.out.println("userIdleTime:key=="+key+"&&value=="+redisTemplate.opsForValue().get(key));
if (!redisTemplate.hasKey(key)){
String msg = "用户10分钟未操作!";
Map<String, Object> mapResult = getMap(401, msg);
ResultUtil.responseJson(attributes.getResponse(), mapResult);
return false;
}
redisTemplate.opsForValue().set(key, "1", 10, TimeUnit.MINUTES);//存入redis并设置过期时间
} catch (
TokenException e) {
sysLog.setUserid("error");
......@@ -247,4 +262,11 @@ public class SysLogAspect {
"\n------------------\n\n");
return content;
}
public Map<String, Object> getMap(Integer status, String msg) {
Map<String, Object> map = new HashMap<>();
map.put("code", status);
map.put("msg", msg);
return map;
}
}
......@@ -30,8 +30,8 @@ public class PublicapiApplication {
@Bean
public RestTemplate restTemplate(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(10000);
factory.setReadTimeout(10000);
factory.setConnectTimeout(100000);
factory.setReadTimeout(100000);
RestTemplate restTemplate = new RestTemplate(factory);
return restTemplate;
}
......
......@@ -27,6 +27,11 @@ spring.redis.host=47.92.225.109
spring.redis.port=3001
spring.redis.password=CcXzxt
spring.redis2.host=47.92.225.109
spring.redis2.port=3001
spring.redis2.password=CcXzxt
spring.redis2.database=10
#\u6570\u636E\u5206\u6790\u4E0A\u4F20\u7684\u8DEF\u5F84
hnkshFilePath=/data/excel/kshExcel/
#\u6570\u636E\u5206\u6790\u4E0B\u8F7D\u7684\u8DEF\u5F84
......
......@@ -26,6 +26,10 @@ spring.redis.port=4379
spring.redis.database=5
spring.redis.password=ENC(LDz9F1qeGmbTMTtoQj7ADA==)
spring.redis2.host=10.142.16.189
spring.redis2.port=4379
spring.redis2.password=ENC(UobMbjASghUGMb9FBnhnCg==)
spring.redis2.database=0
#\u6570\u636E\u5206\u6790\u4E0A\u4F20\u7684\u8DEF\u5F84
hnkshFilePath=/data/excel/kshExcel/
#\u6570\u636E\u5206\u6790\u4E0B\u8F7D\u7684\u8DEF\u5F84
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment