文章目录

  • 前言
  • 一、pom依赖
  • 二、ESAPI配置文件
    • 1.ESAPI.properties
    • 2.validation.properties
    • 3.esapi-java-logging.properties
    • 4.antisamy-esapi.xml
    • 5.配置文件放置位置
    • 6.自定义配置文件路径,封装ESAPI方法

前言

网上有很多对ESAPI的上手讲解,但很少有讲的其原理通透的。我在这里梳理梳理。


一、pom依赖

        <dependency><groupId>org.owasp.esapi</groupId><artifactId>esapi</artifactId><version>2.2.3.0</version><!-- 如果项目中没有引入其他日志框架,可以不排除 --><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId></exclusion></exclusions></dependency>

为什么需要排除slf4j,因为我的项目中用了logback日志框架,如果不排除slf4j则项目中有两个日志实现框架,这样项目将启动失败。

二、ESAPI配置文件

官方配置文件都在github官网上。
ESAPI的github地址

1.ESAPI.properties

ESAPI.printProperties=true# 这个地方是配置ESAPI的实现类,项目中用到那个就选择性配置即可
ESAPI.Encoder=org.owasp.esapi.reference.DefaultEncoder
ESAPI.Validator=org.owasp.esapi.reference.DefaultValidator# 以下是选择性配置,可以在官网找到对应的介绍
ESAPI.Logger=org.owasp.esapi.logging.java.JavaLogFactory
Encoder.AllowMultipleEncoding=false
Encoder.AllowMixedEncoding=false
Encoder.DefaultCodecList=HTMLEntityCodec,PercentCodec,JavaScriptCodec
Logger.ApplicationName=ExampleApplication
Logger.LogEncodingRequired=false
Logger.LogApplicationName=true
Logger.LogServerIP=true
Logger.LogFileName=ESAPI_logging_file
Logger.MaxLogFileSize=10000000
Logger.UserInfo=true
Logger.ClientInfo=true

2.validation.properties

Validator.SafeString=^[.\\p{Alnum}\\p{Space}]{0,1024}$
Validator.Email=^[A-Za-z0-9._%'-]+@[A-Za-z0-9.-]+\\.[a-zA-Z]{2,4}$
Validator.IPAddress=^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Validator.URL=^(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\,\\:\\'\\/\\\\\\+=&;%\\$#_]*)?$
Validator.CreditCard=^(\\d{4}[- ]?){3}\\d{4}$
Validator.SSN=^(?!000)([0-6]\\d{2}|7([0-6]\\d|7[012]))([ -]?)(?!00)\\d\\d\\3(?!0000)\\d{4}$

3.esapi-java-logging.properties

handlers= java.util.logging.ConsoleHandler.level= INFO
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%3$-7s] %5$s %n

4.antisamy-esapi.xml

<?xml version="1.0" encoding="ISO-8859-1"?><!--
W3C rules retrieved from:
http://www.w3.org/TR/html401/struct/global.html
--><!--
Slashdot allowed tags taken from "Reply" page:
<b> <i> <p> <br> <a> <ol> <ul> <li> <dl> <dt> <dd> <em> <strong> <tt> <blockquote> <div> <ecode> <quote>
--><anti-samy-rules xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="antisamy.xsd"><directives><directive name="omitXmlDeclaration" value="true"/><directive name="omitDoctypeDeclaration" value="true"/><directive name="maxInputSize" value="500000"/><directive name="embedStyleSheets" value="false"/></directives><common-regexps><!--From W3C:This attribute assigns a class name or set of class names to anelement. Any number of elements may be assigned the same classname or names. Multiple class names must be separated by whitespace characters.--><regexp name="htmlTitle" value="[a-zA-Z0-9\s-_',:\[\]!\./\\\(\)]*"/> <!-- force non-empty with a '+' at the end instead of '*' --><regexp name="onsiteURL" value="([\w\\/\.\?=&amp;;\#-~]+|\#(\w)+)"/><regexp name="offsiteURL" value="(\s)*((ht|f)tp(s?)://|mailto:)[A-Za-z0-9]+[~a-zA-Z0-9-_\.@#$%&amp;;:,\?=/\+!]*(\s)*"/></common-regexps><!--Tag.name = a, b, div, body, etc.Tag.action = filter: remove tags, but keep content, validate: keep content as long as it passes rules, remove: remove tag and contentsAttribute.name = id, class, href, align, width, etc.Attribute.onInvalid = what to do when the attribute is invalid, e.g., remove the tag (removeTag), remove the attribute (removeAttribute), filter the tag (filterTag)Attribute.description = What rules in English you want to tell the users they can have for this attribute. Include helpful things so they'll be able to tune their HTML--><!--Some attributes are common to all (or most) HTML tags. There aren't many that qualify for this. You have to make sure there's nocollisions between any of these attribute names with attribute names of other tags that are for different purposes.--><common-attributes><attribute name="lang" description="The 'lang' attribute tells the browser what language the element's attribute values and content are written in"><regexp-list><regexp value="[a-zA-Z]{2,20}"/></regexp-list></attribute><attribute name="title" description="The 'title' attribute provides text that shows up in a 'tooltip' when a user hovers their mouse over the element"><regexp-list><regexp name="htmlTitle"/></regexp-list></attribute><attribute name="href" onInvalid="filterTag"><regexp-list><regexp name="onsiteURL"/><regexp name="offsiteURL"/></regexp-list></attribute><attribute name="align" description="The 'align' attribute of an HTML element is a direction word, like 'left', 'right' or 'center'"><literal-list><literal value="center"/><literal value="left"/><literal value="right"/><literal value="justify"/><literal value="char"/></literal-list></attribute></common-attributes><!--This requires normal updates as browsers continue to diverge from the W3C and each other. As long as the browser wars continuethis is going to continue. I'm not sure war is the right word for what's going on. Doesn't somebody have to win a war aftera while?--><global-tag-attributes><attribute name="title"/><attribute name="lang"/></global-tag-attributes><tag-rules><!-- Tags related to JavaScript --><tag name="script" action="remove"/><tag name="noscript" action="remove"/><!-- Frame & related tags --><tag name="iframe" action="remove"/><tag name="frameset" action="remove"/><tag name="frame" action="remove"/><tag name="noframes" action="remove"/><!-- All reasonable formatting tags --><tag name="p" action="validate"><attribute name="align"/></tag><tag name="div" action="validate"/><tag name="i" action="validate"/><tag name="b" action="validate"/><tag name="em" action="validate"/><tag name="blockquote" action="validate"/><tag name="tt" action="validate"/><tag name="br" action="truncate"/><!-- Custom Slashdot tags, though we're trimming the idea of having a possible mismatching end tag with the endtag="" attribute --><tag name="quote" action="validate"/><tag name="ecode" action="validate"/><!-- Anchor and anchor related tags --><tag name="a" action="validate"><attribute name="href" onInvalid="filterTag"/><attribute name="nohref"><literal-list><literal value="nohref"/><literal value=""/></literal-list></attribute><attribute name="rel"><literal-list><literal value="nofollow"/></literal-list></attribute></tag><!-- List tags --><tag name="ul" action="validate"/><tag name="ol" action="validate"/><tag name="li" action="validate"/></tag-rules><!--  No CSS on Slashdot posts --><css-rules></css-rules>
</anti-samy-rules>

5.配置文件放置位置

如果我们对配置文件路径没有自定义配置,那么ESAPI会从默认路径中读取配置文件。
默认路径如下:

  • esapi.jar路径:Not found in ‘org.owasp.esapi.resources’ directory or file not readable
  • 系统资源路径: Not found in SystemResource Directory/resourceDirectory: .esapi\validation.properties
  • 系统user.name目录: Not found in ‘user.home’ (C:\Users\myhome) directory: C:\Users\myhome\esapi\validation.properties

我们如果把配置文件放在以上默认读取路径上,ESAPI读取到配置文件后也是可以正常执行的。但关键是这并不能满足我们的要求,我们希望项目在打包时把配置文件打进jar包、war包。
ESAPI的设计者也一定想到了这一点,所以给了我们设置自定义配置文件路径的方式。

默认读取的配置方法:

如果不自定义配置,那么ESAPI将走这个默认配置。而默认实现是 DefaultSecurityConfiguration。这个配置文件的读取配置文件的方式是:

// 此时我们如果把配置文件放在/src/main/resource目录下
this.getResourceFile(filename);
// 读取不到我们的配置文件,因为他的类加载方式与springBoot不一样。具体没有深究,研究完类加载机制的双亲委派模型之后再来补充。

思路: 所以我们只要重写这个方法,并将我们重写的配置类在ESAPI加载即可大功告成

  1. 第一步我们先重写读取配置的方法
package com.github.common.config;import org.owasp.esapi.reference.DefaultSecurityConfiguration;import java.io.IOException;
import java.io.InputStream;/*** 重写ESAPI读取配置文件方法** @author utrix*/
public class ESAPIConfiguration extends DefaultSecurityConfiguration {@Overridepublic InputStream getResourceStream(String filename) throws IOException {// 通过类加载器读取resource目录下的配置文件,这个filename就是ESAPI.propertiesreturn ClassLoader.getSystemResourceAsStream(filename);}}
  1. 第二步使我们的重写方法生效

此时就要看在ESAPI的配置实现是在哪里赋值的了。

重写配置的静态方法:

至此我们理清了思路,只要通过ESAPI的override()方法注入我们自定义的ESAPIConfiguration 类即可。

6.自定义配置文件路径,封装ESAPI方法

package com.github.util;import cn.hutool.core.util.StrUtil;
import com.github.pig.admin.common.config.ESAPIConfiguration;
import lombok.SneakyThrows;
import org.owasp.esapi.ESAPI;
import org.owasp.esapi.Validator;/*** ESAPI工具类** @author utrix*/
public class EsapiUtil {private static final Validator VALIDATOR;/*** 请求体最大报文长度10万字符*/private static final int MAX_MESSAGE_LENGTH = 10 * 10000;static {// 通过静态方法注入我们重写读取配置文件的类ESAPI.override(new ESAPIConfiguration());VALIDATOR = ESAPI.validator();}/*** 对输入的文本,如果存在xss攻击,则转义** @param str 输入的文本* @return java.lang.String 如果输入的为空,则返回*/@SneakyThrowspublic static String xssEncode(String str) {return StrUtil.isEmpty(str) ? str : VALIDATOR.getValidSafeHTML(EsapiUtil.class.getSimpleName(), str, MAX_MESSAGE_LENGTH, true);}}

至此:我们就可以把ESAPI的配置文件放到项目/src/main/resource目录下,并打入jar、war包了


(本文完!)如需引用文章内容,必须付原文链接!

ESAPI配置文件自定义路径相关推荐

  1. java 配置文件的路径_详解java配置文件的路径问题

    详解java配置文件的路径问题 详解java配置文件的路径问题 各种语言都有自己所支持的配置文件,配置文件中有很多变量是经常改变的.不将程序中的各种变量写死,这样能更方便地脱离程序本身去修改相关变量设 ...

  2. spring cloud中通过配置文件自定义Ribbon负载均衡策略

    2019独角兽企业重金招聘Python工程师标准>>> spring cloud中通过配置文件自定义Ribbon负载均衡策略 博客分类: 微服务 一.Ribbon中的负载均衡策略 1 ...

  3. 【Android 插件化】基于插件化的恶意软件的加载策略分析 ( 自定义路径加载插件 | 系统路径加载插件 | 用户同意后加载插件 | 隐藏恶意插件 )

    文章目录 一.自定义路径加载插件 二.系统路径加载插件 三.用户同意后加载插件 四.隐藏恶意插件 一.自定义路径加载插件 插件化应用中 , 宿主应用 加载 插件 APK , 需要获取该插件 APK 文 ...

  4. java webproject中logback换配置文件的路径

    java webproject中logback换配置文件的路径 本人小站点:   http://51kxd.com/  欢迎大家不开心的时候訪问訪问,调节一下心情 web.xml中配置: <!- ...

  5. Visual Studio 添加 自定义 路径宏

    在编辑VS工程包含路径和库路径时,有时需要添加第三方包的路径,比如c++ boost库, 为了协作的方便,不合适直接把本地绝对路径添加入工程设置,此时可以添加自定义路径宏, 然后参与协作的每个开发人员 ...

  6. linux的vim怎么配置文件路径,Linux_Linux系统配置VI或VIM的技巧,1、VI或VIM的配置文件的路径 - phpStudy...

    Linux系统配置VI或VIM的技巧 1.VI或VIM的配置文件的路径 发现/usr/share/vim/vimrc和/etc/vim/vimrc指向是同一个文件,即vimrc,为vi和vim的配置文 ...

  7. 自定义路径创建Cocos2d-x项目

    自定义路径创建Cocos2d-x项目 本文介绍windows下面如何优雅的创建Cocos2d-x项目.为何称之为优雅,是因为现在网上流传的一些创建方法有一些问题.大致内容如下: l 使用VS向导创建C ...

  8. log4j2的介绍与使用(配置文件,路径等)

    log4j2的介绍与使用(配置文件,路径等) 1. Log4j2的导入 首先到http://logging.apache.org/log4j/2.x/download.html 上下载最新的log4j ...

  9. Silverlight 2.5D RPG游戏技巧与特效处理:(二十一)自定义路径动画

    一直在想应该用什么作为<Silverlight 2.5D RPG游戏技巧与特效处理系列教程>的终结,既要实用而不拖泥带水:又要通用而不哗众取宠.于是一不小心便成就了我一个未了心愿:一切基于 ...

  10. Silverlight 2.5D RPG游戏技巧与特效处理:自定义路径动画

    一直在想应该用什么作为<Silverlight 2.5D RPG游戏技巧与特效处理系列教程>的终结,既要实用而不拖泥带水:又要通用而不哗众取宠.于是一不小心便成就了我一个未了心愿:一切基于 ...

最新文章

  1. 自然语言处理基础技术之词性标注
  2. 函数计算工具链新成员 —— Fun Local 发布啦
  3. 解决Mac安装tesserocr报错问题 Failed building wheel for
  4. Android菜鸟的成长笔记(11)——Android中的事件处理
  5. 第1章 统计学习方法概论
  6. 程序—java记事本
  7. Exchange 2010 (四) CAS Array部署
  8. 数据分布_数据蒋堂 | 数据分布背后的逻辑
  9. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
  10. 怎么下载思维导图模板呢?
  11. 2013年放假安排时间表 法定节假日安排通知 ( IS2120@BG57IV3)
  12. 俄罗斯方块Tetris Beat for Mac(休闲益智游戏)
  13. 概要设计 重要性_艺术留学——服装设计
  14. openCV图片倾斜矫正(java版)
  15. 其实你的AI技能并没有那么值钱\n
  16. python开发指法练习软件_猿编程电脑端
  17. 浏览器英文快捷翻译工具~~有道词典
  18. 知乎上的48条神回复
  19. STL 的 std::set 创建自定义结构体的对象,定义严格弱序的比较函数
  20. cpu超线程优缺点_什么是超线程,它有什么优点?

热门文章

  1. R语言建立VAR模型分析联合内生变量的动态关系
  2. c语言自学教程——数组
  3. 华为网络篇 传输文件-08
  4. 开源扫描仪软件_适用于Windows的优秀开源免费扫描仪软件?
  5. matlab freqz用法ba,【matlab】freqz函数的使用(一)
  6. 机器人工程毕业设计☞开题报告
  7. 【校招VIP】产品项目分析之竞品分析
  8. C#编程之SqlHelper
  9. 数据分析的软件最全场景盘点
  10. C#使用ITextSharp操作pdf