I’m trying to use a Regular Expression to match a CN from an X.509 certificate.
The entire string will look something like this:
CN=JASON, OU=MYOU, O=MYORG
Here’s the nonworking code:
Object certChain = request
.getAttribute("javax.servlet.request.X509Certificate");
String name = "";
if (certChain != null) {
X509Certificate certs[] = (X509Certificate[]) certChain;
X509Certificate cert = certs[0];
name = cert.getSubjectDN().getName();
}
String strPattern = "CN=(.*?),";
Pattern pattern = Pattern.compile(strPattern);
Matcher matcher = pattern.matcher(name);
String cname = "";
if (matcher.matches()) {
cname = matcher.group(1);
}
Matcher.matches() returns false, but I don’t see a problem with the pattern. Can anyone help?
Jason
To match the string fully in
Matcher.matches(), you could use: