【8.1.9】【security】更新校验黑白名单的方法

dev-8.1.9
stylefeng 2024-07-11 10:51:31 +08:00
parent 0f168e03ef
commit ae9f8687e1
2 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,101 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.security.blackwhite;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.security.blackwhite.exception.BlackWhiteExceptionEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* ipip
*
* @author fengshuonan
* @since 2024/7/11 10:14
*/
@Service
public class BlackWhiteValidateService {
@Resource
private BlackListService blackListService;
@Resource
private WhiteListService whiteListService;
/**
*
*
* @author fengshuonan
* @since 2024/7/11 10:44
*/
public void totalValidate(String ip) {
// 校验黑名单是否通过
if (!validateBlackList(ip)) {
throw new ServiceException(BlackWhiteExceptionEnum.IP_INVALID);
}
// 校验白名单是否通过
if (!validateWhiteList(ip)) {
throw new ServiceException(BlackWhiteExceptionEnum.IP_INVALID);
}
}
/**
* IP
*
* @return true-IPfalse-ip
* @author fengshuonan
* @since 2024/7/11 10:29
*/
public boolean validateBlackList(String ip) {
if (ObjectUtil.isEmpty(blackListService.getBlackList())) {
return true;
}
// 如果包含了指定ip则校验不通过
return !blackListService.contains(ip);
}
/**
* IP
*
* @return true-IPfalse-ip
* @author fengshuonan
* @since 2024/7/11 10:37
*/
public boolean validateWhiteList(String ip) {
if (ObjectUtil.isEmpty(whiteListService.getWhiteList())) {
return true;
}
// 如果不为空则白名单里包含了本IP才可以放行
return whiteListService.contains(ip);
}
}

View File

@ -0,0 +1,36 @@
package cn.stylefeng.roses.kernel.security.blackwhite.exception;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum;
import lombok.Getter;
/**
*
*
* @author fengshuonan
* @since 2024/7/11 10:42
*/
@Getter
public enum BlackWhiteExceptionEnum implements AbstractExceptionEnum {
/**
* IPIP
*/
IP_INVALID(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + "10001", "IP校验不通过IP已被限制");
/**
*
*/
private final String errorCode;
/**
*
*/
private final String userTip;
BlackWhiteExceptionEnum(String errorCode, String userTip) {
this.errorCode = errorCode;
this.userTip = userTip;
}
}