Idea 中使用Groovy 脚本自动生成Entitys类

原生成脚本来自园长:from:http://javaweb.org

import com.intellij.database.model.DasTable
import com.intellij.database.model.ObjectKind
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil

/*
 * 默认可用的 context 绑定:
 *   SELECTION   Iterable<DasObject>
 *   PROJECT     project
 *   FILES       files helper
 */

packageName = ""
typeMapping = [
        (~/(?i)tinyint|smallint|mediumint/)      : "Integer",
        (~/(?i)int/)                             : "Long",
        (~/(?i)bool|bit/)                        : "Boolean",
        (~/(?i)float|double|decimal|real/)       : "Double",
        (~/(?i)datetime|timestamp|date|time/)    : "Date",
        (~/(?i)blob|binary|bfile|clob|raw|image/): "InputStream",
        (~/(?i)/)                                : "String"
]

FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
    SELECTION.filter { it instanceof DasTable && it.getKind() == ObjectKind.TABLE }.each { generate(it, dir) }
}

def generate(table, dir) {
    def className = javaName(table.getName(), true)
    def fields = calcFields(table)
    packageName = getPackageName(dir)
    new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields) }
}

/**
 * 获取包名称
 * @param dir 实体类所在目录
 * @return
 */
def getPackageName(dir) {
    return dir.toString().replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
}

/**
 * 实体类代码生成
 * @param out
 * @param className
 * @param fields
 * @return
 */
def generate(out, className, fields) {
    out.println "package $packageName"
    out.println ""

    Set<String> types = new HashSet<String>()

    fields.each() {
        types.add(it.type)
    }


    out.println "import com.alibaba.fastjson.annotation.JSONField;"
    out.println "import javax.persistence.Column;"
    if (types.contains("Date")) {
        out.println "import java.util.Date;"
    }

    if (types.contains("InputStream")) {
        out.println "import java.io.InputStream;"
    }
    out.println "import java.io.Serializable;"

    out.println ""
    out.println "public class $className implements Serializable {"
    fields.each() {
        out.println ""
        if (it.annos != "") out.println "  ${it.annos}"

        // 输出注释
        if (isNotEmpty(it.commoent)) {
            out.println "\t/**"
            out.println "\t * ${it.commoent}"
            out.println "\t */"
        }

        // 输出成员变量
        out.println "\tprivate ${it.type} ${it.name};"
    }

    // 输出get/set方法
    fields.each() {
        out.println ""

        if (isNotEmpty(it.comment)) {
            out.println "\t/**"
            out.println "\t * 获取${it.comment}"
            out.println "\t * @return"
            out.println "\t */"
        }

        def actionName = "Boolean".equals(it.type.toString()) ? "is" : "get"

        out.println "\tpublic ${it.type} ${actionName}${it.name.capitalize()}() {"
        out.println "\t\treturn this.${it.name};"
        out.println "\t}"
        out.println ""

        if (isNotEmpty(it.comment)) {
            out.println "\t/**"
            out.println "\t * 设置${it.comment}"
            out.println "\t * @param ${it.name}"
            out.println "\t */"
        }
        out.println "\tpublic void set${it.name.capitalize()}(${it.type} ${it.name}) {"
        out.println "\t\tthis.${it.name} = ${it.name};"
        out.println "\t}"
    }
    out.println ""
    out.println "}"
}

def calcFields(table) {
    DasUtil.getColumns(table).reduce([]) { fields, col ->
        def spec = Case.LOWER.apply(col.getDataType().getSpecification())
        def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
        fields += [[
                           colName : col.getName(),
                           name    : javaName(col.getName(), false),
                           type    : typeStr,
                           comment: col.getComment(),
                           annos   : ""]]
    }
}

def javaName(str, capitalize) {
    def s = str.split(/[^\p{Alnum}]/).collect { def s = Case.LOWER.apply(it).capitalize() }.join("")
    capitalize ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}

def isNotEmpty(content) {
    return content != null && content.toString().trim().length() > 0
}

增强版本

增加Serializable ID
增加@JSONField以及@Column注解生成方式
增加添加@Entity注解以及添加@Table 名

import com.intellij.database.model.DasTable
import com.intellij.database.model.ObjectKind
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil

/*
 * 默认可用的 context 绑定:
 *   SELECTION   Iterable<DasObject>
 *   PROJECT     project
 *   FILES       files helper
 */

packageName = ""
tableName = ""
typeMapping = [
        (~/(?i)tinyint|smallint|mediumint/)      : "Integer",
        (~/(?i)int/)                             : "Long",
        (~/(?i)bool|bit/)                        : "Boolean",
        (~/(?i)float|double|decimal|real/)       : "Double",
        (~/(?i)datetime|timestamp|date|time/)    : "Date",
        (~/(?i)blob|binary|bfile|clob|raw|image/): "InputStream",
        (~/(?i)/)                                : "String"
]

FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
    SELECTION.filter { it instanceof DasTable && it.getKind() == ObjectKind.TABLE }.each { generate(it, dir) }
}

def generate(table, dir) {
    def className = javaName(table.getName(), true)
    def fields = calcFields(table)
    packageName = getPackageName(dir)
    tableName = table.getName()
    new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields) }
}

/**
 * 获取包名称
 * @param dir 实体类所在目录
 * @return
 */
def getPackageName(dir) {
    return dir.toString().replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
}

/**
 * 实体类代码生成
 * @param out
 * @param className
 * @param fields
 * @return
 */
def generate(out, className, fields) {
    out.println "package $packageName"
    out.println ""

    Set<String> types = new HashSet<String>()

    fields.each() {
        types.add(it.type)
    }


    out.println "import com.alibaba.fastjson.annotation.JSONField;"
    out.println "import javax.persistence.Column;"
    out.println "import javax.persistence.Entity;"
    out.println "import javax.persistence.Table;"
    if (types.contains("Date")) {
        out.println "import java.util.Date;"
    }

    if (types.contains("InputStream")) {
        out.println "import java.io.InputStream;"
    }
    out.println "import java.io.Serializable;"

    out.println ""
    out.println "@Entity"
    out.println "@Table(name = \"$tableName\")"
    out.println "public class $className implements Serializable {"
    fields.each() {
        out.println ""
        if (it.annos != "") out.println "  ${it.annos}"

        // 输出注释
        if (isNotEmpty(it.commont)) {
            out.println "\t/**"
            out.println "\t * ${it.commont}"
            out.println "\t */"
        }

        def matcher = ("${it.colName}" =~ /[0-9]/);

        if (matcher.getCount() > 0) {
            //输出Column
            out.println "\t@Column(name = \"${it.colName}\")"
        }

        //输出JSONField
        out.println "\t@JSONField(name = \"${it.colName}\")"
        // 输出成员变量
        out.println "\tprivate ${it.type} ${it.name};"
    }

    // 输出get/set方法
    fields.each() {
        out.println ""

        if (isNotEmpty(it.comment)) {
            out.println "\t/**"
            out.println "\t * 获取${it.comment}"
            out.println "\t * @return"
            out.println "\t */"
        }

        def actionName = "Boolean".equals(it.type.toString()) ? "is" : "get"

        out.println "\tpublic ${it.type} ${actionName}${it.name.capitalize()}() {"
        out.println "\t\treturn this.${it.name};"
        out.println "\t}"
        out.println ""

        if (isNotEmpty(it.comment)) {
            out.println "\t/**"
            out.println "\t * 设置${it.comment}"
            out.println "\t * @param ${it.name}"
            out.println "\t */"
        }
        out.println "\tpublic void set${it.name.capitalize()}(${it.type} ${it.name}) {"
        out.println "\t\tthis.${it.name} = ${it.name};"
        out.println "\t}"
    }
    out.println ""
    out.println "}"
}

def calcFields(table) {
    DasUtil.getColumns(table).reduce([]) { fields, col ->
        def spec = Case.LOWER.apply(col.getDataType().getSpecification())
        def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
        fields += [[
                           colName: col.getName(),
                           name   : javaName(col.getName(), false),
                           type   : typeStr,
                           comment: col.getComment(),
                           annos  : ""]]
    }
}

def javaName(str, capitalize) {
    def s = str.split(/[^\p{Alnum}]/).collect { def s = Case.LOWER.apply(it).capitalize() }.join("")
    capitalize ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}

def isNotEmpty(content) {
    return content != null && content.toString().trim().length() > 0
}

Spring MVC MultipartResolver特性-QP编码

今天看Spring的Multipart处理发现一段比较奇怪的代码:

20180803094501_282.png
奇怪的是Spring为什么会对“=?”、“?=”进行特殊处理?跟进后发现这玩意是QP编码,用来解决邮件内附件编码问题。Spring调用了java mail的api对文件上传的附件文件名称进行了QP编码。

既然已知Spring的这个特性,那么某些时候或许就可以通过对文件名称进行编码来绕过传统的waf、cdn的防御了。

利用Java mail库生成特殊的文件名:
2.png

上传进行编码后的文件:

3.png

Spring会做decode解析:
4.png

原文地址:http://p2j.cn/?p=1868

SolusVM 授权服务器端

  • 1.编辑hosts文件
vi /etc/hosts
  • 2.添加以下内容
162.211.226.149     www.soluslabs.com
162.211.226.149     soluslabs.com
162.211.226.149     licensing1.soluslabs.net
162.211.226.149     licensing2.soluslabs.net
162.211.226.149     licensing3.soluslabs.net
162.211.226.149     licensing4.soluslabs.net
162.211.226.149     licensing5.soluslabs.net
162.211.226.149     licensing6.soluslabs.net
  • 3.打开SolusVM后台,打开 Configuration –> License 添加授权码并保存
SVMTL-88888-88888-88888-88888-88888-88888

设置完以后可以正常升级。

提示:据说 SolusVM 的主控端还是会偷偷地往法国 OVH 的一个机器发包,非域名,IP 直连请求,可以尝试通过 tcpdump 抓取这个 IP 然后做 iptables 限制访问。
我这边是直接将94.0.0.0/8这个段全部封掉
iptables -I INPUT -s 94.0.0.0/8 -j DROP

Virtualizor破解

license 验证函数的文件路径是:

/usr/local/virtualizor/main/functions.php

一键自动破解脚本

首先创建脚本

vi /opt/license.sh

然后写入下面的内容

#/bin/bash
# 
# 设置license
# 
chattr -i /usr/local/virtualizor/license2.php
rm -rf /usr/local/virtualizor/license2.php
IP=`curl http://members.3322.org/dyndns/getip`
LIC_URL='http://www.03sec.com/make_license.php?str='$IP
license_text=`curl $LIC_URL`
echo $license_text >> /usr/local/virtualizor/license2.php
chattr +i /usr/local/virtualizor/license2.php

最后增加执行权限以及运行脚本

chmod +x /opt/license.sh && /opt/license.sh

测试中发现,virtualizor会自己更改license2.php,即使使用了chattr也没用。建议将上面的脚本设置为定时任务,每小时运行一次

0 * * * * /opt/license.sh