Skip to content

Commit 540cc35

Browse files
Add Support Extracting DN From X500Principal
Closes spring-projectsgh-16980 Signed-off-by: Max Batischev <[email protected]>
1 parent bb2fd24 commit 540cc35

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

web/src/main/java/org/springframework/security/web/authentication/preauth/x509/SubjectDnX509PrincipalExtractor.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@
4343
* "[email protected], CN=..." giving a user name "[email protected]"
4444
*
4545
* @author Luke Taylor
46+
* @author Max Batischev
4647
*/
4748
public class SubjectDnX509PrincipalExtractor implements X509PrincipalExtractor, MessageSourceAware {
4849

@@ -52,14 +53,16 @@ public class SubjectDnX509PrincipalExtractor implements X509PrincipalExtractor,
5253

5354
private Pattern subjectDnPattern;
5455

56+
private boolean extractPrincipalNameFromX500Principal = false;
57+
5558
public SubjectDnX509PrincipalExtractor() {
5659
setSubjectDnRegex("CN=(.*?)(?:,|$)");
5760
}
5861

5962
@Override
6063
public Object extractPrincipal(X509Certificate clientCert) {
61-
// String subjectDN = clientCert.getSubjectX500Principal().getName();
62-
String subjectDN = clientCert.getSubjectDN().getName();
64+
String subjectDN = this.extractPrincipalNameFromX500Principal ? clientCert.getSubjectX500Principal().getName()
65+
: clientCert.getSubjectDN().getName();
6366
this.logger.debug(LogMessage.format("Subject DN is '%s'", subjectDN));
6467
Matcher matcher = this.subjectDnPattern.matcher(subjectDN);
6568
if (!matcher.find()) {
@@ -98,4 +101,14 @@ public void setMessageSource(MessageSource messageSource) {
98101
this.messages = new MessageSourceAccessor(messageSource);
99102
}
100103

104+
/**
105+
* If true then extracts principal name from X500Principal, defaults to {@code false}
106+
* @param extractPrincipalNameFromX500Principal whether to extract the principal name
107+
* from X500Principal
108+
* @since 7.0
109+
*/
110+
public void setExtractPrincipalNameFromX500Principal(boolean extractPrincipalNameFromX500Principal) {
111+
this.extractPrincipalNameFromX500Principal = extractPrincipalNameFromX500Principal;
112+
}
113+
101114
}

web/src/test/java/org/springframework/security/web/authentication/preauth/x509/SubjectDnX509PrincipalExtractorTests.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,13 @@ public void defaultCNPatternReturnsExcpectedPrincipal() throws Exception {
5454
assertThat(principal).isEqualTo("Luke Taylor");
5555
}
5656

57+
@Test
58+
public void defaultCNPatternReturnsPrincipalWhenExtractPrincipalNameFromX500PrincipalIsTrue() throws Exception {
59+
this.extractor.setExtractPrincipalNameFromX500Principal(true);
60+
Object principal = this.extractor.extractPrincipal(X509TestUtils.buildTestCertificate());
61+
assertThat(principal).isEqualTo("Luke Taylor");
62+
}
63+
5764
@Test
5865
public void matchOnEmailReturnsExpectedPrincipal() throws Exception {
5966
this.extractor.setSubjectDnRegex("emailAddress=(.*?),");

0 commit comments

Comments
 (0)