|
38 | 38 | import static org.mockito.ArgumentMatchers.any;
|
39 | 39 | import static org.mockito.Mockito.spy;
|
40 | 40 | import static org.mockito.Mockito.verify;
|
| 41 | +import static org.springframework.security.config.Customizer.withDefaults; |
41 | 42 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.x509;
|
42 | 43 | import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
|
43 | 44 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
@@ -122,6 +123,69 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
122 | 123 | }
|
123 | 124 | }
|
124 | 125 |
|
| 126 | + @Test |
| 127 | + public void x509WhenConfiguredInLambdaThenUsesDefaults() throws Exception { |
| 128 | + this.spring.register(DefaultsInLambdaConfig.class).autowire(); |
| 129 | + X509Certificate certificate = loadCert("rod.cer"); |
| 130 | + |
| 131 | + this.mvc.perform(get("/") |
| 132 | + .with(x509(certificate))) |
| 133 | + .andExpect(authenticated().withUsername("rod")); |
| 134 | + } |
| 135 | + |
| 136 | + @EnableWebSecurity |
| 137 | + static class DefaultsInLambdaConfig extends WebSecurityConfigurerAdapter { |
| 138 | + @Override |
| 139 | + protected void configure(HttpSecurity http) throws Exception { |
| 140 | + // @formatter:off |
| 141 | + http |
| 142 | + .x509(withDefaults()); |
| 143 | + // @formatter:on |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 148 | + // @formatter:off |
| 149 | + auth |
| 150 | + .inMemoryAuthentication() |
| 151 | + .withUser("rod").password("password").roles("USER", "ADMIN"); |
| 152 | + // @formatter:on |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void x509WhenSubjectPrincipalRegexInLambdaThenUsesRegexToExtractPrincipal() throws Exception { |
| 158 | + this.spring.register(SubjectPrincipalRegexInLambdaConfig.class).autowire(); |
| 159 | + X509Certificate certificate = loadCert("rodatexampledotcom.cer"); |
| 160 | + |
| 161 | + this.mvc.perform(get("/") |
| 162 | + .with(x509(certificate))) |
| 163 | + .andExpect(authenticated().withUsername("rod")); |
| 164 | + } |
| 165 | + |
| 166 | + @EnableWebSecurity |
| 167 | + static class SubjectPrincipalRegexInLambdaConfig extends WebSecurityConfigurerAdapter { |
| 168 | + @Override |
| 169 | + protected void configure(HttpSecurity http) throws Exception { |
| 170 | + // @formatter:off |
| 171 | + http |
| 172 | + .x509(x509 -> |
| 173 | + x509 |
| 174 | + .subjectPrincipalRegex("CN=(.*?)@example.com(?:,|$)") |
| 175 | + ); |
| 176 | + // @formatter:on |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 181 | + // @formatter:off |
| 182 | + auth |
| 183 | + .inMemoryAuthentication() |
| 184 | + .withUser("rod").password("password").roles("USER", "ADMIN"); |
| 185 | + // @formatter:on |
| 186 | + } |
| 187 | + } |
| 188 | + |
125 | 189 | private <T extends Certificate> T loadCert(String location) {
|
126 | 190 | try (InputStream is = new ClassPathResource(location).getInputStream()) {
|
127 | 191 | CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
|
|
0 commit comments