System.setProperty("javax.net.ssl.keyStore", xxx) 在 api 休息中不起作用?

问题描述 投票:0回答:2

我有以下代码,如果我通过控制台运行它,它可以正常工作

public static void main(String[] args) throws MalformedURLException {
    // TODO Auto-generated method stub
    try {
        String jSonData = "xxx";

        String nombreCertificadoTicket = "CERTIFICADO.p12";
        String passCertificadoTicket = "PASSWORD";
        String pathCertificado = "/opt/tomcat/properties/"+nombreCertificadoTicket;
        String url_crearTicket = "URL SERVICE";
        
        System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
        System.setProperty("javax.net.ssl.keyStore", pathCertificado);
        System.setProperty("javax.net.ssl.keyStorePassword", passCertificadoTicket);
        
        URL urlCrearTicket = new URL(url_crearTicket);                                                  
        
        HttpURLConnection conexionCrearTicket = (HttpURLConnection) urlCrearTicket.openConnection();
        conexionCrearTicket.setDoOutput(true);
        conexionCrearTicket.setRequestMethod("POST");
        conexionCrearTicket.setRequestProperty("content-type", "application/json");

        OutputStream os = conexionCrearTicket.getOutputStream();
        os.write(jSonData.getBytes());
        os.flush();
        
        System.out.println("CODE :"+conexionCrearTicket.getResponseCode());

我通过 consol 执行这个类,它执行并返回代码 ok

但是我将完全相同的代码放入休息服务中,它返回 401 未经授权的错误,因此我认为它没有正确放置安全证书

@Path("PruebaCertificado")
public class PruebaCertificado {

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public RespuestaWs recibe(InputStream entrada) {
    
    RespuestaWs respuesta = new RespuestaWs();
    
    try {
        
        String jSonData = "";
        
        String jSonData = "xxx";

        String nombreCertificadoTicket = "CERTIFICADO.p12";
        String passCertificadoTicket = "PASSWORD";
        String pathCertificado = "/opt/tomcat/properties/"+nombreCertificadoTicket;
        String url_crearTicket = "URL SERVICE";
        
        System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
        System.setProperty("javax.net.ssl.keyStore", pathCertificado);
        System.setProperty("javax.net.ssl.keyStorePassword", passCertificadoTicket);
        
        URL urlCrearTicket = new URL(url_crearTicket);                                                  
        
        HttpURLConnection conexionCrearTicket = (HttpURLConnection) urlCrearTicket.openConnection();
        conexionCrearTicket.setDoOutput(true);
        conexionCrearTicket.setRequestMethod("POST");
        conexionCrearTicket.setRequestProperty("content-type", "application/json");

        OutputStream os = conexionCrearTicket.getOutputStream();
        os.write(jSonData.getBytes());
        os.flush();
        
        System.out.println("CODE :"+conexionCrearTicket.getResponseCode());

我需要在tomcat中进行一些配置或者类似的东西吗?

环境有apache tomcat 8 y java 1.8

java api rest
2个回答
0
投票

我用我找到的另一个代码解决了这个问题,我留下它以防它对某人有用。注意我使用 HttpsURLConnection

        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        InputStream keyInput = new FileInputStream(pathCertificado);
        keyStore.load(keyInput, passCertificadoTicket.toCharArray());
        keyInput.close();
        keyManagerFactory.init(keyStore, passCertificadoTicket.toCharArray());
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
        
        HttpsURLConnection conexionCrearTicket = (HttpsURLConnection) urlCrearTicket.openConnection();
        conexionCrearTicket.setSSLSocketFactory(context.getSocketFactory());
        conexionCrearTicket.setDoOutput(true);
        conexionCrearTicket.setRequestMethod("POST");
        conexionCrearTicket.setRequestProperty("content-type", "application/json");

-1
投票

Alexis Jimenez,¿您如何创建/声明 urlCrearTicket 变量以及您使用了哪些库?我评论你是因为我有错误转换为 HttpsURLConnection

© www.soinside.com 2019 - 2024. All rights reserved.