在这里将服务器的名字设为localhost,geth对应的url为localhost/eth,其对应的密码文件通过auth_basic_user_file指令进行制定。 此时可以用浏览器中访问看什么效果,此时应出现的效果应该是: 访问:http://localhost/,出现404页面 访问:http://localhost/eth 当输入正确的用户名和密码后点击OK,如果没有出现错误,这说明配置成功。 到现在为止,已经成功的用nginx为geth构建了一层安全防护,并给geth映射了一个外部访问的url,现在访问geth不必通过http://<ip>:<port>的方式,而是直接访问映射的url。如geth attach http://username:password@ip/eth 所以现在是时候把geth暴露出去的rpc端口采取禁止外部访问的操作了。腾讯云阿里云都有安全组可以实现这个操作。 web3j使用的Http模块为OkHttp3,认证需要的用户名和密码信息可以按照OkHttp3添加认证的方式来添加。没使用Http认证时,web3j构建Admin对象的方式是: Admin ethClient; ethClient = Admin.build(new HttpService(url)); 加入认证用户名和密码的方式: OK,现在可以用Web3j访问加入了Http认证保护的geth节点了。 上面适合安卓客户端,而ios端调用web3.js来访问Http Basic Authentication保护资源的问题 ios客户端访问geth的方式跟安卓端不一样。由于没有开源成熟的OC语言的类似于web3j的库,ios端只有通过webview的方式建立一个Html页面,在页面里通过js来调用web3.js的API函数来访问geth。在浏览器里面访问有Http Basic Authentication的geth节点,会报错,这是浏览器在报js跨域访问的问题。一番谷歌后仿照网上解决CROS的方法,通过添加配置nginx的配置文件添加,最终的配置: 当然除了HTTP基本认证还可以配置带证书保护的Https,这个更安全。 Http协议的传输过程是明文,因此使用HTTP协议传输隐私信息非常不安全。HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,要比http协议安全。它能够对传输内容进行加密。HTTPS需要申请一个CA证书,阿里云上面可以申请免费的Symantec证书。阿里云证书需要先有一个可用的域名,该域名映射到节点的ip。申请证书后再下载到节点中,然后在nginx的配置文件中进行配置。 nginx配置HTTPS,具体配置方法如下: 重启nginx服务既可以使用https了 在某些时候,我们可能还需要仅允许部分IP可以访问。当然,这个需求,其实在阿里云或腾讯云的安全组上就可以实现,或者使用服务器的iptables防火墙也是可以的。下面这种方法是利用nginx中访问控制的功能进行实现的。nginx配置如下: 例如以上配置,出现在allow中的IP是可以访问的,未出现在allow中的IP访问 https://localhost/eth 时,会出现403拒绝访问的状态码。 以上,就是今天分享的全部内容了。 希望大家通过以上方式可以解决自己的实际需求,解决自己目前所遇到的问题。
curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}' http://eth:123456@lijie.bbef.top/eth
3.3 安卓机ios访问配置
private static OkHttpClient buildBasicAuthClient() { return new OkHttpClient.Builder().authenticator(new Authenticator() { @Override public Request authenticate(Route route, Response response) throws IOException { String credential = okhttp3.Credentials.basic(Define.userName, Define.passwd); return response.request().newBuilder().header("Authorization", credential).build(); } }).build(); } Admin ethClient; ethClient = Admin.build(new HttpService(url,buildBasicAuthClient(),false));
3.4 nginx 跨域配置
server { listen 80 default_server; server_name localhost; location / { try_files $uri $uri/ =404; } location /eth { auth_basic "Restricted Area"; auth_basic_user_file /etc/nginx/geth.htpasswd; proxy_pass http://localhost:8545; if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Auth orization'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } } access_log /var/log/nginx/localhost.log main; }
3.5 nginx https 配置
server{ listen 443 ssl; server_name localhost; charset utf-8; ssl_certificate /etc/nginx/cert/xxx.pem; ssl_certificate_key /etc/nginx/cert/xxx.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL; ssl_prefer_server_ciphers on; location / { try_files $uri $uri/ =404; } location /eth { auth_basic "Restricted Area"; auth_basic_user_file /etc/nginx/geth.htpasswd; proxy_pass http://localhost:8545; if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } } access_log /var/log/nginx/localhost.log main; }
3.6 nginx 访问控制配置
server{ listen 443 ssl; server_name localhost; charset utf-8; ssl_certificate /etc/nginx/cert/xxx.pem; ssl_certificate_key /etc/nginx/cert/xxx.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL; ssl_prefer_server_ciphers on; location / { try_files $uri $uri/ =404; } location /eth { auth_basic "Restricted Area"; auth_basic_user_file /etc/nginx/geth.htpasswd; proxy_pass http://localhost:8545; if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } #####限制仅部分IP访问############## allow 49.7.66.132; allow 124.65.8.139; deny all; ######限制仅部分IP访问############## } access_log /var/log/nginx/localhost.log main; }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。