我要使用 web3j Tomcat8 上的 servlet 内部库Linux 服务器 (Ubuntu 16.04) 上的网络服务器。
我需要从 servlet 读取文件。 servlet中的代码是:
Credentials credentials = null;
String password = "secretPassword";
String pathToWalletFile = "/home/ubuntu/.ethereum/testnet/keystore/UTC--2017-06-18T03-11-13.235052023Z--6c992fb9531c9285ca9aba80c32a63074b0acd00";
File file = new File(pathToWalletFile);
try {
credentials = WalletUtils.loadCredentials(
password,
file
);
} catch (CipherException e) {
e.printStackTrace();
}
该文件具有权限:644 (-rw-r--r--) 在 Ubuntu 16.04 上使用“sudo apt install tomcat8”以默认设置安装的 Tomcat8(用户:tomcat8)
如果我调用 servlet,我会收到以下错误:
HTTP Status 500 - /home/ubuntu/.ethereum/testnet/keystore/UTC--2017-06-18T03-11-13.235052023Z--6c992fb9531c9285ca9aba80c32a63074b0acd00 (Permission denied)
type Exception report
message /home/ubuntu/.ethereum/testnet/keystore/UTC--2017-06-18T03-11-13.235052023Z--6c992fb9531c9285ca9aba80c32a63074b0acd00 (Permission denied)
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.io.FileNotFoundException: /home/ubuntu/.ethereum/testnet/keystore/UTC--2017-06-18T03-11-13.235052023Z--6c992fb9531c9285ca9aba80c32a63074b0acd00 (Permission denied)
java.io.FileInputStream.open0(Native Method)
java.io.FileInputStream.open(FileInputStream.java:195)
java.io.FileInputStream.<init>(FileInputStream.java:138)
com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:756)
com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2740)
org.web3j.crypto.WalletUtils.loadCredentials(WalletUtils.java:76)
net.cryptonomica.tomcatweb3j.TestServlet.doGet(TestServlet.java:75)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.32 (Ubuntu) logs.
Apache Tomcat/8.0.32 (Ubuntu)
如果我将文件放在 WEB-INF 中,它就可以工作。如何允许在 tomcat(或 jetty )中访问 WEB-INF 之外的文件?
更新: 用户tomcat8可以读取/home/ubuntu/.ethereum/,但是不能读取/home/ubuntu/.ethereum/testnet/目录
$ sudo -u tomcat8 ls /home/ubuntu/.ethereum/testnet/
返回:
ls: cannot access '/home/ubuntu/.ethereum/testnet/': Permission denied
即使我将“tomcat8”用户添加到“ubuntu”组
$members ubuntu
现在返回:
ubuntu tomcat8
和
$ ls -l /home/ubuntu/.ethereum/testnet/
返回:
total 8
drwxr--r-- 5 ubuntu ubuntu 4096 Jun 17 17:33 geth
srw------- 1 ubuntu ubuntu 0 Jun 18 14:08 geth.ipc
drwxr--r-- 2 ubuntu ubuntu 4096 Jun 18 03:11 keystore
但是tomcat8仍然没有对/home/ubuntu/.ethereum/testnet/的读取权限
将文件移动到另一个目录(用户“tomcat8”有权访问的目录)解决了问题。但我仍然不明白问题出在哪里。一般来说,我希望从 servlet 中读取文件/home/ubuntu/.ethereum/testnet/
最佳答案
这是一个基本的权限问题。
看起来好像这个文件在 ubuntu
用户的主目录下。在这种情况下更改权限是个坏主意。更好的选择是将这些文件的存储移动到任何特定用户的主目录之外,或者更好的是,将它们移动到 tomcat 用户的主目录(如果该目录仅用于服务器管理的内容)
否则,您需要做的是找出tomcat进程以哪个用户身份运行,然后相应地授予权限:
sudo chown tomcat:tomcat /home/ubuntu/.ethereum/testnet/keystore/UTC--2017-06-18T03-11-13.235052023Z--6c992fb9531c9285ca9aba80c32a63074b0acd00
#OR
sudo chmod a+r /home/ubuntu/.ethereum/testnet/keystore/UTC--2017-06-18T03-11-13.235052023Z--6c992fb9531c9285ca9aba80c32a63074b0acd00
#OR grant folder access if files are dynamically generated
sudo chmod -R a+r /home/ubuntu/.ethereum/testnet/keystore/
以上代码假设运行tomcat的用户名为tomcat
。
关于java - 访问 WEB-INF 之外的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44615255/