Commit 300991d5 authored by 技术部-任文超's avatar 技术部-任文超

优化log

parent e5f0a099
...@@ -171,16 +171,15 @@ public class CaptchaFiniteValidateAdvisor { ...@@ -171,16 +171,15 @@ public class CaptchaFiniteValidateAdvisor {
LOGGER.info("参数无效, credential:{}", credential); LOGGER.info("参数无效, credential:{}", credential);
return null; return null;
} }
// 当前用户手机号 // 当前用户手机号和密码
String phoneNo = credentialArr[0]; String phoneNo = credentialArr[0];
// 当前请求的SingleToken
String password = credentialArr[1]; String password = credentialArr[1];
headerParamValid = headerParamValid && ValidationUtil.validatePhoneNo(phoneNo) && StringUtils.isNotBlank(password); headerParamValid = headerParamValid && ValidationUtil.validatePhoneNo(phoneNo) && StringUtils.isNotBlank(password);
if (!headerParamValid) { if (!headerParamValid) {
LOGGER.info("参数无效, credential:{}, phoneNo:{}, password:{}", credential, phoneNo, password); LOGGER.info("参数无效, credential:{}, phoneNo:{}, password:{}", credential, phoneNo, password);
return null; return null;
} }
LOGGER.info("账密登录, phoneNo:{}, password:{}", phoneNo, password); LOGGER.info("账密登录, phoneNo:{}", phoneNo);
Map<String, String> phonePasswordMap = new HashMap<String, String>(2); Map<String, String> phonePasswordMap = new HashMap<String, String>(2);
phonePasswordMap.put("phoneNo", phoneNo); phonePasswordMap.put("phoneNo", phoneNo);
phonePasswordMap.put("password", password); phonePasswordMap.put("password", password);
......
...@@ -75,7 +75,7 @@ public class PasswordErrorFiniteValidateAdvisor { ...@@ -75,7 +75,7 @@ public class PasswordErrorFiniteValidateAdvisor {
// 黑名单 // 黑名单
if(redisTemplate.opsForSet().isMember(Constants.IPV4_LOCK_BLACK, clientIp)){ if(redisTemplate.opsForSet().isMember(Constants.IPV4_LOCK_BLACK, clientIp)){
IPUtil.logIp(LOGGER, request); IPUtil.logIp(LOGGER, request);
LOGGER.info("Lock_ipv4: locked ip access:{}", clientIp); LOGGER.info("Lock_ipv4: black ip access:{}", clientIp);
return JsonResult.buildErrorStateResult("登录失败", null); return JsonResult.buildErrorStateResult("登录失败", null);
} }
String lockIpv4Key = getLockIpv4Key(clientIp); String lockIpv4Key = getLockIpv4Key(clientIp);
......
...@@ -107,7 +107,7 @@ public class LockIpv4Controller implements IBaseController { ...@@ -107,7 +107,7 @@ public class LockIpv4Controller implements IBaseController {
minutes = Integer.valueOf(redisMinutes); minutes = Integer.valueOf(redisMinutes);
} }
redisTemplate.opsForValue().set(lockIpv4Key, Boolean.TRUE.toString(), minutes, TimeUnit.MINUTES); redisTemplate.opsForValue().set(lockIpv4Key, Boolean.TRUE.toString(), minutes, TimeUnit.MINUTES);
LOGGER.info("Lock_ipv4: locked ip access:{}, error overstep {} times in {} minutes, do lock {} minutes", ip, counts, Constants.IPV4_FAILED_COUNT_MINUTES, minutes); LOGGER.info("Lock_ipv4: locked ip Success. ip:{}, error overstep {} times in {} minutes, do lock {} minutes", ip, counts, Constants.IPV4_FAILED_COUNT_MINUTES, minutes);
}else{ }else{
redisTemplate.delete(lockIpv4Key); redisTemplate.delete(lockIpv4Key);
LOGGER.info("Lock_ipv4: unlocked ip Success. ip:{}", ip); LOGGER.info("Lock_ipv4: unlocked ip Success. ip:{}", ip);
......
...@@ -349,7 +349,8 @@ public class SmsController implements IBaseController { ...@@ -349,7 +349,8 @@ public class SmsController implements IBaseController {
try { try {
smsService.getSmsSender().sendMsg(message); smsService.getSmsSender().sendMsg(message);
redisTemplate.opsForValue().set(key, uniqueId + ":" + randomCode, EXPIRE_MINUTES, TimeUnit.MINUTES); redisTemplate.opsForValue().set(key, uniqueId + ":" + randomCode, EXPIRE_MINUTES, TimeUnit.MINUTES);
deleteRetSendCode(phoneNo);//删除用户重置密码,多次错误逻辑 //删除用户重置密码,多次错误逻辑
deleteRetSendCode(phoneNo);
if(needImageVlidate(clientIp,deviceId,phoneNo)){ if(needImageVlidate(clientIp,deviceId,phoneNo)){
return JsonResult.buildSuccessResult("发送成功", uniqueId,0003L); return JsonResult.buildSuccessResult("发送成功", uniqueId,0003L);
......
...@@ -84,10 +84,7 @@ public class UserController implements IBaseController { ...@@ -84,10 +84,7 @@ public class UserController implements IBaseController {
@RequestMapping("/test") @RequestMapping("/test")
public JsonResult test() { public JsonResult test() {
HttpServletRequest request = getRequest(); HttpServletRequest request = getRequest();
String remoteAddr = request.getRemoteAddr(); IPUtil.logIp(LOGGER, request);
String xRealIp = IPUtil.getRemoteIP(request);
String xOriginalClientIp = request.getHeader("x-original-client-ip");
LOGGER.info("Test ips:[client={}, old={}, new={}]", remoteAddr, xRealIp, xOriginalClientIp);
return JsonResult.buildSuccessResult("", getCurrentUserFromRedis()); return JsonResult.buildSuccessResult("", getCurrentUserFromRedis());
} }
...@@ -154,7 +151,7 @@ public class UserController implements IBaseController { ...@@ -154,7 +151,7 @@ public class UserController implements IBaseController {
@RequestParam(required = false,defaultValue = "xyqb") String key, @RequestParam(required = false,defaultValue = "xyqb") String key,
HttpServletRequest request, String openId, HttpServletRequest request, String openId,
@RequestParam(required = false) String dimension) { @RequestParam(required = false) String dimension) {
LOGGER.info("user/login,请求参数channelId:{},appChannel:{},createdFrom:{},userId:{},key:{},openId:{},dimension:{},",channelId,appChannel,createdFrom,userId,key,openId,dimension); LOGGER.info("login -> channelId:{},appChannel:{},createdFrom:{},userId:{},key:{},openId:{},dimension:{}",channelId,appChannel,createdFrom,userId,key,openId,dimension);
Merchant merchant = merchantService.findMerchantByName(key); Merchant merchant = merchantService.findMerchantByName(key);
if (merchant == null) { if (merchant == null) {
return JsonResult.buildErrorStateResult("未知的连接", null); return JsonResult.buildErrorStateResult("未知的连接", null);
...@@ -175,7 +172,7 @@ public class UserController implements IBaseController { ...@@ -175,7 +172,7 @@ public class UserController implements IBaseController {
@RequestParam(required = false)Long btRegisterChannelId, @RequestParam(required = false)Long btRegisterChannelId,
@RequestParam(required = false) String dimension ,HttpServletRequest request) { @RequestParam(required = false) String dimension ,HttpServletRequest request) {
Map<String, JsonResult> validMap = getHeaderParam(request); Map<String, JsonResult> validMap = getHeaderParam(request);
LOGGER.info("user/login/fast,请求参数channelId:{},appChannel:{},createdFrom:{},btRegisterChannelId:{},key:{},dimension:{},",channelId,appChannel,createdFrom,btRegisterChannelId,key,dimension); LOGGER.info("login/fast -> channelId:{},appChannel:{},createdFrom:{},btRegisterChannelId:{},key:{},dimension:{}",channelId,appChannel,createdFrom,btRegisterChannelId,key,dimension);
if (null != validMap.get("fail")) { if (null != validMap.get("fail")) {
return validMap.get("fail"); return validMap.get("fail");
} }
...@@ -208,9 +205,9 @@ public class UserController implements IBaseController { ...@@ -208,9 +205,9 @@ public class UserController implements IBaseController {
private User registerFastWhenLogin(String phoneNo, Long channelId, Long registerFrom, String appChannel, Long btRegisterChannelId,String dimension) { private User registerFastWhenLogin(String phoneNo, Long channelId, Long registerFrom, String appChannel, Long btRegisterChannelId,String dimension) {
String password = genRandomPwd(); String password = genRandomPwd();
LOGGER.info("用户快速注册, phoneNo:{}, channelId:{}, registerFrom:{},appChannel:{},btRegisterChannelId", phoneNo, channelId, registerFrom, appChannel,btRegisterChannelId); LOGGER.info("用户快速注册:login/fast -> phoneNo:{}, channelId:{}, registerFrom:{},appChannel:{},btRegisterChannelId:{}", phoneNo, channelId, registerFrom, appChannel,btRegisterChannelId);
if (!ValidationUtil.validatePhoneNo(phoneNo)) { if (!ValidationUtil.validatePhoneNo(phoneNo)) {
LOGGER.info("用户快速注册失败,手机号错误, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户快速注册失败,手机号错误:login/fast -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
throw new UserNotExistException("手机号错误"); throw new UserNotExistException("手机号错误");
} }
if (null == registerFrom) { if (null == registerFrom) {
...@@ -220,7 +217,7 @@ public class UserController implements IBaseController { ...@@ -220,7 +217,7 @@ public class UserController implements IBaseController {
registerFrom=222L; registerFrom=222L;
} }
User user=userService.registerAndReturn(phoneNo, password, registerFrom,btRegisterChannelId); User user=userService.registerAndReturn(phoneNo, password, registerFrom,btRegisterChannelId);
LOGGER.info("用户快速注册成功, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户快速注册成功:login/fast -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
MqUtils.sendRegisterMessage(channelId,dimension, user); MqUtils.sendRegisterMessage(channelId,dimension, user);
return user; return user;
} }
...@@ -276,17 +273,17 @@ public class UserController implements IBaseController { ...@@ -276,17 +273,17 @@ public class UserController implements IBaseController {
@RequestParam(required = false) Long registerFrom, @RequestParam(required = false, defaultValue = "") String appChannel, @RequestParam(required = false) Long registerFrom, @RequestParam(required = false, defaultValue = "") String appChannel,
@RequestParam(required = false)Long btRegisterChannelId,@RequestParam(required = false)String dimension) { @RequestParam(required = false)Long btRegisterChannelId,@RequestParam(required = false)String dimension) {
String password = genRandomPwd(); String password = genRandomPwd();
LOGGER.info("用户快速注册, phoneNo:{}, verificationCode:{}, channelId:{}, registerFrom:{},appChannel:{},btRegisterChannelId:{},dimension:{}", phoneNo, verificationCode, channelId, registerFrom, appChannel,btRegisterChannelId,dimension); LOGGER.info("用户快速注册:register/fast -> phoneNo:{}, verificationCode:{}, channelId:{}, registerFrom:{},appChannel:{},btRegisterChannelId:{},dimension:{}", phoneNo, verificationCode, channelId, registerFrom, appChannel,btRegisterChannelId,dimension);
if (!ValidationUtil.validatePhoneNo(phoneNo)) { if (!ValidationUtil.validatePhoneNo(phoneNo)) {
LOGGER.info("用户快速注册失败,手机号错误, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户快速注册失败,手机号错误:register/fast -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
return JsonResult.buildErrorStateResult("手机号错误", null); return JsonResult.buildErrorStateResult("手机号错误", null);
} }
if (StringUtils.isEmpty(password)) { if (StringUtils.isEmpty(password)) {
LOGGER.info("用户快速注册失败,密码不能为空, registerFrom:{}, phoneNo:{}, password:{}", registerFrom, phoneNo, password); LOGGER.info("用户快速注册失败,密码不能为空:register/fast -> registerFrom:{}, phoneNo:{}, password:{}", registerFrom, phoneNo, password);
return JsonResult.buildErrorStateResult("密码不能为空", null); return JsonResult.buildErrorStateResult("密码不能为空", null);
} }
if (password.length() < 6 || password.length() > 12) { if (password.length() < 6 || password.length() > 12) {
LOGGER.info("用户快速注册失败,密码长度须在6位至12位之间, registerFrom:{}, phoneNo:{}, password:{}", registerFrom, phoneNo, password); LOGGER.info("用户快速注册失败,密码长度须在6位至12位之间:register/fast -> registerFrom:{}, phoneNo:{}, password:{}", registerFrom, phoneNo, password);
return JsonResult.buildErrorStateResult("密码应为6-12位", null); return JsonResult.buildErrorStateResult("密码应为6-12位", null);
} }
if (null == registerFrom) { if (null == registerFrom) {
...@@ -294,14 +291,14 @@ public class UserController implements IBaseController { ...@@ -294,14 +291,14 @@ public class UserController implements IBaseController {
} }
smsValidForFastLogin(phoneNo, verificationCode); smsValidForFastLogin(phoneNo, verificationCode);
if (userService.exist(phoneNo)) { if (userService.exist(phoneNo)) {
LOGGER.info("用户快速注册失败,该手机号已经被注册, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户快速注册失败,该手机号已经被注册:register/fast -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
return JsonResult.buildErrorStateResult("该手机号已经被注册", null); return JsonResult.buildErrorStateResult("该手机号已经被注册", null);
} }
if (!userService.register(phoneNo, password, registerFrom, getIp(), channelId,btRegisterChannelId,dimension)) { if (!userService.register(phoneNo, password, registerFrom, getIp(), channelId,btRegisterChannelId,dimension)) {
LOGGER.info("用户快速注册失败,请稍后重试, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户快速注册失败,请稍后重试:register/fast -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
return JsonResult.buildErrorStateResult("注册失败,请稍后重试", null); return JsonResult.buildErrorStateResult("注册失败,请稍后重试", null);
} }
LOGGER.info("用户快速注册成功, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户快速注册成功:register/fast -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
return JsonResult.buildSuccessResult(null, null); return JsonResult.buildSuccessResult(null, null);
} }
...@@ -320,17 +317,17 @@ public class UserController implements IBaseController { ...@@ -320,17 +317,17 @@ public class UserController implements IBaseController {
@RequestParam String verificationCode, @RequestParam(required = false) Long channelId, @RequestParam String verificationCode, @RequestParam(required = false) Long channelId,
@RequestParam(required = false) Long registerFrom, @RequestParam(required = false) Long registerFrom,
@RequestParam(required = false)Long btRegisterChannelId,@RequestParam(required = false)String dimension) { @RequestParam(required = false)Long btRegisterChannelId,@RequestParam(required = false)String dimension) {
LOGGER.info("[/user/register]用户注册, phoneNo:{}, verificationCode:{}, channelId:{}, registerFrom:{},btRegisterChannelId:{},dimension:{}", phoneNo, verificationCode, channelId, registerFrom,btRegisterChannelId,dimension); LOGGER.info("用户注册:register -> phoneNo:{}, verificationCode:{}, channelId:{}, registerFrom:{},btRegisterChannelId:{},dimension:{}", phoneNo, verificationCode, channelId, registerFrom,btRegisterChannelId,dimension);
if (!ValidationUtil.validatePhoneNo(phoneNo)) { if (!ValidationUtil.validatePhoneNo(phoneNo)) {
LOGGER.info("用户注册失败,手机号错误, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户注册失败,手机号错误:register -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
return JsonResult.buildErrorStateResult("手机号错误", null); return JsonResult.buildErrorStateResult("手机号错误", null);
} }
if (StringUtils.isEmpty(password)) { if (StringUtils.isEmpty(password)) {
LOGGER.info("用户注册失败,密码不能为空, registerFrom:{}, phoneNo:{}, password:{}", registerFrom, phoneNo, password); LOGGER.info("用户注册失败,密码不能为空:register -> registerFrom:{}, phoneNo:{}, password:{}", registerFrom, phoneNo, password);
return JsonResult.buildErrorStateResult("密码不能为空", null); return JsonResult.buildErrorStateResult("密码不能为空", null);
} }
if (password.length() < 6 || password.length() > 12) { if (password.length() < 6 || password.length() > 12) {
LOGGER.info("用户注册失败,密码长度须在6位至12位之间, registerFrom:{}, phoneNo:{}, password:{}", registerFrom, phoneNo, password); LOGGER.info("用户注册失败,密码长度须在6位至12位之间:register -> registerFrom:{}, phoneNo:{}, password:{}", registerFrom, phoneNo, password);
return JsonResult.buildErrorStateResult("密码应为6-12位", null); return JsonResult.buildErrorStateResult("密码应为6-12位", null);
} }
if (null == registerFrom) { if (null == registerFrom) {
...@@ -338,14 +335,14 @@ public class UserController implements IBaseController { ...@@ -338,14 +335,14 @@ public class UserController implements IBaseController {
} }
smsValidForRegister(phoneNo, verificationCode); smsValidForRegister(phoneNo, verificationCode);
if (userService.exist(phoneNo)) { if (userService.exist(phoneNo)) {
LOGGER.info("用户注册失败,该手机号已经被注册, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户注册失败,该手机号已经被注册:register -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
return JsonResult.buildErrorStateResult("该手机号已经被注册", null); return JsonResult.buildErrorStateResult("该手机号已经被注册", null);
} }
if (!userService.register(phoneNo, password, registerFrom, getIp(), channelId,btRegisterChannelId,dimension)) { if (!userService.register(phoneNo, password, registerFrom, getIp(), channelId,btRegisterChannelId,dimension)) {
LOGGER.info("用户快速注册失败,请稍后重试, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户快速注册失败,请稍后重试:register -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
return JsonResult.buildErrorStateResult("注册失败,请稍后重试", null); return JsonResult.buildErrorStateResult("注册失败,请稍后重试", null);
} }
LOGGER.info("用户注册成功, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户注册成功:register -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
return JsonResult.buildSuccessResult(null, null); return JsonResult.buildSuccessResult(null, null);
} }
...@@ -437,12 +434,15 @@ public class UserController implements IBaseController { ...@@ -437,12 +434,15 @@ public class UserController implements IBaseController {
private String genRandomPwd() { private String genRandomPwd() {
int pwdMax = PWD_BASE.length; int pwdMax = PWD_BASE.length;
int i; // 生成的随机数 // 生成的随机数
int count = 0; // 生成的密码的长度 int i;
// 生成的密码的长度
int count = 0;
StringBuffer pwd = new StringBuffer(); StringBuffer pwd = new StringBuffer();
Random r = new Random(); Random r = new Random();
while (count < 15) { while (count < 15) {
i = Math.abs(r.nextInt(pwdMax)); // 生成的数最大为36-1 // 生成的数最大为36-1
i = Math.abs(r.nextInt(pwdMax));
if (i >= 0 && i < PWD_BASE.length) { if (i >= 0 && i < PWD_BASE.length) {
pwd.append(PWD_BASE[i]); pwd.append(PWD_BASE[i]);
count++; count++;
...@@ -464,23 +464,6 @@ public class UserController implements IBaseController { ...@@ -464,23 +464,6 @@ public class UserController implements IBaseController {
return JsonResult.buildSuccessResult("token校验成功", userModel); return JsonResult.buildSuccessResult("token校验成功", userModel);
} }
private User registerFastWhenLogin(String phoneNo, Long channelId, Long registerFrom, String appChannel) {
String password = genRandomPwd();
LOGGER.info("用户快速注册, phoneNo:{}, verificationCode:{}, channelId:{}, registerFrom:{},appChannel:{}", phoneNo, channelId, registerFrom, appChannel);
if (!ValidationUtil.validatePhoneNo(phoneNo)) {
LOGGER.info("用户快速注册失败,手机号错误, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
throw new UserNotExistException("手机号错误");
}
if (null == registerFrom) {
registerFrom = 1L;
}
User newUser = userService.registerAndReturn(phoneNo, password, registerFrom);
if(newUser != null && newUser.getId() != null && newUser.getId() > 0){
LOGGER.info("用户快速注册成功, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
}
return newUser;
}
private JsonResult loginWithHttpBasic(Long channelId, String appChannel, Long createdFrom, Merchant merchant, HttpServletRequest request, String openId,String dimension) { private JsonResult loginWithHttpBasic(Long channelId, String appChannel, Long createdFrom, Merchant merchant, HttpServletRequest request, String openId,String dimension) {
User user = verificateUserNameAndPassword(request, openId); User user = verificateUserNameAndPassword(request, openId);
if (user == null) { if (user == null) {
...@@ -516,7 +499,6 @@ public class UserController implements IBaseController { ...@@ -516,7 +499,6 @@ public class UserController implements IBaseController {
} }
String phoneNo = credentialArr[0]; String phoneNo = credentialArr[0];
String pass = credentialArr[1]; String pass = credentialArr[1];
LOGGER.info("用户正在登录... [{}]", phoneNo);
User user = userService.findByPhoneWithCache(phoneNo); User user = userService.findByPhoneWithCache(phoneNo);
if (user == null || !user.getEnable()) { if (user == null || !user.getEnable()) {
// 向该phoneNo添加错误计数器 // 向该phoneNo添加错误计数器
......
package cn.quantgroup.xyqb.interceptors; package cn.quantgroup.xyqb.interceptors;
import cn.quantgroup.xyqb.util.IPUtil; import cn.quantgroup.xyqb.util.IPUtil;
import cn.quantgroup.xyqb.util.ValidationUtil;
import com.google.common.collect.Sets;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
...@@ -10,16 +8,15 @@ import org.springframework.web.servlet.ModelAndView; ...@@ -10,16 +8,15 @@ import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.Set;
/** /**
* 内部服务白名单拦截器
* Created by Administrator on 2017/5/9. * Created by Administrator on 2017/5/9.
* @Modify by renwc
* @date 2017-12-06
*/ */
public class IPWhiteListInterceptor implements HandlerInterceptor { public class IPWhiteListInterceptor implements HandlerInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(IPWhiteListInterceptor.class); private static final Logger LOGGER = LoggerFactory.getLogger(IPWhiteListInterceptor.class);
private static final String [] allowIPs = {"139.198.7.123"};
private static Set<String> allowIPSet = Sets.newHashSet(allowIPs);
private Integer isDebug; private Integer isDebug;
public IPWhiteListInterceptor(Integer isDebug) { public IPWhiteListInterceptor(Integer isDebug) {
...@@ -32,10 +29,10 @@ public class IPWhiteListInterceptor implements HandlerInterceptor { ...@@ -32,10 +29,10 @@ public class IPWhiteListInterceptor implements HandlerInterceptor {
return true; return true;
} }
String remoteIP = IPUtil.getRemoteIP(request); String remoteIP = IPUtil.getRemoteIP(request);
if(IPUtil.whiteOf(remoteIP) || ValidationUtil.validateLocalIpv4(remoteIP) || allowIPSet.contains(remoteIP)) { if(IPUtil.whiteOf(remoteIP)) {
return true; return true;
} }
LOGGER.info("非法IP尝试访问,ip:[{}]",remoteIP); LOGGER.info("白名单不匹配拦截:ip={}",remoteIP);
return false; return false;
} }
......
...@@ -26,10 +26,11 @@ public class IPUtil { ...@@ -26,10 +26,11 @@ public class IPUtil {
* 172.20.0.0/16 - 3B私有云 * 172.20.0.0/16 - 3B私有云
* 172.30.0.0/16 - 3C私有云 * 172.30.0.0/16 - 3C私有云
*/ */
private static final Set<String> whiteAddr = Sets.newHashSet(); private static final Set<String> WHITE_ADDRESS = Sets.newHashSet();
private static final String LOCAL_ADDRESS = "127.0.0.1";
static { static {
String[] ips = {"172.16.", "172.20.", "172.30.", "192.168.3.", "192.168.4."}; String[] ips = {"172.16.", "172.20.", "172.30.", "192.168.3.", "192.168.4."};
whiteAddr.addAll(Arrays.asList(ips)); WHITE_ADDRESS.addAll(Arrays.asList(ips));
} }
/** /**
...@@ -39,7 +40,7 @@ public class IPUtil { ...@@ -39,7 +40,7 @@ public class IPUtil {
*/ */
public static final boolean whiteOf(String ipv4){ public static final boolean whiteOf(String ipv4){
if(ValidationUtil.validateIpv4(ipv4)){ if(ValidationUtil.validateIpv4(ipv4)){
for(String ipField : whiteAddr){ for(String ipField : WHITE_ADDRESS){
if(ipv4.startsWith(ipField)){ if(ipv4.startsWith(ipField)){
return true; return true;
} }
...@@ -71,7 +72,7 @@ public class IPUtil { ...@@ -71,7 +72,7 @@ public class IPUtil {
*/ */
public static String getRemoteIP(HttpServletRequest request) { public static String getRemoteIP(HttpServletRequest request) {
String ip = request.getHeader("x-original-client-ip"); String ip = request.getHeader("x-original-client-ip");
if (ValidationUtil.validateIpv4(ip) && !ip.startsWith("127.")) { if (ValidationUtil.validateIpv4(ip) && !Objects.equals(LOCAL_ADDRESS, ip)) {
return ip; return ip;
} }
......
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