Java如何从静态方法读/写类路径中的文件

Java如何从静态方法读/写类路径中的文件,java,file-writing,Java,File Writing,我正在为我雇主的软件产品制作许可证。该应用程序是部署在tomcat中的web应用程序。应用程序部署在cusotmer premise 我使用maven作为构建工具。我的测试许可证文件位于src/main/resources/license.lic。这意味着我需要阅读License.lic,它包含加密文本。用户登录后,我解密并检查许可证是否仍然有效。最后,我将一些内容写回许可证文件(license.lic) 我遇到的问题是,在检查之后,我修改了更新的文本并对其进行了加密,但无法回写到license

我正在为我雇主的软件产品制作许可证。该应用程序是部署在tomcat中的web应用程序。应用程序部署在cusotmer premise

我使用maven作为构建工具。我的测试许可证文件位于
src/main/resources/license.lic
。这意味着我需要阅读License.lic,它包含加密文本。用户登录后,我解密并检查许可证是否仍然有效。最后,我将一些内容写回许可证文件(license.lic)

我遇到的问题是,在检查之后,我修改了更新的文本并对其进行了加密,但无法回写到license.lic。读和写都是静态方法

我写回License.lic的代码如下:

protected static void writeToLicense(LicenseData licData) throws Exception{
        String plainText = licData.toString();
        String encText = LicenseEncDec.encrypt(plainText);
        System.out.println("encText is "+encText);

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL url = classLoader.getResource(licFileName1);
        File file = new File(url.toURI().getPath());    

        PrintWriter pw = new PrintWriter(new FileWriter(file));
        pw.println(encText);
        pw.flush();
        pw.close();
    }   
private static String readFile() throws IOException {

    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    InputStream is = loader.getResourceAsStream(licFileName1);

    BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

    String line = br.readLine();

    br.close();
    is.close();

    return line;
}   
从License.lic读取内容如下:

protected static void writeToLicense(LicenseData licData) throws Exception{
        String plainText = licData.toString();
        String encText = LicenseEncDec.encrypt(plainText);
        System.out.println("encText is "+encText);

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL url = classLoader.getResource(licFileName1);
        File file = new File(url.toURI().getPath());    

        PrintWriter pw = new PrintWriter(new FileWriter(file));
        pw.println(encText);
        pw.flush();
        pw.close();
    }   
private static String readFile() throws IOException {

    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    InputStream is = loader.getResourceAsStream(licFileName1);

    BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

    String line = br.readLine();

    br.close();
    is.close();

    return line;
}   

无法将License.lic写回。我的密码有问题吗

那是个错误的设计。一个安全性良好的web应用程序不应该有权限写入自己的代码或类路径中的任何地方。句号。如果应用程序的一个部分被破坏,不这样做会提供无限的攻击向量

因此,
src/main/resources
下的任何内容都应被视为只读。正确的方法是有一个专用目录,可以选择在环境变量中声明,您可以在其中编写所需的任何内容。如果
getResource
只提供只读访问,而任何试图绕过它的尝试都会带来问题,那么这不是意外:

  • 正如我在第一段中解释的那样
  • 根据定义,未爆炸战争是只读的
一旦在某个地方有了一个数据目录,将资源转换为普通文件就不再是一个令人头痛的问题:您有一个普通文件,可以用它做您想做的事情


我必须承认这仅仅是我的观点,但我真的敦促您对可写资源三思。

您假设License.lic是文件系统中的可写文件。如果不是,你需要考虑它是如何存储的。e、 g.它在JAR中吗?有例外吗?@NielsNet我知道它试图写入本地文件,但从类路径读取。所以我不希望出现任何错误,只是文件没有更改。为什么要在这方面花费时间?如果您的客户希望在不支付“许可证”的情况下使用您的代码,那么他所要做的就是反编译您的代码,删除检查,然后--presto。如果您想要一些(更好的)安全性,请只向您的客户机提供一个客户机,并让您的业务逻辑在客户机无法从License.lic读取且可写的服务器上运行。License.lic位于类路径中。这样我既能读又能写。也不例外。