增加白名单配置
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package com.sunyard.chsm.config;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.sunyard.chsm.constant.ParamConfKeyConstant;
|
||||
import com.sunyard.chsm.enums.EnableStatus;
|
||||
import com.sunyard.chsm.mapper.IpWhitelisttMapper;
|
||||
import com.sunyard.chsm.mapper.ParamConfMapper;
|
||||
import com.sunyard.chsm.model.entity.IpWhitelist;
|
||||
import com.sunyard.chsm.model.entity.ParamConf;
|
||||
import com.sunyard.chsm.utils.IpUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/12/2
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class IpFilter extends OncePerRequestFilter implements ApplicationRunner {
|
||||
|
||||
public static List<String> whiteIps = new ArrayList<>();
|
||||
|
||||
@Resource
|
||||
private IpWhitelisttMapper ipWhitelisttMapper;
|
||||
@Resource
|
||||
private ParamConfMapper paramConfMapper;
|
||||
private Boolean enableWhiteIp = false;
|
||||
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
|
||||
String ip = IpUtils.getIpAddress(request);
|
||||
if (!enableWhiteIp || CollectionUtils.isEmpty(whiteIps) || whiteIps.contains(ip)) {
|
||||
chain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
||||
response.setContentType(MediaType.TEXT_PLAIN_VALUE);
|
||||
response.getWriter().println("此ip禁止访问");
|
||||
response.getWriter().flush();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
Executors.newSingleThreadScheduledExecutor()
|
||||
.scheduleWithFixedDelay(() -> {
|
||||
|
||||
ParamConf conf = paramConfMapper.selectByKey(ParamConfKeyConstant.IP_WHITELIST_SWITCH);
|
||||
if (conf != null) {
|
||||
enableWhiteIp = Objects.equals("true", conf.getValue());
|
||||
}
|
||||
if (enableWhiteIp) {
|
||||
List<IpWhitelist> list = ipWhitelisttMapper.selectList(
|
||||
new LambdaQueryWrapper<IpWhitelist>()
|
||||
.eq(IpWhitelist::getScope, "global")
|
||||
.eq(IpWhitelist::getStatus, EnableStatus.ENABLED.getCode())
|
||||
);
|
||||
whiteIps = list.stream().map(IpWhitelist::getIp).collect(Collectors.toList());
|
||||
}
|
||||
}, 0L, 5L, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package com.sunyard.chsm.constant;
|
||||
|
||||
/**
|
||||
* @author:fyc
|
||||
* @date:2020/3/17
|
||||
* @description: 业务参数key值常量
|
||||
*/
|
||||
public interface ParamConfKeyConstant {
|
||||
|
||||
/**
|
||||
* 白名单开关
|
||||
*/
|
||||
String IP_WHITELIST_SWITCH = "ipWhitelistSwitch";
|
||||
|
||||
/**
|
||||
* 通讯超时时间
|
||||
*/
|
||||
String COMMUNICATE_TIME_OUT = "communicateTimeOut";
|
||||
|
||||
/**
|
||||
* 心跳检测时间
|
||||
*/
|
||||
String HEART_DETECT_TIME = "heartDetectTime";
|
||||
|
||||
/**
|
||||
* 心跳包
|
||||
*/
|
||||
String HEART_PACKAGE = "heartPackage";
|
||||
|
||||
/**
|
||||
* ftp传输路径设置
|
||||
*/
|
||||
String FTP_UPLOAD_PATH = "ftpUploadPath";
|
||||
|
||||
/**
|
||||
* B端界面超时时间
|
||||
*/
|
||||
String B_INTERFACE_TIMEOUT = "BInterfaceTimeOut";
|
||||
|
||||
/**
|
||||
* 加密机操作审批
|
||||
*/
|
||||
String ENCRYPTION_MACHINE_APPROVAL = "encryptionMachineApproval";
|
||||
|
||||
/**
|
||||
* 秘钥操作审批
|
||||
*/
|
||||
String SECRET_KEY_APPROVAL = "secretKeyApproval";
|
||||
|
||||
/**
|
||||
* 登入模式
|
||||
*/
|
||||
String LOGIN_METHOD = "loginMethod";
|
||||
|
||||
/**
|
||||
* 是否开启图形码登入
|
||||
*/
|
||||
String IS_USE_GRAPHIC_CODE = "isUseGraphicCode";
|
||||
|
||||
/**
|
||||
* IP白名单配置类目数值
|
||||
*/
|
||||
Integer IP_WHITELIST_ITEM = 0;
|
||||
|
||||
/**
|
||||
* 通讯参数类目数值
|
||||
*/
|
||||
Integer COMMUNICATE_PARAM_ITEM = 1;
|
||||
|
||||
/**
|
||||
* 终端UI参数类目数值
|
||||
*/
|
||||
Integer TERMINAL_PARAM_ITEM = 2;
|
||||
|
||||
/**
|
||||
* 权限审批类目数值
|
||||
*/
|
||||
Integer AUTHORITY_APPROVAL_PARAM_ITEM = 3;
|
||||
|
||||
/**
|
||||
* 登入选项类目数值
|
||||
*/
|
||||
Integer LOGIN_PARAM_ITEM = 4;
|
||||
|
||||
/**
|
||||
* 审批结果true
|
||||
*/
|
||||
String APPROVAL_TRUE = "true";
|
||||
|
||||
/**
|
||||
* 审批结果false
|
||||
*/
|
||||
String APPROVAL_FALSE = "false";
|
||||
|
||||
/**
|
||||
* 调试模式类目数值
|
||||
*/
|
||||
Integer SYS_PARAM_ITEM = 7;
|
||||
|
||||
/**
|
||||
* 调试模式开关
|
||||
*/
|
||||
String SYS_DEBUG_SWITCH = "sysDebugSwitch";
|
||||
|
||||
/**
|
||||
* 系统初始化配置文件路劲
|
||||
*/
|
||||
String SYS_PARAM_CONFIG_FILE_PATH = System.getProperty("user.dir") + "/config/sysParam.config.json";
|
||||
|
||||
/**
|
||||
* 调试模式开关枚举
|
||||
*/
|
||||
enum SYS_DEBUG_SWITCH_VALUE{
|
||||
DEV("dev"), CONFIG("config"), PRODUCT("product");
|
||||
private String value;
|
||||
private SYS_DEBUG_SWITCH_VALUE(String value){
|
||||
this.value = value;
|
||||
}
|
||||
public String getValue(){
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.sunyard.chsm.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.sunyard.chsm.model.entity.ParamConf;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author fyc
|
||||
* @since 2020-03-17
|
||||
*/
|
||||
@Repository
|
||||
public interface ParamConfMapper extends BaseMapper<ParamConf> {
|
||||
|
||||
@Select("<script>"+
|
||||
" select * from SC_PARAM_CONF where KEY = #{key}"+
|
||||
"</script>")
|
||||
ParamConf selectByKey(@Param("key")String key);
|
||||
|
||||
@Select("<script>"+
|
||||
" select * from SC_PARAM_CONF where item = #{item}"+
|
||||
"</script>")
|
||||
List<ParamConf> selectByItem(@Param("item") Integer item);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.sunyard.chsm.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* </p>
|
||||
*
|
||||
* @author fyc
|
||||
* @since 2020-03-17
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
@TableName("SC_PARAM_CONF")
|
||||
public class ParamConf implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@TableId("ID")
|
||||
private Long id;
|
||||
|
||||
@TableField("ITEM")
|
||||
private Integer item;
|
||||
|
||||
// @TableField("`KEY`") mysql需加
|
||||
@TableField("KEY")
|
||||
private String key;
|
||||
|
||||
@TableField("VALUE")
|
||||
private String value;
|
||||
|
||||
@TableField("TYPE")
|
||||
private String type;
|
||||
|
||||
@TableField("STATUS")
|
||||
private Integer status;
|
||||
|
||||
@TableField("CREATE_TIME")
|
||||
private LocalDateTime creatTime;
|
||||
|
||||
@TableField("MEMO")
|
||||
private String memo;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.sunyard.chsm.utils;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
public class IpUtils {
|
||||
|
||||
|
||||
/**
|
||||
* 获取真实ip地址,避免获取代理ip
|
||||
*/
|
||||
public static String getIpAddress(HttpServletRequest request) {
|
||||
if (request == null) {
|
||||
return "unknown";
|
||||
}
|
||||
String ip = request.getHeader("x-forwarded-for");
|
||||
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("X-Forwarded-For");
|
||||
if (StringUtils.hasText(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
// 多次反向代理后会有多个IP值,第一个为真实IP。
|
||||
int index = ip.indexOf(',');
|
||||
if (index != -1) {
|
||||
ip = ip.substring(0, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("X-Real-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
if("0:0:0:0:0:0:0:1".equals(ip)){
|
||||
return "127.0.0.1";
|
||||
}else {
|
||||
if(ip.equals("127.0.0.1") || ip.equalsIgnoreCase("localhost") && ObjectUtils.isEmpty(request.getRemoteAddr())){
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user