提交 df91c2b8 编写于 作者: Skyeye云's avatar Skyeye云

添加黑名单过滤

上级 f4620da8
...@@ -25,6 +25,11 @@ public class Constants { ...@@ -25,6 +25,11 @@ public class Constants {
".js", ".gif", ".jpg", ".eot", ".svg", ".ttf", ".woff", ".woff2", ".js", ".gif", ".jpg", ".eot", ".svg", ".ttf", ".woff", ".woff2",
".mp4", ".rmvb", ".avi", "3gp", ".html", ".less", ".otf", ".scss", ".mp4", ".rmvb", ".avi", "3gp", ".html", ".less", ".otf", ".scss",
".ico" }; ".ico" };
/**
* IP过滤
*/
public static final String[] FILTER_FILE_IP_OPTION = { "0:0:0:0:0:0:0:1", "127.0.0.1"};
/** /**
* 过滤器过滤请求类型项 * 过滤器过滤请求类型项
......
package com.skyeye.common.interceptor; package com.skyeye.common.interceptor;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import com.skyeye.common.constans.Constants;
import com.skyeye.common.object.ObjectConstant; import com.skyeye.common.object.ObjectConstant;
import com.skyeye.common.object.OutputObject; import com.skyeye.common.object.OutputObject;
import com.skyeye.common.object.PutObject; import com.skyeye.common.object.PutObject;
import com.skyeye.common.util.ToolUtil;
/** /**
...@@ -23,10 +28,47 @@ public class HandlerInterceptorMain implements HandlerInterceptor{ ...@@ -23,10 +28,47 @@ public class HandlerInterceptorMain implements HandlerInterceptor{
//在进入Handler方法之前执行   //在进入Handler方法之前执行  
//用于身份认证、身份授权、   //用于身份认证、身份授权、  
//比如身份认证,如果认证不通过表示当前用户没有登录,需要此方法拦截不再向下执行 //比如身份认证,IP黑名单过滤,如果认证不通过表示当前用户没有登录,需要此方法拦截不再向下执行
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
new PutObject(request, response); new PutObject(request, response);
/**IP黑名单过滤开始**/
String ip;
ip = request.getHeader("x-forwarded-for");
// 针对IP是否使用代理访问进行处理
if (ToolUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ToolUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ToolUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (ip != null && ip.indexOf(",") != -1) {
ip = ip.substring(0, ip.indexOf(","));
}
for(String str : Constants.FILTER_FILE_IP_OPTION) {
if (ip.indexOf(str) != -1) {
ip = request.getParameter("loginPCIp");
break;
}
}
Properties prop = new Properties();
String path = ToolUtil.getIPPropertiesPath();
InputStream in = new BufferedInputStream(new FileInputStream(path));
prop.load(in); /// 加载属性列表
Iterator<String> it = prop.stringPropertyNames().iterator();
while (it.hasNext()) {
String key = it.next();
if (prop.getProperty(key).equals(ip)) {
return false;
}
}
in.close();
/**IP黑名单过滤结束**/
HttpServletRequest servletRequest = (HttpServletRequest) request; HttpServletRequest servletRequest = (HttpServletRequest) request;
request.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
......
...@@ -4,6 +4,11 @@ import java.io.Serializable; ...@@ -4,6 +4,11 @@ import java.io.Serializable;
public class SearchItem implements Serializable { public class SearchItem implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String id; private String id;
private String title; private String title;
private String sell_point; private String sell_point;
......
package com.skyeye.common.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
/**
*
* @ClassName: PropertiesUtil
* @Description: 读写配置文件
* @author 卫志强
* @date 2018年10月08日
*
*/
public class PropertiesUtil {
public static Properties prop = new Properties();
/**
* 取出值
*
* @param k key键
* @param filepath properties文件路径
* @return
*/
public static String getValue(String k, String filepath) {
InputStream in;
try {
in = new BufferedInputStream(new FileInputStream(filepath));
prop.load(in); /// 加载属性列表
Iterator<String> it = prop.stringPropertyNames().iterator();
while (it.hasNext()) {
String key = it.next();
if (key.equals(k)) {
return prop.getProperty(key);
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 获取文件中的key-value的size
*
* @param filepath properties文件路径
* @return
*/
public static int getPropertiesSize(String filepath) {
InputStream in;
int size = 0;
try {
in = new BufferedInputStream(new FileInputStream(filepath));
prop.load(in); /// 加载属性列表
Iterator<String> it = prop.stringPropertyNames().iterator();
while (it.hasNext()) {
size++;
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return size;
}
/**
* 设置键值
* @param filepath properties文件路径
* @param key key键
* @param value value值
*/
public static void writeProperties(String filepath, String key, String value){
///保存属性到properties文件
FileOutputStream oFile ;
try {
oFile = new FileOutputStream(filepath, true);//true表示追加打开
prop.setProperty(key, value);
prop.store(oFile, "Update IPproperties value");
oFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
...@@ -392,7 +392,21 @@ public class ToolUtil { ...@@ -392,7 +392,21 @@ public class ToolUtil {
} }
return treeNode; return treeNode;
} }
/**
* 获取ip.properties路径
* @return
*/
public static String getIPPropertiesPath(){
String contextPath = new Object() {
public String getPath() {
return this.getClass().getResource("/").getPath();
}
}.getPath().substring(1);
System.out.println(contextPath);
String path = contextPath + "/properties/ip.properties";
return path;
}
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
String str = "123456"; String str = "123456";
......
...@@ -17,4 +17,5 @@ getProperties(); 读取.properties 结尾的配置文件用,getP, getPara ...@@ -17,4 +17,5 @@ getProperties(); 读取.properties 结尾的配置文件用,getP, getPara
getSurFaceId(); 获取ID getSurFaceId(); 获取ID
MD5(); 加密 MD5(); 加密
allMenuToTree(); 使用递归方法建树 allMenuToTree(); 使用递归方法建树
findChildren(); 递归查找子节点 findChildren(); 递归查找子节点
\ No newline at end of file getIPPropertiesPath(); 获取ip.properties路径
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册