GithubHelp home page GithubHelp logo

ifeilong / feilong Goto Github PK

View Code? Open in Web Editor NEW
116.0 5.0 30.0 32.25 MB

让Java开发更简便的工具库

License: Apache License 2.0

Java 99.83% HTML 0.17%
java-library java8 httpclient xml-parser json-parser zip security md5 aes velocity

feilong's Introduction

feilong 让Java开发更简便的工具库

License JDK 1.8 Maven Central

Reduce development, Release ideas (灵感从重复简单的代码中释放出来)

  1. 让你从大量重复的底层代码中脱身,提高工作效率;
  2. 让你的代码更简炼易写易读易于维护;

feilong 优点:

对比1:

对比2:

  1. 有常用的工具类 (如 处理日期的 DateUtil,处理 集合 的 CollectionsUtil 等)
  2. 有常用的JAVA常量类 (如日期格式 DatePattern, 时间间隔 TimeInterval 等)
  3. 不必要的Exception 转成了RuntimeException,减少不必要的代码
  4. 国内中文注释最完善的API
  5. 有完善的单元测试

详细的帮助文档 http://feilong-core.mydoc.io/

📝 常用组件/功能

feilong 的历史

since 2008, 起初应对开发过程中不断重复的代码进行了封装,进而在公司内部推广

🐉 Maven使用配置

feilong 自从3.0.0开始,发布**仓库 https://search.maven.org/artifact/com.github.ifeilong/feilong

maven 配置

<dependency>
	<groupId>com.github.ifeilong</groupId>
	<artifactId>feilong</artifactId>
	<version>4.1.0</version>
</dependency>

Gradle 配置

com.github.ifeilong:feilong:4.1.0

非Maven项目

点击 https://repo1.maven.org/maven2/com/github/ifeilong/feilong/ 链接,下载 feilong.jar即可:

注意:

  • feilong 3 需要 JDK8+,对Android平台没有测试,不能保证所有工具类或工具方法可用。
  • 如果你的项目使用 JDK7,请使用 feilong-core 2.1.0 版本

How to install?

有些小伙伴想下载并 自行install 进行研究, 你需要执行以下 2 个步骤:

git clone https://github.com/ifeilong/feilong.git --depth 1
mvn install

子模块介绍

module 介绍
feilong-core 核心包 (推荐)
feilong-validator 常用的校验, 包含可配置式的手机号码, 邮编等等
feilong-json json format以及tobean toMap等常见操作
feilong-io 文件常见操作
feilong-servlet 基于http servlet 的封装,含常见request,response操作快捷封装 (推荐)
feilong-accessor 便捷式使用session ,cookie
feilong-formatter 将Map,bean,list format成友好形式
feilong-net-http http封装操作 (推荐)
feilong-net-jsoup jsoup操作
feilong-net-filetransfer ftp/sftp操作 (推荐)
feilong-template 模板操作,如velocity
feilong-net-mail 发送邮件,接收邮件操作
feilong-net-cxf cxf操作
feilong-xml xml format以及tobean toMap等常见操作
feilong-security 加密解密操作
feilong-context 上下文操作 (推荐)
feilong-namespace 可以spring xml 来配置的便捷操作
feilong-taglib jsp 自定义标签 (将会废弃)
feilong-tools 可用性操作
feilong-office-csv csv生成操作
feilong-office-excel excel操作,xml配置式来生成和读取excel文件
feilong-office-zip 压缩解压缩操作
feilong-component 组件式操作,含配置式即可获取数据-->转成excel-->打成zip压缩包-->发送邮件 (推荐)
feilong 一体化total jar包,包含上述所有功能
feilong-with-optional 一体化total jar包,包含上述所有功能,且包含所有optional jar依赖

Star History

Star History Chart

📝 说明

  1. 基于 Apache2 协议,您可以下载代码用于闭源项目,但每个修改的过的文件必须放置 版权说明 ;

📝 常见问题

🐼 提bug反馈或建议

提交问题反馈 Github issue

🌀 feilong 即时交流

QQ 群 243306798 微信公众号 feilong飞龙

feilong's People

Contributors

venusdrogon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

feilong's Issues

Maputil 添加 removeKeys(Map<K, V> map,Collection<K> keyList) 方法

Maputil 添加 removeKeys(Map<K, V> map,Collection keyList) 方法

现在需要 转成 数组

 //去除 有毛病的 sku ,其他的sku 继续添加
Map<Long, Integer> canAddSkuIdAndCountMap = 
MapUtil.removeKeys(skuIdAndCountMap,
 toArray(skuIdAndShoppingcartResultFailMap.keySet(), Long.class)
);

新建 MapUtil.toArrayValueMap(Map<K, V>, Class<V>)

MapUtil.toArrayValueMap(Map<K, String>) 将参数转成泛型

现在的代码是 Map<K, String[]> toArrayValueMap(Map<K, String> singleValueMap)

value 只能是 String, 不普用, 需要转成 泛型的形式

json-lib 换成 jackson

目前feilong-core 使用的 json-lib 作为 json处理工具 其主页: http://json-lib.sourceforge.net/

json-lib

但是 json-lib 自从 就不再维护了 (Last Published: 12/14/2010)

而且 json-lib 自身依赖的jar 版本都比较老

commons-lang 2.5
commons-beanutils 1.8.0
commons-collections 3.2.1
commons-logging 1.1.1
ezmorph 1.0.6

image

jackson

而jackson 目前社区很活跃 其主页是: https://github.com/FasterXML/jackson
相比json-lib框架,Jackson所依赖的jar包较少,简单易用并且性能也要相对高些。而且Jackson社区相对比较活跃,更新速度也比较快。

这里有性能对比 :

http://wangym.iteye.com/blog/738933
http://www.tuicool.com/articles/2a2eAb

这种测试报告均显示 jackson 性能更好

这项工作有点大, 计划会在 1.8系列加入 jackson

并在 1.9系列移除 json-lib

json首先需要有 format 和 toBean两大功能

其中format 要有

  1. pretty format
  2. include
  3. exclude

tobean要有

  1. toarray
  2. tomap
  3. tobean
  4. tolist

json-lib 支持 DynaBean, 计划可能会推迟

我们来看下述的例子


    @Test
    public void testBasicDynaClass(){
        Map<String, Class<?>> typeMap = new HashMap<>();
        typeMap.put("address", java.util.Map.class);
        typeMap.put("firstName", String.class);
        typeMap.put("lastName", String.class);

        Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("address", new HashMap());
        valueMap.put("firstName", "Fred");
        valueMap.put("lastName", "Flintstone");

        DynaBean dynaBean = BeanUtil.createDynaBean(typeMap, valueMap);

    }

上面的代码

使用 jackson format

      LOGGER.debug(JacksonUtil.format(dynaBean));

结果是

29:26 DEBUG (JacksonUtilTest.java:86) [testBasicDynaClass()] {
  "dynaClass" : {
    "dynaBeanClass" : "org.apache.commons.beanutils.BasicDynaBean",
    "name" : "org.apache.commons.beanutils.BasicDynaClass",
    "dynaProperties" : [ {
      "name" : "firstName",
      "type" : "java.lang.String",
      "indexed" : false,
      "mapped" : false
    }, {
      "name" : "lastName",
      "type" : "java.lang.String",
      "indexed" : false,
      "mapped" : false
    }, {
      "name" : "address",
      "type" : "java.util.Map",
      "indexed" : false,
      "mapped" : true
    } ]
  },
  "map" : {
    "address" : { },
    "firstName" : "Fred",
    "lastName" : "Flintstone"
  }
}

而使用json-lib

     LOGGER.debug(JsonUtil.format(dynaBean));

结果是

31:37 DEBUG (BeanUtilTest.java:402) [testBasicDynaClass()]     {
        "lastName": "Flintstone",
        "address": {},
        "firstName": "Fred"
    }

使用 alibaba fastjson

    LOGGER.debug(JSON.toJSONString(dynaBean, SerializerFeature.PrettyFormat));

结果是:

33:24 DEBUG (FastjsonUtilTest.java:68) [testBasicDynaClass()] {
    "dynaClass":{
        "dynaBeanClass":"org.apache.commons.beanutils.BasicDynaBean",
        "dynaProperties":[{
            "indexed":false,
            "mapped":false,
            "name":"firstName"
        },{
            "indexed":false,
            "mapped":false,
            "name":"lastName"
        },{
            "indexed":false,
            "mapped":true,
            "name":"address"
        }],
        "name":"org.apache.commons.beanutils.BasicDynaClass"
    },
    "map":{
        "firstName":"Fred",
        "lastName":"Flintstone",
        "address":{}
    }
}

image

image

研究下 XML External Entity Injection XXE

关于XML解析存在的安全问题指引

https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=23_5

微信支付商户,最近暴露的XML外部实体注入漏洞(XML External Entity Injection,简称 XXE),该安全问题是 由XML组件默认没有禁用外部实体引用导致 ,非微信支付系统存在漏洞。

如果你在使用支付业务回调通知中,存在以下场景有使用XML解析的情况,请务必检查是否对进行了防范。

场景1:支付成功通知;
场景2:退款成功通知;
场景3:委托代扣签约、解约、扣款通知;
场景4:车主解约通知;
场景5:扫码支付模式一回调;

注:APP支付的用户端SDK不受影响,但APP支付成功回调通知里面要检查。

微信支付会通过这几个系统号码通知商户进行安全周知和询问是否授权平台进行安全扫描。
(0755)36560292
(0755)61954612
(0755)1954613
(0755)61954614
(0755)61954615
(0755)61954616
授权检测支付系统操作,不会影响商户系统安全。

检查及修复建议

1.如果您的后台系统使用了官方SDK,请更新SDK到最新版本 SDK的链接:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
2.如果您是有系统提供商,请联系提供商进行核查和升级修复;
3.如果您是自研系统,请联系技术部门按以下指引核查和修复:

XXE漏洞需要你在代码中进行相应的设置,不同语言设置的内容不同,下面提供了几种主流开发语言的设置指引:

【JAVA】

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; // catching unsupported features
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
String FEATURE = null;
try {
	// This is the PRIMARY defense. If DTDs (doctypes) are disallowed, almost all XML entity attacks are prevented
	// Xerces 2 only - http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl
	FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
	dbf.setFeature(FEATURE, true);
	
	// If you can't completely disable DTDs, then at least do the following:
	// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities
	// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities
	// JDK7+ - http://xml.org/sax/features/external-general-entities 
	FEATURE = "http://xml.org/sax/features/external-general-entities";
	dbf.setFeature(FEATURE, false);
	
	// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-parameter-entities
	// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities
	// JDK7+ - http://xml.org/sax/features/external-parameter-entities 
	FEATURE = "http://xml.org/sax/features/external-parameter-entities";
	dbf.setFeature(FEATURE, false);
	
	// Disable external DTDs as well
	FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
	dbf.setFeature(FEATURE, false);
	
	// and these as well, per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks"
	dbf.setXIncludeAware(false);
	dbf.setExpandEntityReferences(false);
	
	// And, per Timothy Morgan: "If for some reason support for inline DOCTYPEs are a requirement, then 
	// ensure the entity settings are disabled (as shown above) and beware that SSRF attacks
	// (http://cwe.mitre.org/data/definitions/918.html) and denial 
	// of service attacks (such as billion laughs or decompression bombs via "jar:") are a risk."
	
	// remaining parser logic
} catch (ParserConfigurationException e) {
	// This should catch a failed setFeature feature
	logger.info("ParserConfigurationException was thrown. The feature '" +
	FEATURE + "' is probably not supported by your XML processor.");
}
catch (SAXException e) {
	// On Apache, this should be thrown when disallowing DOCTYPE
	logger.warning("A DOCTYPE was passed into the XML document");
}
catch (IOException e) {
	// XXE that points to a file that doesn't exist
	logger.error("IOException occurred, XXE may still possible: " + e.getMessage());
}
DocumentBuilder safebuilder = dbf.newDocumentBuilder();

附录

更多开源库/语言版本的修复建议可参考:
https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#C.2FC.2B.2B

feilong-bind support list parse

feilong-bind support list parse

比如如下数据:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MROfListOfTLotteryItem xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <data>
        <recordcount>1</recordcount>
        <pageindex>0</pageindex>

        <TLotteryItem>
            <id>1536009</id>
            <lcode>BP458402870</lcode>
            <pcode>20160818001</pcode>
            <usedmodeno>KX</usedmodeno>
            <ptype>2</ptype>
            <pdiscount>200.0000</pdiscount>
            <startdate>2016-08-18T00:00:00</startdate>
            <enddate>2016-08-19T00:00:00</enddate>
            <isused>true</isused>
            <serviceollyow>-79228162514264337593543950335</serviceollyow>
            <createman>admin</createman>
            <opearman>zhufeng</opearman>
            <usedman>朱峰</usedman>
            <usedtime>2016-08-18T15:32:31.597</usedtime>
            <remark>会员卡快修消费95折(有促销享受折上折)付款成功,模块[2]金额[332]</remark>
            <ServiceList/>
            <UsedOid>365</UsedOid>
            <saleprice>200.0000</saleprice>
            <CreateDate>2016-08-18T15:28:29.01</CreateDate>
            <SaleOid>100</SaleOid>
            <SaleDate>2016-08-18T15:28:28.903</SaleDate>
            <MerchantsCardNo>100186</MerchantsCardNo>
            <Iinstructions>此券最终解释权归本公司所有</Iinstructions>
            <AddToWeixinCardTime>0001-01-01T00:00:00</AddToWeixinCardTime>
        </TLotteryItem>

        <TLotteryItem>
            <id>1536009</id>
            <lcode>CP458402870</lcode>
            <pcode>20160818001</pcode>
            <usedmodeno>KX</usedmodeno>
            <ptype>2</ptype>
            <pdiscount>200.0000</pdiscount>
            <startdate>2016-08-18T00:00:00</startdate>
            <enddate>2016-08-19T00:00:00</enddate>
            <isused>false</isused>
            <serviceollyow>-79228162514264337593543950335</serviceollyow>
            <createman>admin</createman>
            <opearman>zhufeng</opearman>
            <usedman>朱峰</usedman>
            <usedtime>2016-08-18T15:32:31.597</usedtime>
            <remark>会员卡快修消费95折(有促销享受折上折)付款成功,模块[2]金额[332]</remark>
            <ServiceList/>
            <UsedOid>365</UsedOid>
            <saleprice>200.0000</saleprice>
            <CreateDate>2016-08-18T15:28:29.01</CreateDate>
            <SaleOid>100</SaleOid>
            <SaleDate>2016-08-18T15:28:28.903</SaleDate>
            <MerchantsCardNo>100186</MerchantsCardNo>
            <Iinstructions>此券最终解释权归本公司所有</Iinstructions>
            <AddToWeixinCardTime>0001-01-01T00:00:00</AddToWeixinCardTime>
        </TLotteryItem>
    </data>
</MROfListOfTLotteryItem>

目前支持 直接转成bean 的方法

比如 com.feilong.bind.xml.NodeNameAndStringValueVarCommandXmlParse

移除 StringUtil.tokenizeToStringArray(String, String)

移除 StringUtil.tokenizeToStringArray(String, String)

这个方法是借鉴了 spring 的 org.springframework.util.StringUtils#tokenizeToStringArray

其实 apache commons lang3 也有相似的功能

Apache Spring
split tokenizeToStringArray
splitByWholeSeparator delimitedListToStringArray

建议在CollectionsUtil类里添加一个与addAllIgnoreNull方法类似的方法

建议在CollectionsUtil类里添加一个与addAllIgnoreNull方法类似的方法,暂称之为addAllIgnoreNull2。期望该方法的作用是将一个非空list2的元素全部添加到list1。如果list1为null,则直接令list1=list2。

例如:

List<String> list1 = null;
List<String> list2 = new ArrayList<>();
list2.add("张三");
list2.add("李四");

addAllIgnoreNull2(list1, list2);
System.out.println(list1.toString());

输出结果为:
[张三,李四]

在原来的addAllIgnoreNull方法中,如果list1为null,会抛出异常,而不执行任何操作。

该方法有如下应用场景,比如,某个订单的extraData(Map类型数据)有一个key为“packageInfoList”,这个key对应的value为null。现在我通过addAllIgnoreNull方法对“packageInfoList”进行赋值,把一个不为空的list赋给它。原来的addAllIgnoreNull方法会抛异常,现在我希望不抛异常,而是把这个list的值赋给它。

针对这个需求,增加如下方法(伪代码):

public static <O> void addAllIgnoreNull2(final Collection<O> objectCollection, 
final Iterable<? extends O> iterable) {
        if(Validate.isNullOrEmpty(objectCollection)){
        	objectCollection = iterable;
        	return;
        }
        if(Validate.isNullOrEmpty(iterable)){
        	return;
        }        

        CollectionUtils.addAll(objectCollection, iterable);
    }

SessionAccessor基于接口

建议SessionAccessor实现Accessible接口(或者类似功能的Accessor接口),如果寄存实现想由Session改为Redis,直接替换Accessible接口的实现类即可.

stfp com.jcraft.jsch.JSchException: connection is closed by foreign host

stfp com.jcraft.jsch.JSchException: connection is closed by foreign host

今天某商城 使用出现了以下的异常

1013 04:00:00 ERROR (SFTPFileTransfer.java:122) connect() - sftpFileTransferConfig:    {
        "port": 22,
        "sshConfig": {"StrictHostKeyChecking": "no"},
        "userName": "columbia",
        "hostName": "********",
        "password": "******",
        "sessionTimeout": 120000
    },by:connection is closed by foreign host
com.jcraft.jsch.JSchException: connection is closed by foreign host
    at com.jcraft.jsch.Session.connect(Session.java:269)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at com.feilong.tools.net.filetransfer.sftp.SFTPUtil.connectSession(SFTPUtil.java:76)
    at com.feilong.tools.net.filetransfer.sftp.SFTPFileTransfer.connect(SFTPFileTransfer.java:108)
    at com.feilong.tools.net.filetransfer.AbstractFileTransfer.download(AbstractFileTransfer.java:83)
    at com.store.manager.member.impl.MemberImportManagerImpl.readViplist(MemberImportManagerImpl.java:156)
    at com.store.manager.member.impl.MemberImportManagerImpl.importVIPstaff(MemberImportManagerImpl.java:123)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy533.importVIPstaff(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.