gpt4 book ai didi

java - 异常无法验证 spring MVC 中的目标证书

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:10:07 26 4
gpt4 key购买 nike

我正在尝试使用我的用户名和密码从 jira 服务器获取问题详细信息,但我收到 ssl 错误,提示无法验证证书

那么如何验证证书

网址:http:local/8080/frr/hello

获取错误:

nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for

"https://jira.example.com/rest/api/2/issue/id":

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target] with root cause sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

我的Service.class代码

@Controller
public class Service{


@RequestMapping("/hello")


public String Data(ModelMap model){

RestTemplate restTemplate = new RestTemplate();

ResponseEntity<String> result = restTemplate.exchange("https://jira.example.com/rest/api/2/issue/id", HttpMethod.GET, new HttpEntity<String>(createHeaders("username", "password")), String.class);

model.addAttribute("message", result);


return "helloworld";
}

RestTemplate restTemplate = new RestTemplate();
HttpHeaders createHeaders( String username, String password ){
HttpHeaders header = new HttpHeaders();
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")) );
String base64Creds = "Basic " + new String( encodedAuth );
header.add("Authorization", "Basic " + base64Creds);
return header;
}
}

最佳答案

您面临的问题是您的应用程序无法验证您尝试连接的外部服务器,因为其证书不受信任

简而言之,发生的事情是:

  • 您的应用程序尝试通过安全 (HTTPS) channel 连接到 Jira 实例
  • 为建立安全连接,应用程序下载证书
  • 应用程序通过尝试将证书追溯到已知的 CA(保存在 JRE 证书库中)来检查证书的有效性
  • 证书检查失败,因为证书是自签名的(很可能)或已过期等。

如果此 Jira 实例是本地部署的(由您的公司托管),那么拥有自签名证书的可能性不大。在这种情况下,证书不是由已知的 CA 颁发的,因此如果您希望信任它,您需要手动注册它

首先获取证书:

openssl s_client -connect jira.example.com:443 < /dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > public.crt

然后将其导入到您自己的 keystore 中:

$JAVA_HOME/keytool -import -alias <server_name> -keystore $JAVA_HOME/lib/security/cacerts -file public.crt

注意:以上命令适用于Unix环境。在 Windows 下,我建议从命令行使用类似的 openssl,但也有可用于相同目的的 GUI 工具。

关于java - 异常无法验证 spring MVC 中的目标证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32051596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com