Skip to content

Commit e6ace08

Browse files
committed
Migrate X509ConfigurerTests groovy->java
Issue: gh-4939
1 parent ac38232 commit e6ace08

File tree

2 files changed

+133
-60
lines changed

2 files changed

+133
-60
lines changed

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

Lines changed: 0 additions & 60 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2002-2019 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.config.annotation.web.configurers;
18+
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.core.io.ClassPathResource;
24+
import org.springframework.security.config.annotation.ObjectPostProcessor;
25+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
26+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
27+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
28+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
29+
import org.springframework.security.config.test.SpringTestRule;
30+
import org.springframework.security.web.authentication.preauth.x509.X509AuthenticationFilter;
31+
import org.springframework.test.web.servlet.MockMvc;
32+
33+
import java.io.InputStream;
34+
import java.security.cert.Certificate;
35+
import java.security.cert.CertificateFactory;
36+
import java.security.cert.X509Certificate;
37+
38+
import static org.mockito.ArgumentMatchers.any;
39+
import static org.mockito.Mockito.spy;
40+
import static org.mockito.Mockito.verify;
41+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.x509;
42+
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
43+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
44+
45+
/**
46+
* Tests for {@link X509Configurer}
47+
*
48+
* @author Rob Winch
49+
* @author Eleftheria Stein
50+
*/
51+
public class X509ConfigurerTests {
52+
53+
@Rule
54+
public final SpringTestRule spring = new SpringTestRule();
55+
56+
@Autowired
57+
MockMvc mvc;
58+
59+
@Test
60+
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnX509AuthenticationFilter() {
61+
this.spring.register(ObjectPostProcessorConfig.class).autowire();
62+
63+
verify(ObjectPostProcessorConfig.objectPostProcessor)
64+
.postProcess(any(X509AuthenticationFilter.class));
65+
}
66+
67+
@EnableWebSecurity
68+
static class ObjectPostProcessorConfig extends WebSecurityConfigurerAdapter {
69+
static ObjectPostProcessor<Object> objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
70+
71+
@Override
72+
protected void configure(HttpSecurity http) throws Exception {
73+
// @formatter:off
74+
http
75+
.x509();
76+
// @formatter:on
77+
}
78+
79+
@Bean
80+
static ObjectPostProcessor<Object> objectPostProcessor() {
81+
return objectPostProcessor;
82+
}
83+
}
84+
85+
static class ReflectingObjectPostProcessor implements ObjectPostProcessor<Object> {
86+
@Override
87+
public <O> O postProcess(O object) {
88+
return object;
89+
}
90+
}
91+
92+
@Test
93+
public void x509WhenInvokedTwiceThenUsesOriginalSubjectPrincipalRegex() throws Exception {
94+
this.spring.register(DuplicateDoesNotOverrideConfig.class).autowire();
95+
X509Certificate certificate = loadCert("rodatexampledotcom.cer");
96+
97+
this.mvc.perform(get("/")
98+
.with(x509(certificate)))
99+
.andExpect(authenticated().withUsername("rod"));
100+
}
101+
102+
@EnableWebSecurity
103+
static class DuplicateDoesNotOverrideConfig extends WebSecurityConfigurerAdapter {
104+
@Override
105+
protected void configure(HttpSecurity http) throws Exception {
106+
// @formatter:off
107+
http
108+
.x509()
109+
.subjectPrincipalRegex("CN=(.*?)@example.com(?:,|$)")
110+
.and()
111+
.x509();
112+
// @formatter:on
113+
}
114+
115+
@Override
116+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
117+
// @formatter:off
118+
auth
119+
.inMemoryAuthentication()
120+
.withUser("rod").password("password").roles("USER", "ADMIN");
121+
// @formatter:on
122+
}
123+
}
124+
125+
private <T extends Certificate> T loadCert(String location) {
126+
try (InputStream is = new ClassPathResource(location).getInputStream()) {
127+
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
128+
return (T) certFactory.generateCertificate(is);
129+
} catch (Exception e) {
130+
throw new IllegalArgumentException(e);
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)