我正在尝试向这个端点发送一个请求,并以字符串的形式获得响应。keystore密码是“password”。但它说错了。我需要澄清这一点,并需要在代码中的任何chnage工作。
package org.example;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.rest.RestBindingMode;
import org.apache.camel.support.jsse.KeyManagersParameters;
import org.apache.camel.support.jsse.KeyStoreParameters;
import org.apache.camel.support.jsse.SSLContextParameters;
import org.apache.camel.component.jetty.JettyHttpComponent;
import java.util.Arrays;
public class CamelRestHttpsExample {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
String PASSWORD ="password";
KeyStoreParameters keyStoreParameters = new KeyStoreParameters();
keyStoreParameters.setType("PKCS12");
keyStoreParameters.setResource("ssl_key.p12");// Replace with your keystore path
keyStoreParameters.setPassword(Arrays.toString(PASSWORD.toCharArray())); // Replace with your keystore password
KeyManagersParameters keyManagersParameters = new KeyManagersParameters();
keyManagersParameters.setKeyStore(keyStoreParameters);
SSLContextParameters sslContextParameters = new SSLContextParameters();
sslContextParameters.setKeyManagers(keyManagersParameters);
JettyHttpComponent jettyComponent = context.getComponent("jetty", JettyHttpComponent.class);
jettyComponent.setSslContextParameters(sslContextParameters);
jettyComponent.setSslKeyPassword(Arrays.toString(PASSWORD.toCharArray()));
jettyComponent.setSslPassword(Arrays.toString(PASSWORD.toCharArray()));
jettyComponent.setKeystore("ssl_key.p12");
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
restConfiguration()
.component("jetty")
.scheme("https")
.host("127.0.0.1")
.port(8443)
.bindingMode(RestBindingMode.auto)
.dataFormatProperty("prettyPrint", "true")
.contextPath("/api")
.endpointProperty("sslKeystore", "ssl_key.p12")
.endpointProperty("sslPassword", Arrays.toString(PASSWORD.toCharArray()))
.endpointProperty("sslKeyPassword", Arrays.toString(PASSWORD.toCharArray()));
rest("/hello")
.get()
.route()
.transform().constant("Hello from Camel over HTTPS!")
.endRest();
}
});
context.start();
System.out.println("Camel context started. Access the service at: https://localhost:8443/api/hello");
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
context.stop();
} catch (Exception e) {
e.printStackTrace();
}
}));
}
}
错误消息:
Exception in thread "main" org.apache.camel.RuntimeCamelException: java.io.IOException: keystore password was incorrect
at org.apache.camel.component.jetty.JettyHttpComponent.createConnector(JettyHttpComponent.java:613)
at org.apache.camel.component.jetty.JettyHttpComponent.getSslSocketConnector(JettyHttpComponent.java:593)
at org.apache.camel.component.jetty.JettyHttpComponent.getConnector(JettyHttpComponent.java:569)
at org.apache.camel.component.jetty.JettyHttpComponent.connect(JettyHttpComponent.java:326)
at org.apache.camel.http.common.HttpCommonEndpoint.connect(HttpCommonEndpoint.java:186)
at org.apache.camel.http.common.HttpConsumer.doStart(HttpConsumer.java:58)
at org.apache.camel.component.jetty.JettyHttpConsumer.doStart(JettyHttpConsumer.java:31)
at org.apache.camel.support.service.BaseService.start(BaseService.java:119)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
at org.apache.camel.impl.engine.AbstractCamelContext.startService(AbstractCamelContext.java:3464)
at org.apache.camel.impl.engine.InternalRouteStartupManager.doStartOrResumeRouteConsumers(InternalRouteStartupManager.java:401)
at org.apache.camel.impl.engine.InternalRouteStartupManager.doStartRouteConsumers(InternalRouteStartupManager.java:319)
at org.apache.camel.impl.engine.InternalRouteStartupManager.safelyStartRouteServices(InternalRouteStartupManager.java:213)
at org.apache.camel.impl.engine.InternalRouteStartupManager.doStartOrResumeRoutes(InternalRouteStartupManager.java:147)
at org.apache.camel.impl.engine.AbstractCamelContext.doStartCamel(AbstractCamelContext.java:3167)
at org.apache.camel.impl.engine.AbstractCamelContext.doStartContext(AbstractCamelContext.java:2847)
at org.apache.camel.impl.engine.AbstractCamelContext.doStart(AbstractCamelContext.java:2798)
at org.apache.camel.support.service.BaseService.start(BaseService.java:119)
at org.apache.camel.impl.engine.AbstractCamelContext.start(AbstractCamelContext.java:2494)
at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:245)
at org.example.CamelRestHttpsExample.main(CamelRestHttpsExample.java:61)
Caused by: java.io.IOException: keystore password was incorrect
at java.base/sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:2159)
at java.base/sun.security.util.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:221)
at java.base/java.security.KeyStore.load(KeyStore.java:1473)
at org.apache.camel.support.jsse.KeyStoreParameters.createKeyStore(KeyStoreParameters.java:166)
at org.apache.camel.support.jsse.KeyManagersParameters.createKeyManagers(KeyManagersParameters.java:102)
at org.apache.camel.support.jsse.SSLContextParameters.createSSLContext(SSLContextParameters.java:262)
at org.apache.camel.component.jetty.JettyHttpComponent.createSslContextFactory(JettyHttpComponent.java:653)
at org.apache.camel.component.jetty.JettyHttpComponent.createConnector(JettyHttpComponent.java:611)
... 20 more
Caused by: java.security.UnrecoverableKeyException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
... 28 more
进程已完成,退出代码为% 1。
1条答案
按热度按时间ctrmrzij1#
你的主要问题是调用
Arrays.toString(PASSWORD.toCharArray()))
,你希望它以String的形式返回密码-但它不是这样设计的:举个例子:
你真正想做的是