Skip to content

Commit 430b673

Browse files
Add Support SubjectX500PrincipalExtractor
Closes gh-16980 Signed-off-by: Max Batischev <[email protected]>
1 parent ff8b77d commit 430b673

File tree

12 files changed

+276
-12
lines changed

12 files changed

+276
-12
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/X509Configurer.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 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.
@@ -33,6 +33,7 @@
3333
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
3434
import org.springframework.security.web.authentication.preauth.PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails;
3535
import org.springframework.security.web.authentication.preauth.x509.SubjectDnX509PrincipalExtractor;
36+
import org.springframework.security.web.authentication.preauth.x509.SubjectX500PrincipalExtractor;
3637
import org.springframework.security.web.authentication.preauth.x509.X509AuthenticationFilter;
3738
import org.springframework.security.web.authentication.preauth.x509.X509PrincipalExtractor;
3839
import org.springframework.security.web.context.RequestAttributeSecurityContextRepository;
@@ -74,6 +75,7 @@
7475
*
7576
* @author Rob Winch
7677
* @author Ngoc Nhan
78+
* @author Max Batischev
7779
* @since 3.2
7880
*/
7981
public final class X509Configurer<H extends HttpSecurityBuilder<H>>
@@ -161,14 +163,38 @@ public X509Configurer<H> authenticationUserDetailsService(
161163
* @param subjectPrincipalRegex the regex to extract the user principal from the
162164
* certificate (i.e. "CN=(.*?)(?:,|$)").
163165
* @return the {@link X509Configurer} for further customizations
166+
* @deprecated Please use {{@link #extractPrincipalNameFromEmail(boolean)}} instead
164167
*/
168+
@Deprecated
165169
public X509Configurer<H> subjectPrincipalRegex(String subjectPrincipalRegex) {
170+
if (this.x509PrincipalExtractor instanceof SubjectX500PrincipalExtractor) {
171+
throw new IllegalStateException(
172+
"Cannot use subjectPrincipalRegex and extractPrincipalNameFromEmail together. "
173+
+ "Please use one or the other.");
174+
}
166175
SubjectDnX509PrincipalExtractor principalExtractor = new SubjectDnX509PrincipalExtractor();
167176
principalExtractor.setSubjectDnRegex(subjectPrincipalRegex);
168177
this.x509PrincipalExtractor = principalExtractor;
169178
return this;
170179
}
171180

181+
/**
182+
* If true then DN will be extracted from EMAIlADDRESS, defaults to {@code false}
183+
* @param extractPrincipalNameFromEmail whether to extract DN from EMAIlADDRESS
184+
* @since 7.0
185+
*/
186+
public X509Configurer<H> extractPrincipalNameFromEmail(boolean extractPrincipalNameFromEmail) {
187+
if (this.x509PrincipalExtractor instanceof SubjectDnX509PrincipalExtractor) {
188+
throw new IllegalStateException(
189+
"Cannot use subjectPrincipalRegex and extractPrincipalNameFromEmail together. "
190+
+ "Please use one or the other.");
191+
}
192+
SubjectX500PrincipalExtractor extractor = new SubjectX500PrincipalExtractor();
193+
extractor.setExtractPrincipalNameFromEmail(extractPrincipalNameFromEmail);
194+
this.x509PrincipalExtractor = extractor;
195+
return this;
196+
}
197+
172198
@Override
173199
public void init(H http) {
174200
PreAuthenticatedAuthenticationProvider authenticationProvider = new PreAuthenticatedAuthenticationProvider();

config/src/main/java/org/springframework/security/config/http/AuthenticationConfigBuilder.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 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.
@@ -57,6 +57,7 @@
5757
import org.springframework.security.web.authentication.preauth.j2ee.J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource;
5858
import org.springframework.security.web.authentication.preauth.j2ee.J2eePreAuthenticatedProcessingFilter;
5959
import org.springframework.security.web.authentication.preauth.x509.SubjectDnX509PrincipalExtractor;
60+
import org.springframework.security.web.authentication.preauth.x509.SubjectX500PrincipalExtractor;
6061
import org.springframework.security.web.authentication.preauth.x509.X509AuthenticationFilter;
6162
import org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter;
6263
import org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter;
@@ -522,12 +523,25 @@ void createX509Filter(BeanReference authManager,
522523
filterBuilder.addPropertyValue("securityContextHolderStrategy",
523524
authenticationFilterSecurityContextHolderStrategyRef);
524525
String regex = x509Elt.getAttribute("subject-principal-regex");
526+
String extractPrincipalNameFromEmail = x509Elt.getAttribute("extract-principal-name-from-email");
527+
if (StringUtils.hasText(regex) && StringUtils.hasText(extractPrincipalNameFromEmail)) {
528+
throw new IllegalStateException(
529+
"Cannot use subjectPrincipalRegex and extractPrincipalNameFromEmail together. "
530+
+ "Please use one or the other.");
531+
}
525532
if (StringUtils.hasText(regex)) {
526533
BeanDefinitionBuilder extractor = BeanDefinitionBuilder
527534
.rootBeanDefinition(SubjectDnX509PrincipalExtractor.class);
528535
extractor.addPropertyValue("subjectDnRegex", regex);
529536
filterBuilder.addPropertyValue("principalExtractor", extractor.getBeanDefinition());
530537
}
538+
if (StringUtils.hasText(extractPrincipalNameFromEmail)) {
539+
BeanDefinitionBuilder extractor = BeanDefinitionBuilder
540+
.rootBeanDefinition(SubjectX500PrincipalExtractor.class);
541+
extractor.addPropertyValue("extractPrincipalNameFromEmail",
542+
Boolean.parseBoolean(extractPrincipalNameFromEmail));
543+
filterBuilder.addPropertyValue("principalExtractor", extractor.getBeanDefinition());
544+
}
531545
injectAuthenticationDetailsSource(x509Elt, filterBuilder);
532546
filter = (RootBeanDefinition) filterBuilder.getBeanDefinition();
533547
createPrauthEntryPoint(x509Elt);

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
import org.springframework.security.oauth2.server.resource.web.server.authentication.ServerBearerTokenAuthenticationConverter;
120120
import org.springframework.security.web.PortMapper;
121121
import org.springframework.security.web.authentication.logout.LogoutHandler;
122-
import org.springframework.security.web.authentication.preauth.x509.SubjectDnX509PrincipalExtractor;
122+
import org.springframework.security.web.authentication.preauth.x509.SubjectX500PrincipalExtractor;
123123
import org.springframework.security.web.authentication.preauth.x509.X509PrincipalExtractor;
124124
import org.springframework.security.web.server.DefaultServerRedirectStrategy;
125125
import org.springframework.security.web.server.DelegatingServerAuthenticationEntryPoint;
@@ -943,8 +943,8 @@ public ServerHttpSecurity formLogin(Customizer<FormLoginSpec> formLoginCustomize
943943
* }
944944
* </pre>
945945
*
946-
* Note that if extractor is not specified, {@link SubjectDnX509PrincipalExtractor}
947-
* will be used. If authenticationManager is not specified,
946+
* Note that if extractor is not specified, {@link SubjectX500PrincipalExtractor} will
947+
* be used. If authenticationManager is not specified,
948948
* {@link ReactivePreAuthenticatedAuthenticationManager} will be used.
949949
* @return the {@link X509Spec} to customize
950950
* @since 5.2
@@ -978,8 +978,8 @@ public X509Spec x509() {
978978
* }
979979
* </pre>
980980
*
981-
* Note that if extractor is not specified, {@link SubjectDnX509PrincipalExtractor}
982-
* will be used. If authenticationManager is not specified,
981+
* Note that if extractor is not specified, {@link SubjectX500PrincipalExtractor} will
982+
* be used. If authenticationManager is not specified,
983983
* {@link ReactivePreAuthenticatedAuthenticationManager} will be used.
984984
* @param x509Customizer the {@link Customizer} to provide more options for the
985985
* {@link X509Spec}
@@ -4180,7 +4180,7 @@ private X509PrincipalExtractor getPrincipalExtractor() {
41804180
if (this.principalExtractor != null) {
41814181
return this.principalExtractor;
41824182
}
4183-
return new SubjectDnX509PrincipalExtractor();
4183+
return new SubjectX500PrincipalExtractor();
41844184
}
41854185

41864186
private ReactiveAuthenticationManager getAuthenticationManager() {

config/src/main/resources/org/springframework/security/config/spring-security-7.0.rnc

+3
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,9 @@ x509.attlist &=
10501050
x509.attlist &=
10511051
## Reference to an AuthenticationDetailsSource which will be used by the authentication filter
10521052
attribute authentication-details-source-ref {xsd:token}?
1053+
x509.attlist &=
1054+
## If true then DN will be extracted from EMAIlADDRESS
1055+
attribute extract-principal-name-from-email {xsd:token}?
10531056

10541057
jee =
10551058
## Adds a J2eePreAuthenticatedProcessingFilter to the filter chain to provide integration with container authentication.

config/src/main/resources/org/springframework/security/config/spring-security-7.0.xsd

+6
Original file line numberDiff line numberDiff line change
@@ -2911,6 +2911,12 @@
29112911
</xs:documentation>
29122912
</xs:annotation>
29132913
</xs:attribute>
2914+
<xs:attribute name="extract-principal-name-from-email" type="xs:token">
2915+
<xs:annotation>
2916+
<xs:documentation>If true then DN will be extracted from EMAIlADDRESS
2917+
</xs:documentation>
2918+
</xs:annotation>
2919+
</xs:attribute>
29142920
</xs:attributeGroup>
29152921
<xs:element name="jee">
29162922
<xs:annotation>

config/src/test/java/org/springframework/security/config/annotation/web/configurers/X509ConfigurerTests.java

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 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.
@@ -123,6 +123,16 @@ public void x509WhenSubjectPrincipalRegexInLambdaThenUsesRegexToExtractPrincipal
123123
// @formatter:on
124124
}
125125

126+
@Test
127+
public void x509WhenExtractPrincipalNameFromEmailIsTrueThenUsesEmailAddressToExtractPrincipal() throws Exception {
128+
this.spring.register(EmailPrincipalConfig.class).autowire();
129+
X509Certificate certificate = loadCert("max.cer");
130+
// @formatter:off
131+
this.mvc.perform(get("/").with(x509(certificate)))
132+
.andExpect(authenticated().withUsername("[email protected]"));
133+
// @formatter:on
134+
}
135+
126136
@Test
127137
public void x509WhenUserDetailsServiceNotConfiguredThenUsesBean() throws Exception {
128138
this.spring.register(UserDetailsServiceBeanConfig.class).autowire();
@@ -277,6 +287,33 @@ UserDetailsService userDetailsService() {
277287

278288
}
279289

290+
@Configuration
291+
@EnableWebSecurity
292+
static class EmailPrincipalConfig {
293+
294+
@Bean
295+
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
296+
// @formatter:off
297+
http
298+
.x509((x509) ->
299+
x509.extractPrincipalNameFromEmail(true)
300+
);
301+
// @formatter:on
302+
return http.build();
303+
}
304+
305+
@Bean
306+
UserDetailsService userDetailsService() {
307+
UserDetails user = User.withDefaultPasswordEncoder()
308+
.username("[email protected]")
309+
.password("password")
310+
.roles("USER", "ADMIN")
311+
.build();
312+
return new InMemoryUserDetailsManager(user);
313+
}
314+
315+
}
316+
280317
@Configuration
281318
@EnableWebSecurity
282319
static class UserDetailsServiceBeanConfig {

config/src/test/resources/max.cer

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIICojCCAgugAwIBAgIBADANBgkqhkiG9w0BAQ0FADBuMQswCQYDVQQGEwJydTEP
3+
MA0GA1UECAwGTW9zY293MQ8wDQYDVQQKDAZTcHJpbmcxFjAUBgNVBAMMDU1heCBC
4+
YXRpc2NoZXYxJTAjBgkqhkiG9w0BCQEWFm1heGJhdGlzY2hldkBnbWFpbC5jb20w
5+
HhcNMjUwNTE0MTcyODM5WhcNMjYwNTE0MTcyODM5WjBuMQswCQYDVQQGEwJydTEP
6+
MA0GA1UECAwGTW9zY293MQ8wDQYDVQQKDAZTcHJpbmcxFjAUBgNVBAMMDU1heCBC
7+
YXRpc2NoZXYxJTAjBgkqhkiG9w0BCQEWFm1heGJhdGlzY2hldkBnbWFpbC5jb20w
8+
gZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALVZ2K/iOINeHZ4XAV3QmNRgS+iB
9+
Vw0fW07uzYkCoSZU1lOBQE0k8+fdM2+X9AsgwfRCE3tUZquPApEKynB5V9Seh+bR
10+
vc9aj7PunMyN+zjRU6X7/BL3VqLfrJLSc15bQaSN1phJ6NT+BTXPTuiPbXldnJLC
11+
wVo6PView83yZ335AgMBAAGjUDBOMB0GA1UdDgQWBBQhyQfxL2ZYotcS8AmMJtli
12+
2IRAMTAfBgNVHSMEGDAWgBQhyQfxL2ZYotcS8AmMJtli2IRAMTAMBgNVHRMEBTAD
13+
AQH/MA0GCSqGSIb3DQEBDQUAA4GBAIIIJxpsTPtUEnePAqqgVFWDKC2CExhtCBYL
14+
MjLSC+7E9OlfuuX1joAsD4Yv86k4Ox836D0KQtINtg3y6D8O+HSylhVg1xtOiK7l
15+
ElXVRepB8GcX3vf9F58v9s++cSDvXf8vJu/O7nI4fv9C5SfUtMY4JPh/3MTsyl8O
16+
tgxTKjvO
17+
-----END CERTIFICATE-----

docs/modules/ROOT/pages/servlet/appendix/namespace/http.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,9 @@ Defines a regular expression which will be used to extract the username from the
22232223
Allows a specific `UserDetailsService` to be used with X.509 in the case where multiple instances are configured.
22242224
If not set, an attempt will be made to locate a suitable instance automatically and use that.
22252225

2226+
[[nsa-x509-extract-principal-name-from-email]]
2227+
* **extract-principal-name-from-email**
2228+
If true then DN will be extracted from EMAIlADDRESS.
22262229

22272230
[[nsa-filter-chain-map]]
22282231
== <filter-chain-map>

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

+5-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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.web.authentication.preauth.x509;
1818

19+
import java.security.Principal;
1920
import java.security.cert.X509Certificate;
2021
import java.util.regex.Matcher;
2122
import java.util.regex.Pattern;
@@ -43,7 +44,9 @@
4344
* "[email protected], CN=..." giving a user name "[email protected]"
4445
*
4546
* @author Luke Taylor
47+
* @deprecated Please use {@link SubjectX500PrincipalExtractor} instead
4648
*/
49+
@Deprecated
4750
public class SubjectDnX509PrincipalExtractor implements X509PrincipalExtractor, MessageSourceAware {
4851

4952
protected final Log logger = LogFactory.getLog(getClass());
@@ -59,6 +62,7 @@ public SubjectDnX509PrincipalExtractor() {
5962
@Override
6063
public Object extractPrincipal(X509Certificate clientCert) {
6164
// String subjectDN = clientCert.getSubjectX500Principal().getName();
65+
Principal principal = clientCert.getSubjectDN();
6266
String subjectDN = clientCert.getSubjectDN().getName();
6367
this.logger.debug(LogMessage.format("Subject DN is '%s'", subjectDN));
6468
Matcher matcher = this.subjectDnPattern.matcher(subjectDN);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2002-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.authentication.preauth.x509;
18+
19+
import java.security.cert.X509Certificate;
20+
import java.util.regex.Matcher;
21+
import java.util.regex.Pattern;
22+
23+
import javax.security.auth.x500.X500Principal;
24+
25+
import org.apache.commons.logging.Log;
26+
import org.apache.commons.logging.LogFactory;
27+
28+
import org.springframework.context.MessageSource;
29+
import org.springframework.context.MessageSourceAware;
30+
import org.springframework.context.support.MessageSourceAccessor;
31+
import org.springframework.core.log.LogMessage;
32+
import org.springframework.security.authentication.BadCredentialsException;
33+
import org.springframework.security.core.SpringSecurityMessageSource;
34+
import org.springframework.util.Assert;
35+
36+
/**
37+
* Obtains the principal from a certificate using RFC2253 and RFC1779 formats. By default,
38+
* RFC2253 is used: DN is extracted from CN. If extractPrincipalNameFromEmail is true then
39+
* format RFC1779 will be used: DN is extracted from EMAIlADDRESS.
40+
*
41+
* @author Max Batischev
42+
* @since 7.0
43+
*/
44+
public final class SubjectX500PrincipalExtractor implements X509PrincipalExtractor, MessageSourceAware {
45+
46+
private final Log logger = LogFactory.getLog(getClass());
47+
48+
private MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
49+
50+
private boolean extractPrincipalNameFromEmail = false;
51+
52+
private final Pattern cnSubjectDnPattern = Pattern.compile("CN=(.*?)(?:,|$)", Pattern.CASE_INSENSITIVE);
53+
54+
private final Pattern emailSubjectDnPattern = Pattern.compile("OID.1.2.840.113549.1.9.1=(.*?)(?:,|$)",
55+
Pattern.CASE_INSENSITIVE);
56+
57+
@Override
58+
public Object extractPrincipal(X509Certificate clientCert) {
59+
Assert.notNull(clientCert, "clientCert cannot be null");
60+
X500Principal principal = clientCert.getSubjectX500Principal();
61+
String subjectDN = this.extractPrincipalNameFromEmail ? principal.getName("RFC1779") : principal.getName();
62+
this.logger.debug(LogMessage.format("Subject DN is '%s'", subjectDN));
63+
Matcher matcher = this.extractPrincipalNameFromEmail ? this.emailSubjectDnPattern.matcher(subjectDN)
64+
: this.cnSubjectDnPattern.matcher(subjectDN);
65+
if (!matcher.find()) {
66+
throw new BadCredentialsException(this.messages.getMessage("SubjectX500PrincipalExtractor.noMatching",
67+
new Object[] { subjectDN }, "No matching pattern was found in subject DN: {0}"));
68+
}
69+
String principalName = matcher.group(1);
70+
this.logger.debug(LogMessage.format("Extracted Principal name is '%s'", principalName));
71+
return principalName;
72+
}
73+
74+
@Override
75+
public void setMessageSource(MessageSource messageSource) {
76+
Assert.notNull(messageSource, "messageSource cannot be null");
77+
this.messages = new MessageSourceAccessor(messageSource);
78+
}
79+
80+
/**
81+
* If true then DN will be extracted from EMAIlADDRESS, defaults to {@code false}
82+
* @param extractPrincipalNameFromEmail whether to extract DN from EMAIlADDRESS
83+
*/
84+
public void setExtractPrincipalNameFromEmail(boolean extractPrincipalNameFromEmail) {
85+
this.extractPrincipalNameFromEmail = extractPrincipalNameFromEmail;
86+
}
87+
88+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 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.
@@ -28,7 +28,7 @@
2828
*/
2929
public class X509AuthenticationFilter extends AbstractPreAuthenticatedProcessingFilter {
3030

31-
private X509PrincipalExtractor principalExtractor = new SubjectDnX509PrincipalExtractor();
31+
private X509PrincipalExtractor principalExtractor = new SubjectX500PrincipalExtractor();
3232

3333
@Override
3434
protected Object getPreAuthenticatedPrincipal(HttpServletRequest request) {

0 commit comments

Comments
 (0)