Commit 08f2d3ae authored by 技术部-任文超's avatar 技术部-任文超

完成加锁/解锁/秘钥生成工具类

parent 37533248
...@@ -76,23 +76,9 @@ public class UserController implements IBaseController { ...@@ -76,23 +76,9 @@ public class UserController implements IBaseController {
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
@PasswordFineteValidator @RequestMapping("/test")
@RequestMapping("/login") public JsonResult test() {
public JsonResult login( return JsonResult.buildSuccessResult("", getCurrentUserFromRedis());
@RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel,
@RequestParam(required = false, defaultValue = "1") Long createdFrom,
@RequestParam(required = false, defaultValue = "") String userId, @RequestParam(required = false,defaultValue = "xyqb") String key, HttpServletRequest request, String openId,@RequestParam(required = false) String dimension) {
LOGGER.info("user/login,请求参数channelId:{},appChannel:{},createdFrom:{},userId:{},key:{},openId:{},dimension:{},",channelId,appChannel,createdFrom,userId,key,openId,dimension);
Merchant merchant = merchantService.findMerchantByName(key);
if (merchant == null) {
return JsonResult.buildErrorStateResult("未知的连接", null);
}
if (!StringUtils.isEmpty(userId) && userId.length() > 10) {
return loginWithUserId(channelId, appChannel, createdFrom, userId, merchant,dimension);
} else {
return loginWithHttpBasic(channelId, appChannel, createdFrom, merchant, request, openId,dimension);
}
} }
/** /**
...@@ -104,7 +90,7 @@ public class UserController implements IBaseController { ...@@ -104,7 +90,7 @@ public class UserController implements IBaseController {
* @return * @return
*/ */
@RequestMapping("/lock_ipv4") @RequestMapping("/lock_ipv4")
public JsonResult clearLockForIpv4(@RequestParam(required = true)String ip, public JsonResult clearOrLockIpv4(@RequestParam(required = true)String ip,
@RequestParam(required = false)String act, @RequestParam(required = false)String act,
@RequestParam(required = true)String key, @RequestParam(required = true)String key,
HttpServletRequest request) { HttpServletRequest request) {
...@@ -114,21 +100,41 @@ public class UserController implements IBaseController { ...@@ -114,21 +100,41 @@ public class UserController implements IBaseController {
} }
// 操作标记 // 操作标记
boolean lock = Objects.equals(Boolean.TRUE.toString(), act); boolean lock = Objects.equals(Boolean.TRUE.toString(), act);
// 解析密令 boolean valid = ValidationUtil.isValid(key, lock);
String header_key = request.getHeader(Constants.CLEAR_LOCK_FOR_IPV4);
byte[] buf = Base64.decodeBase64(header_key);
header_key = new String(buf, Charset.forName("UTF-8"));
boolean valid = ValidationUtil.isValid(header_key, lock);
if(valid){ if(valid){
String lockIpv4Key = getLockIpv4Key(ip); String lockIpv4Key = getLockIpv4Key(ip);
stringRedisTemplate.delete(lockIpv4Key); if(lock){
LOGGER.info("Clear_or_lock ip Success:{}", ip); stringRedisTemplate.opsForValue().set(lockIpv4Key, Boolean.TRUE.toString(), Constants.IPV4_LOCK_MINUTES, TimeUnit.MINUTES);
LOGGER.info("Locked ip access:{}, error overstep {} times in {} minutes, do lock {} minutes", ip, Constants.IPV4_LOCK_ON_COUNTS, Constants.IPV4_COUNT_MINUTES, Constants.IPV4_LOCK_MINUTES);
}else{
stringRedisTemplate.delete(lockIpv4Key);
LOGGER.info("Clear_or_lock ip Success:{}", ip);
}
return JsonResult.buildSuccessResult("Success",null); return JsonResult.buildSuccessResult("Success",null);
} }
LOGGER.info("Fail to clear_or_lock ip:{}", ip); LOGGER.info("Fail to clear_or_lock ip:{}", ip);
return JsonResult.buildErrorStateResult("Are you a robot monkey?(^_^)",null); return JsonResult.buildErrorStateResult("Are you a robot monkey?(^_^)",null);
} }
@PasswordFineteValidator
@RequestMapping("/login")
public JsonResult login(
@RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel,
@RequestParam(required = false, defaultValue = "1") Long createdFrom,
@RequestParam(required = false, defaultValue = "") String userId, @RequestParam(required = false,defaultValue = "xyqb") String key, HttpServletRequest request, String openId,@RequestParam(required = false) String dimension) {
LOGGER.info("user/login,请求参数channelId:{},appChannel:{},createdFrom:{},userId:{},key:{},openId:{},dimension:{},",channelId,appChannel,createdFrom,userId,key,openId,dimension);
Merchant merchant = merchantService.findMerchantByName(key);
if (merchant == null) {
return JsonResult.buildErrorStateResult("未知的连接", null);
}
if (!StringUtils.isEmpty(userId) && userId.length() > 10) {
return loginWithUserId(channelId, appChannel, createdFrom, userId, merchant,dimension);
} else {
return loginWithHttpBasic(channelId, appChannel, createdFrom, merchant, request, openId,dimension);
}
}
@RequestMapping("/login/fast") @RequestMapping("/login/fast")
public JsonResult loginFast( public JsonResult loginFast(
@RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel, @RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel,
......
...@@ -73,6 +73,21 @@ public class ValidationUtil { ...@@ -73,6 +73,21 @@ public class ValidationUtil {
return Objects.equals(key, MD5Util.build(_key.toString())); return Objects.equals(key, MD5Util.build(_key.toString()));
} }
/**
* 计算密令
* 私钥 + 操作 + 时
* @param key - Md5密令
* @param lock - true or false
* @return 加密后的Md5
*/
public static String getMd5Key(String key, boolean lock) {
Calendar today = Calendar.getInstance();
int hour_24 = today.get(Calendar.HOUR_OF_DAY);
StringBuilder _key = new StringBuilder();
_key.append(Constants.CLEAR_LOCK_FOR_IPV4_KEY).append(lock).append(hour_24);
return MD5Util.build(_key.toString());
}
public static boolean isAtDangerousTime() { public static boolean isAtDangerousTime() {
Calendar now = Calendar.getInstance(); Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY); int hour = now.get(Calendar.HOUR_OF_DAY);
......
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