@Aspect
@Component
public class ValidateAspect {
@Pointcut("@annotation(com.nudt.annotation.Validate)")
public void validatePointCut() {
}
@Before("validatePointCut()")
public void before(JoinPoint joinPoint) throws Exception{
int status = IPUtil.getStatus();
if(status==0){
throw new ValidationException("未登录");
}
if(status==1){
throw new ValidationException("无权限");
}
}
}
3、使用
复制代码
@GetMapping
@Validate
public String del(){
}
工具类:获取服务器和客户端ip,以及在session中获取信息
复制代码
public class IPUtil {
private static final Logger log = LoggerFactory.getLogger(IPUtil.class);
//获取服务器ip
public static String getServerIp() {
try{
InetAddress localhost = InetAddress.getLocalHost();
return localhost.getHostAddress();
}catch(UnknownHostException e){
throw new RuntimeException(e);
}
}
//获取客户端ip
public static String getClientIp() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = request.getHeader("x-forwarded-for");
log.info("x-forwarded-for ip:{}",ip);
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
log.info("Proxy-Client-IP ip:{}",ip);
}
if (StringUtils.isBlank(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
log.info("WL-Proxy-Client-IP ip:{}",ip);
}
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
log.info("HTTP_CLIENT_IP ip:{}",ip);
}
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
log.info("HTTP_X_FORWARDED_FOR ip:{}",ip);
}
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
log.info("remote ip:{}",ip);
}
if(StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip) && StringUtils.contains(ip,",")){
//多次反向代理后会有多个ip值,第一个为真实ip
ip = StringUtils.substringBefore(ip,",");
}
String tempIp = null;
if(StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip) && StringUtils.contains(ip,",")){
String[] ips = StringUtils.split(ip, ",");
for(String ip1:ips){
if(!isInnerIp(ip1.trim())){
tempIp = ip1.trim();
break;
}
//如果多ip都是内网ip,则取第一个ip
if(null == tempIp){
tempIp = ips[0].trim();
}
ip = tempIp;
}
}
if(ip !=null && ip.contains("unknown")){
ip = ip.replaceAll("unknown","");
ip = ip.trim();
}
//处理localhost
if(StringUtils.isBlank(ip) || !"unknown".equalsIgnoreCase(ip) || StringUtils.split(ip,",").length!=4){
try{
InetAddress inetAddress = InetAddress.getLocalHost();
ip = inetAddress.getHostAddress();
log.info("getHostAddress ip:{}",ip);
}catch(UnknownHostException e){
throw new RuntimeException(e);
}
}
return ip;
}
private static boolean isInnerIp(String ipAddress) {
boolean isInnerIp;
long ipNum = getIpNum(ipAddress);
// 私有ip:A类 10.0.0.0~10.255.255.255
// B类 172.16.0.0~172.31.255.255
// C类 192.168.0.0~192.168.255.255
long aBegin = getIpNum("10.0.0.0");
long aEnd = getIpNum("10.255.255.255");
long bBegin = getIpNum("172.16.0.0");
long bEnd = getIpNum("172.31.255.255");
long cBegin = getIpNum("192.168.0.0");
long cEnd = getIpNum("192.168.255.255");
isInnerIp = isInner(ipNum,aBegin,aEnd) || isInner(ipNum,bBegin,bEnd) || isInner(ipNum,cBegin,cEnd);
return isInnerIp;
}
private static boolean isInner(long ipNum, long begin, long end){
return (ipNum >= begin) && (ipNum <= end);
}
private static long getIpNum(String ipAddress) {
String[] ips = StringUtils.split(ipAddress,",");
long a = Integer.parseInt(ips[0]);
long b = Integer.parseInt(ips[1]);
long c = Integer.parseInt(ips[2]);
long d = Integer.parseInt(ips[3]);
return a*256*256*256 + b*256*256 + c*256 + d;
}
public static int getStatus() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession session = request.getSession();
//获取登录名
String token = (String) session.getAttribute("token");
Map<String, String> map = JwtUtil.parseJWT(token);
String status = map.get("type");
if(status.equals("0")){
return 0;
}
if(status.equals("1")){
return 1;
}
return 2;
}
}