微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基于classfinal对java项目加密

1 单个jar包加密

官方文档
https://gitee.com/roseboy/classfinal

jar包下载地址
https://repo1.maven.org/maven2/net/roseboy/classfinal-fatjar/1.2.1/classfinal-fatjar-1.2.1.jar

将下载好的jar包和需要加密的包放到一个文件夹,执行下面命令

java -jar classfinal-fatjar-1.2.1.jar -file 换成你的jar包 -packages 你的包名  -pwd 你的密码 -Y

具体参数说明,可以自行增减

-file        加密的jar/war完整路径
-packages    加密的包名(可为空,多个用","分割)
-libjars     jar/war包lib下要加密jar文件名(可为空,多个用","分割)
-cfgfiles    需要加密的配置文件,一般是classes目录下的yml或properties文件(可为空,多个用","分割)
-exclude     排除的类名(可为空,多个用","分割)
-classpath   外部依赖的jar目录,例如/tomcat/lib(可为空,多个用","分割)
-pwd         加密密码,如果是#号,则使用无密码模式加密
-code        机器码,在绑定的机器生成,加密后只可在此机器上运行
-Y           无需确认,不加此参数会提示确认以上信息

加密后的文件多了个encrypted

在这里插入图片描述

2 查看加密效果

反编译工具下载
https://blog.csdn.net/lh155136/article/details/114310951

classfinal主要加密class文件,主要是对方法返回null处理

在这里插入图片描述

在这里插入图片描述

3 jar包部署

启动jar包,我把encrypted去掉了

nohup java -javaagent:你的包名.jar="-pwd 你的密码" -jar 你的包名.jar

这种方式通过ps -ef|grep jar查看进程可以查到密码

so

另一种方式启动推荐

nohup java -javaagent:你的包名.jar -jar 你的包名.jar

使用nohup命令启动时,如果系统支持gui,会弹出输入密码的界面,如果是纯命令行下,不支持gui,则需要在同级目录下的classfinal.txt或yourpaoject-encrypted.classfinal.txt中写入密码,项目读取到密码后会清空此文件
为了保险起见请自行删除文件

启动时再加上这个参数-XX:+disableAttachMechanism(防止连接上部署好的jar包来反编译)
关闭工具关联JVM的功能认情况下,该选项开启,此时可以使用诊断工具如:jcmd,jstack,jmap和jinfo。

nohup java  -XX:+disableAttachMechanism -javaagent:你的包名.jar -jar 你的包名.jar

3 maven集成

对于自己项目的build,自行增删,版本和参数可以提取到最大的pom文件

<build>
    <plugins>
        <!--   spring boot maven 打包插件  -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.2.7.RELEASE</version>
            <configuration>
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        
        <!-- 加密插件,防止反编译 -->
        <plugin>
            <!-- https://gitee.com/roseboy/classfinal -->
            <groupId>net.roseboy</groupId>
            <artifactId>classfinal-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <!--加密密码,如果是#号,则使用无密码模式加密,【加密后没有pom文件,不用担心泄漏】-->
                <password>你的密码</password>
                <!--加密的包名(可为空,多个用","分割)-->
                <packages>cn.fox.demo</packages>
                <!--需要加密的配置文件,一般是classes目录下的yml或properties文件(可为空,多个用","分割)-->
                <cfgfiles>*.properties,/mapper/*mapper.xml</cfgfiles>
                <!--外部依赖的jar目录,例如/tomcat/lib(可为空,多个用","分割)-->
                <!--<classpath></classpath>-->
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>classFinal</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

插件配置在pom文件里,执行maven的package时,在target下面会出现xxx-encrypted.jar,这个加密后的包,放到linux部署即可

3 错误总结

在部署过程中遇到很多多多多多多多多多多多多多多多多问题

  1. 打包插件不统一
    就是标签里面有没有用的其他标签,打包后无法运行,最好用我上面的
  2. 远程调用bean注入冲突
The bean 'XXX.FeignClientSpecification', defined in null, Could not be registered

@FeignClient注解里面加一个contextId = “isp-form”

@FeignClient(name="common-service", contextId = "example")
  1. mybatisplus模板注入冲突、Redis模板冲突
    将项目里面的多个模板合成一个放到common里面,其他模块引用即可
  2. 最恶心的错重新打包就好了
nohup: ignoring input
Failed to find Premain-Class manifest attribute in provider.jar
Error occurred during initialization of VM
agent library Failed to init: instrument
  1. 更恶心的错mapUnderscoreToCamelCase
2021-03-15 16:07:11.165  WARN 7770 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Controller': Unsatisfied dependency expressed through field 'Service'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'TServiceImpl': Injection of resource dependencies Failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'TMapper' defined in URL [jar:file:/data1/aa/provider-1.0.jar!/BOOT-INF/classes!/com/itl/aa/system/provider/mapper/TMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.sqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.lang.NoSuchFieldError: mapUnderscoreToCamelCase

这个错误的原因是配置文件typeAliasesPackage配错了,然后一直报map-underscore-to-camel-case的问题,我一直以为是我xml配置文件没有驼峰命名

在这里插入图片描述

原文地址:https://www.jb51.cc/wenti/3283083.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐