keytool tomcat 启动ssl(单向认证)

通过keytools生成keystore 

keytool -genkey -alias tomcat -keyalg RSA -keypass changeit -storepass changeit -keystore d:\server.keystore 
注意CN必须域名 
比如以后通过https://localhost:8443/path/ 访问网站 
这时候CN = localhost 

2、tomcat 打开SSL配置 

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"    
maxThreads="150" scheme="https" secure="true"    
clientAuth="false" sslProtocol="TLS"    
keystoreFile="d:/server.eystore"  keystorePass="changeit"/>   

3、 直接访问 

浏览器输入https://localhost:8443/path/ 这个时候由于证书不是第三方颁发的所以会提示认证安全证书有问题。 

4、导出x509证书 

keytool -export -alias tomcat -file d:\server.cer -keystore d:\server.keystore. 
先导出一个x509证书 

5、新建client信任的keystore. 

keytool -genkey -alias trust -keyalg RSA -keypass changeit -storepass changeit -keystore d:\trust.keystore 

6、添加服务器端证书进入本地信任keystore 

keytool -import -v -alias tomcat -file d:\server.cer -keystore d:\trust.keystore 

7、java 测试代码 

import java.io.IOException;  
import java.net.MalformedURLException;  
import java.net.URL;  
  
import javax.net.ssl.HttpsURLConnection;  
  
  
public class TClient {  
    public static void main(String[] args) throws Exception {  
        System.setProperty("javax.net.ssl.trustStore", "d:/trust.keystore" );  
        new TClient().test();  
    }  
  
    private void test() {  
        String https_url = "https://localhost:8443/path/login.jsp";  
        URL url;  
        try {  
            url = new URL(https_url);  
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();  
            connection.setDoOutput(true);  
            connection.setRequestMethod("POST");  
            connection.getOutputStream().flush();  
            connection.getOutputStream().close();  
            System.out.println( connection.getPeerPrincipal().toString() );  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  

8 HttpClient包下载地址

   csdn我的资源页,或者 http://hc.apache.org/ 去下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值