All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.menglim.sutils.annotations.xssprotection.XSSProtectionValidator Maven / Gradle / Ivy

There is a newer version: 1.2.16
Show newest version
package com.github.menglim.sutils.annotations.xssprotection;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.util.HtmlUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class XSSProtectionValidator implements ConstraintValidator {

    @Override
    public void initialize(XSSProtection constraintAnnotation) {
        
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isNotEmpty(value)) {
            return !isHtml(value);
        }
        return true;
    }

    public static boolean isHtml(String input) {
        boolean isHtml = false;
        if (input != null) {
            String htmlEscapedHtml = HtmlUtils.htmlEscape(input);
            if (!input.equals(htmlEscapedHtml)) {
                isHtml = true;
            }
        }
        return isHtml;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy