java验证是否包含特殊字符-java工具类

文章目录

        • 简单效验
            • 1.javax.validation包下 的
            • 2.apache工具包common-lang中的工具类,其中之一就是StringEscapeUtil
        • 复杂效验MatchTest工具类

简单效验

1.javax.validation包下 的

转载自

2.apache工具包common-lang中的工具类,其中之一就是StringEscapeUtil

java验证是否包含特殊字符-java工具类_第1张图片

复杂效验MatchTest工具类

package com.hwqh.huawenstockuser.utils.string;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.regex.Pattern;

/**
 * @program: hxzq-cloud-parent
 * @ClassName: MatchTest
 * @version: 1.0
 * @description:
 * @author: zhaonian
 * @create: 2020-06-09 14:59
 **/
public class MatchTest {


    public static void main(String[] args) {
        String s = "1234567890-=!@#$%^^&*()_~!@#$%^^&*()_QWERTYUIOP{}{|":LKGFSAZXCVBNM<>?"}"?>?>?>?>?>?>?<><<><><>?>?>?>>>>>>>>>>>>>>>>>>>>>L:::::::::::::::::::::::::::::::::::::::::::::::::::::::::""::';\n" +
                "\n" +
                "';';[qw87287636425326473247256~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

        System.out.println(containsAll(s));

    }


    public static boolean containsAll(String str) {
        if (str.contains("*")) {
            return true;
        }
        if (str.contains("'")) {
            return true;
        }
        if (str.contains("~")) {
            return true;
        }
        if (str.contains(">")) {
            return true;
        }
        if (str.contains("*")) {
            return true;
        }
        if (str.contains("<")) {
            return true;
        }
        if (str.contains("&")) {
            return true;
        }
        if (str.contains("%")) {
            return true;
        }
        if (str.contains("$")) {
            return true;
        }
        if (str.contains("@")) {
            return true;
        }
        if (str.contains("=")) {
            return true;
        }
        if (str.contains("or")) {
            return true;
        }
        return false;
    }

    public static String replaceXSS(String value) {
        if (value != null) {
            try {
                value = value.replace("+", "%2B");   //'+' replace to '%2B'
                value = URLDecoder.decode(value, "utf-8");
            } catch (UnsupportedEncodingException e) {
            } catch (IllegalArgumentException e) {
            }

            // Avoid null characters
            value = value.replaceAll("\0", "");

            // Avoid anything between script tags
            Pattern scriptPattern = Pattern.compile("", Pattern.CASE_INSENSITIVE);
            value = scriptPattern.matcher(value).replaceAll("");

            // Avoid anything in a src='...' type of e­xpression
            scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            value = scriptPattern.matcher(value).replaceAll("");

            scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            value = scriptPattern.matcher(value).replaceAll("");

            // Remove any lonesome  tag
            scriptPattern = Pattern.compile("", Pattern.CASE_INSENSITIVE);
            value = scriptPattern.matcher(value).replaceAll("");

            // Remove any lonesome 
                    
                    

你可能感兴趣的:(java,java工具类)