| @@ -29,9 +29,15 @@ import org.apache.shiro.authc.UnknownAccountException; | |||||
| import org.apache.shiro.subject.Subject; | import org.apache.shiro.subject.Subject; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.beans.factory.annotation.Qualifier; | import org.springframework.beans.factory.annotation.Qualifier; | ||||
| import org.springframework.dao.DataAccessException; | |||||
| import org.springframework.data.redis.connection.RedisConnection; | |||||
| import org.springframework.data.redis.core.RedisCallback; | |||||
| import org.springframework.data.redis.core.RedisTemplate; | import org.springframework.data.redis.core.RedisTemplate; | ||||
| import org.springframework.data.redis.serializer.RedisSerializer; | |||||
| import org.springframework.stereotype.Controller; | import org.springframework.stereotype.Controller; | ||||
| import org.springframework.web.bind.annotation.*; | import org.springframework.web.bind.annotation.*; | ||||
| import redis.clients.jedis.Protocol; | |||||
| import redis.clients.util.SafeEncoder; | |||||
| import javax.servlet.http.Cookie; | import javax.servlet.http.Cookie; | ||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
| @@ -202,9 +208,19 @@ public class WechatLoginController extends BaseController { | |||||
| } else { | } else { | ||||
| // 跳转登录选择页面 | // 跳转登录选择页面 | ||||
| String key = WECHAT_PREV + TOTP.generateWechatOpen(accessToken.getOpenId()); | String key = WECHAT_PREV + TOTP.generateWechatOpen(accessToken.getOpenId()); | ||||
| openRedisTemplate.opsForValue().setIfAbsent(key, accessToken.getOpenId()); | |||||
| // 设置过期时间 | |||||
| openRedisTemplate.expire(key, 60, TimeUnit.SECONDS); | |||||
| Boolean isAbsent = openRedisTemplate.<Boolean>execute(new RedisCallback<Boolean>() { | |||||
| @Override | |||||
| public Boolean doInRedis(RedisConnection connection) throws DataAccessException { | |||||
| RedisSerializer valueSerializer = openRedisTemplate.getValueSerializer(); | |||||
| RedisSerializer keySerializer = openRedisTemplate.getKeySerializer(); | |||||
| Object obj = connection.execute("set", keySerializer.serialize(key), | |||||
| valueSerializer.serialize(key), | |||||
| SafeEncoder.encode("NX"), | |||||
| SafeEncoder.encode("EX"), | |||||
| Protocol.toByteArray(60)); // 60s 过期时间 | |||||
| return obj != null; | |||||
| } | |||||
| }); | |||||
| List<DisplayUserInfo> users = new ArrayList<DisplayUserInfo>(); | List<DisplayUserInfo> users = new ArrayList<DisplayUserInfo>(); | ||||
| for(MallUserInfoVo user: userList) { | for(MallUserInfoVo user: userList) { | ||||
| DisplayUserInfo disUser = new DisplayUserInfo(); | DisplayUserInfo disUser = new DisplayUserInfo(); | ||||
| @@ -7,10 +7,16 @@ import com.iformall.utils.IPUtil; | |||||
| import com.iformall.utils.UrlCheck; | import com.iformall.utils.UrlCheck; | ||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| import org.springframework.dao.DataAccessException; | |||||
| import org.springframework.data.redis.connection.RedisConnection; | |||||
| import org.springframework.data.redis.core.RedisCallback; | |||||
| import org.springframework.data.redis.core.RedisTemplate; | import org.springframework.data.redis.core.RedisTemplate; | ||||
| import org.springframework.data.redis.serializer.RedisSerializer; | |||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||
| import org.springframework.web.servlet.ModelAndView; | import org.springframework.web.servlet.ModelAndView; | ||||
| import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||||
| import redis.clients.jedis.Protocol; | |||||
| import redis.clients.util.SafeEncoder; | |||||
| import javax.annotation.Resource; | import javax.annotation.Resource; | ||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
| @@ -78,8 +84,20 @@ public class RequestInterceptor extends HandlerInterceptorAdapter { | |||||
| sb.append(resultBody); | sb.append(resultBody); | ||||
| String key = "request-" + HashUtil.md5(sb.toString()); | String key = "request-" + HashUtil.md5(sb.toString()); | ||||
| if (redisTemplate.opsForValue().setIfAbsent(key, key)) { | |||||
| redisTemplate.expire(key, 3, TimeUnit.SECONDS); | |||||
| Boolean isAbsent = redisTemplate.<Boolean>execute(new RedisCallback<Boolean>() { | |||||
| @Override | |||||
| public Boolean doInRedis(RedisConnection connection) throws DataAccessException { | |||||
| RedisSerializer valueSerializer = redisTemplate.getValueSerializer(); | |||||
| RedisSerializer keySerializer = redisTemplate.getKeySerializer(); | |||||
| Object obj = connection.execute("set", keySerializer.serialize(key), | |||||
| valueSerializer.serialize(key), | |||||
| SafeEncoder.encode("NX"), | |||||
| SafeEncoder.encode("EX"), | |||||
| Protocol.toByteArray(3)); // 3s | |||||
| return obj != null; | |||||
| } | |||||
| }); | |||||
| if (isAbsent) { | |||||
| logger.info(key + ": 第一次提交"); | logger.info(key + ": 第一次提交"); | ||||
| return true; | return true; | ||||
| } | } | ||||
| @@ -4,10 +4,16 @@ import com.iformall.utils.HashUtil; | |||||
| import com.iformall.utils.IPUtil; | import com.iformall.utils.IPUtil; | ||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| import org.springframework.dao.DataAccessException; | |||||
| import org.springframework.data.redis.connection.RedisConnection; | |||||
| import org.springframework.data.redis.core.RedisCallback; | |||||
| import org.springframework.data.redis.core.RedisTemplate; | import org.springframework.data.redis.core.RedisTemplate; | ||||
| import org.springframework.data.redis.serializer.RedisSerializer; | |||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||
| import org.springframework.web.servlet.ModelAndView; | import org.springframework.web.servlet.ModelAndView; | ||||
| import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||||
| import redis.clients.jedis.Protocol; | |||||
| import redis.clients.util.SafeEncoder; | |||||
| import javax.annotation.Resource; | import javax.annotation.Resource; | ||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
| @@ -70,8 +76,20 @@ public class RequestInterceptor extends HandlerInterceptorAdapter { | |||||
| sb.append(resultBody); | sb.append(resultBody); | ||||
| String key = "request-" + HashUtil.md5(sb.toString()); | String key = "request-" + HashUtil.md5(sb.toString()); | ||||
| if (redisTemplate.opsForValue().setIfAbsent(key, key)) { | |||||
| redisTemplate.expire(key, 3, TimeUnit.SECONDS); | |||||
| Boolean isAbsent = redisTemplate.<Boolean>execute(new RedisCallback<Boolean>() { | |||||
| @Override | |||||
| public Boolean doInRedis(RedisConnection connection) throws DataAccessException { | |||||
| RedisSerializer valueSerializer = redisTemplate.getValueSerializer(); | |||||
| RedisSerializer keySerializer = redisTemplate.getKeySerializer(); | |||||
| Object obj = connection.execute("set", keySerializer.serialize(key), | |||||
| valueSerializer.serialize(key), | |||||
| SafeEncoder.encode("NX"), | |||||
| SafeEncoder.encode("EX"), | |||||
| Protocol.toByteArray(3)); // 3s | |||||
| return obj != null; | |||||
| } | |||||
| }); | |||||
| if (isAbsent) { | |||||
| logger.info(key + ": 第一次提交"); | logger.info(key + ": 第一次提交"); | ||||
| return true; | return true; | ||||
| } | } | ||||
| @@ -4,10 +4,16 @@ import com.iformall.utils.HashUtil; | |||||
| import com.iformall.utils.IPUtil; | import com.iformall.utils.IPUtil; | ||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| import org.springframework.dao.DataAccessException; | |||||
| import org.springframework.data.redis.connection.RedisConnection; | |||||
| import org.springframework.data.redis.core.RedisCallback; | |||||
| import org.springframework.data.redis.core.RedisTemplate; | import org.springframework.data.redis.core.RedisTemplate; | ||||
| import org.springframework.data.redis.serializer.RedisSerializer; | |||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||
| import org.springframework.web.servlet.ModelAndView; | import org.springframework.web.servlet.ModelAndView; | ||||
| import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||||
| import redis.clients.jedis.Protocol; | |||||
| import redis.clients.util.SafeEncoder; | |||||
| import javax.annotation.Resource; | import javax.annotation.Resource; | ||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
| @@ -72,8 +78,20 @@ public class RequestInterceptor extends HandlerInterceptorAdapter { | |||||
| sb.append(resultBody); | sb.append(resultBody); | ||||
| String key = "request-" + HashUtil.md5(sb.toString()); | String key = "request-" + HashUtil.md5(sb.toString()); | ||||
| if (redisTemplate.opsForValue().setIfAbsent(key, key)) { | |||||
| redisTemplate.expire(key, 3, TimeUnit.SECONDS); | |||||
| Boolean isAbsent = redisTemplate.<Boolean>execute(new RedisCallback<Boolean>() { | |||||
| @Override | |||||
| public Boolean doInRedis(RedisConnection connection) throws DataAccessException { | |||||
| RedisSerializer valueSerializer = redisTemplate.getValueSerializer(); | |||||
| RedisSerializer keySerializer = redisTemplate.getKeySerializer(); | |||||
| Object obj = connection.execute("set", keySerializer.serialize(key), | |||||
| valueSerializer.serialize(key), | |||||
| SafeEncoder.encode("NX"), | |||||
| SafeEncoder.encode("EX"), | |||||
| Protocol.toByteArray(3)); // 3s | |||||
| return obj != null; | |||||
| } | |||||
| }); | |||||
| if (isAbsent) { | |||||
| logger.info(key + ": 第一次提交"); | logger.info(key + ": 第一次提交"); | ||||
| logger.debug("preHandle start 2"); | logger.debug("preHandle start 2"); | ||||
| return true; | return true; | ||||