6

I need to extract more information than just the CN of the certificate. Currently, I only get the standard UserDetails loadUserByUsername(String arg) where arg is the CN of the certificate. I need to get the X509Certificate object. Is it possible?

on spring security xml file :

<x509 subject-principal-regex="CN=(.*?),"  user-service-ref="myUserDetailsService" />
imambenjol
  • 196
  • 1
  • 2
  • 5

2 Answers2

6

No you can't get it that way. You need to grab it from the HttpServletRequest:

X509Certificate[] certs = (X509Certificate[])HttpServletRequest.getAttribute("javax.servlet.request.X509Certificate");
Gandalf
  • 9,648
  • 8
  • 53
  • 88
  • If you print out `cert[0].toString()`, should the cert begin with something like `---- BEGIN CERTIFICATE ---- ....` and end with `---- END CERTIFICATE -----`? – Kevin Meredith May 09 '13 at 18:46
1

It is also worth noting that once you are authorized by the in-built X509AuthenticationFilter of Spring Security as it has accepted your certificate, then you can access the X509Certificate as

Object object = SecurityContextHolder.getContext().getAuthentication().getCredentials();
if (object instanceof X509Certificate)
{
    X509Certificate x509Certificate = (X509Certificate) object;
    //convert to bouncycastle if you want
    X509CertificateHolder x509CertificateHolder =
        new X509CertificateHolder(x509Certificate.getEncoded());
    ...
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428