Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java如何提取;发给「;还是来自HttpRequest中SSL证书的用户?_Java_Ssl_Httprequest - Fatal编程技术网

Java如何提取;发给「;还是来自HttpRequest中SSL证书的用户?

Java如何提取;发给「;还是来自HttpRequest中SSL证书的用户?,java,ssl,httprequest,Java,Ssl,Httprequest,在Java中,如何从HttpRequest中的SSL证书中提取“颁发给”或用户 我的工作内容: Object certChain = request .getAttribute("javax.servlet.request.X509Certificate"); if (certChain != null) { X509Certificate certs[] = (X509Certificate[]) certChain; X509Certificate cert = certs

在Java中,如何从
HttpRequest
中的SSL证书中提取“颁发给”或用户

我的工作内容:

 Object certChain = request
    .getAttribute("javax.servlet.request.X509Certificate");
if (certChain != null)
{
  X509Certificate certs[] = (X509Certificate[]) certChain;
  X509Certificate cert = certs[0];
  String user = cert.getSubjectDN().getName();
}
不应该再使用了

被诋毁,替换为GetSubjectX500 Principal()

因此,请使用:

cert[0].getSubjectX500Principal().getName();

这是的JavaDoc。

这是为httprequest中的证书提取“颁发给”aka主题的代码

import java.security.cert.X509Certificate;
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;

X509Certificate[] certs = (X509Certificate[])request.getAttribute("javax.servlet.request.X509Certificate");
if ((certs == null) || (certs.length == 0)) {
   return null;
}

String name = certs[0].getSubjectX500Principal().getName(); // if you are looking for issuer then use cert[0].getIssuerX500Principal().getName();
LdapName ldapName = null;
try {
   ldapName = new LdapName(name);
} catch (InvalidNameException e) {
   throw new RuntimeException(e);
}

for (Rdn rdn : ldapName.getRdns()) {
   String type = rdn.getType();
   if ("CN".equals(type)) { 
        String issuedTo = (String)rdn.getValue();
   }
}
JDK 6在其rt.jar中本地包含API getSubjectX500Prinicpal(),但不确定以前的版本,

是的,你就是这样做的。你的问题是什么?@erickson我只需要确认这确实是正确的做法,而不是我必须去查一下在JavaDoc中使用的
诋毁
的意思。似乎基本上是一种不太严重的
弃用形式
。至少据我所知,我有个打字错误,很抱歉。作为提示,我在上问了一个问题,您可能想检查一下。谢谢您的回答jschoenmy编译器找不到方法getSubjectX500Principal(),这是什么库?我如何从HttpRequest对象中获得它我的编译器没有遇到任何问题。我使用的是JDK 6,这个类在rt.jar中可用,您使用的是哪个版本的编译器?啊。。我使用javax而不是java作为基本包。无论如何,谢谢。我刚意识到你的名字叫“stackoverflow”…太好了!!:D