java 校验IP地址。支持配置通配符

  /**
     * @Author robin.zhang
     * @Date 2019/7/29 13:52
     * @Param
     * @return
     * @Description validlIP 校验ip是否有效
     * validlIP 可能的格式,如:
     * 10.2.0.1
     * 10.2.0.*
     * 10.2.*.*
     * 10.*.*.*
     * *.*.*.*
     * ip 的格式一定为10.2.0.2
     **/
    public static Boolean validIp(String validlIP,String ip){
        if (!StringUtils.hasText(validlIP)) {
            return true;
        }
        validlIP = validlIP.trim();
        if (!StringUtils.hasText(ip)) {
            return false;
        }
        ip = ip.trim();
        String ipReg = "^((1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])|\\*)\\."
                + "((1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)|\\*)\\."
                + "((1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)|\\*)\\."
                + "((1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)|\\*)$";
        /**校验ip的格式*/
        Pattern pattern = Pattern.compile(ipReg);
        Matcher ipMatcher = pattern.matcher(ip);
        if (!ipMatcher.matches()) {
            return false;
        }
        /**validlIP 的格式*/
        Pattern oriPattern = Pattern.compile(ipReg);
        Matcher oriMatcher = oriPattern.matcher(validlIP);
        if (!oriMatcher.matches()) {
            return false;
        }
        /**validlIP 相同*/
        if(validlIP.equals(ip)){
            return true;
        }
        /**校验ip是否处在validlIP段内*/
        String[] oriIpArr = validlIP.split("\\.");
        String[] ipArr = ip.split("\\.");
        Boolean hasStar=false;
        String star = "*";
        for(int i=0;i<oriIpArr.length;i++){
            String oriIp = oriIpArr[i];
            boolean flag = oriIp.equals(star) ;
            if(flag){
                hasStar=flag;
            }
            if(hasStar && !flag){
                return false;
            }
            if(!ipArr[i].equals(oriIp) && !oriIp.equals(star)){
                return false;
            }
        }
        return true;
    }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在使用netty的HttpProxyHandler时,可以通过设置自定义的ProxyHandlerSelector来实现忽略地址的功能,具体步骤如下: 1. 创建一个自定义的ProxyHandlerSelector类,继承自DefaultProxyHandlerSelector类。 2. 在自定义的ProxyHandlerSelector类中重写select方法,在该方法中判断是否需要忽略当前请求的目标地址,如果需要忽略,则返回null,否则返回HttpProxyHandler。 3. 在创建Bootstrap时,通过调用handler方法设置自定义的ProxyHandlerSelector类。 下面是一个示例代码,演示如何使用通配符*来忽略地址: ```java import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpClientCodec; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.proxy.DefaultProxyHandlerSelector; import io.netty.handler.proxy.ProxyHandler; import io.netty.handler.proxy.ProxyHandlerSelector; import io.netty.handler.proxy.Socks5ProxyHandler; import io.netty.handler.proxy.Socks4ProxyHandler; import io.netty.handler.proxy.Socks4aProxyHandler; import io.netty.handler.proxy.Socks5AddressEncoder; import io.netty.handler.proxy.Socks5CommandRequest; import io.netty.handler.proxy.Socks5InitialRequest; import io.netty.handler.proxy.Socks5InitialResponse; import io.netty.handler.proxy.Socks5ProxyHandler; import io.netty.handler.proxy.Socks5ProxyServerDecoder; import io.netty.handler.proxy.Socks5ProxyServerEncoder; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.SslProvider; import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import java.net.InetSocketAddress; import java.net.URI; public class NettyHttpProxy { private static final String PROXY_HOST = "127.0.0.1"; private static final int PROXY_PORT = 1080; public static void main(String[] args) throws Exception { URI uri = new URI("https://www.google.com"); String host = uri.getHost(); int port = uri.getPort() == -1 ? 443 : uri.getPort(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(new NioEventLoopGroup()) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (uri.getScheme().equalsIgnoreCase("https")) { SslContext sslContext = SslContextBuilder.forClient().sslProvider(SslProvider.JDK) .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); pipeline.addFirst(sslContext.newHandler(ch.alloc(), host, port)); } ProxyHandlerSelector selector = new MyProxyHandlerSelector(); ProxyHandler proxyHandler = new HttpProxyHandler(new InetSocketAddress(PROXY_HOST, PROXY_PORT), selector); pipeline.addLast(proxyHandler); pipeline.addLast(new HttpClientCodec()); pipeline.addLast(new HttpObjectAggregator(1024 * 1024)); pipeline.addLast(new SimpleChannelInboundHandler<HttpResponse>() { @Override protected void channelRead0(ChannelHandlerContext ctx, HttpResponse msg) throws Exception { System.out.println(msg.toString()); } }); } }); Channel channel = bootstrap.connect(host, port).sync().channel(); HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath()); request.headers().set("Host", host); request.headers().set("Connection", "close"); request.headers().set("User-Agent", "netty-tcp-client"); channel.writeAndFlush(request).sync(); channel.closeFuture().sync(); } private static class MyProxyHandlerSelector extends DefaultProxyHandlerSelector { private static final String[] IGNORED_HOSTS = new String[]{"*.google.com"}; @Override public ProxyHandler select(URI uri) { String host = uri.getHost(); for (String ignoredHost : IGNORED_HOSTS) { if (host.matches(ignoredHost.replace("*", ".*"))) { return null; } } return super.select(uri); } } } ``` 在上面的示例代码中,我们定义了一个MyProxyHandlerSelector类来实现忽略地址的功能。在该类中,我们使用IGNORED_HOSTS数组来指定需要忽略的目标地址,每个地址可以使用通配符*来表示任意字符。然后在select方法中,我们遍历IGNORED_HOSTS数组,如果当前请求的目标地址匹配到了需要忽略的地址,则返回null,否则返回HttpProxyHandler。最后,在创建Bootstrap时,我们通过调用handler方法设置自定义的ProxyHandlerSelector类。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值