且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用双向身份验证时在Servlet中检索客户端证书?

更新时间:2023-12-04 07:58:28

您将需要导入java.security.cert.X509Certificate和.在您的doGet(...)方法中,使用以下命令:

You will need to import java.security.cert.X509Certificate and . In your doGet(...) method, use the following:

String cn = null;
X509Certificate[] certs = (X509Certificate[]) req
    .getAttribute("javax.servlet.request.X509Certificate");
if (certs != null) {
  String dn = certs[0].getSubjectX500Principal().getName();
  // parse the CN out from the DN (distinguished name)
  Pattern p = Pattern.compile("(^|,)CN=([^,]*)(,|$)");
  cn = p.matcher(dn).find().group(2);
} else {
  // no certificate provided
}