Skip to content

Commit d285c6a

Browse files
eleftheriasrwinch
authored andcommitted
Migrate JeeConfigurerTests groovy->java
Issue: gh-4939
1 parent 8e6db95 commit d285c6a

File tree

2 files changed

+128
-68
lines changed

2 files changed

+128
-68
lines changed

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

Lines changed: 0 additions & 68 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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.security.config.annotation.ObjectPostProcessor;
24+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
25+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
26+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
27+
import org.springframework.security.config.test.SpringTestRule;
28+
import org.springframework.security.web.authentication.preauth.j2ee.J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource;
29+
import org.springframework.security.web.authentication.preauth.j2ee.J2eePreAuthenticatedProcessingFilter;
30+
import org.springframework.test.web.servlet.MockMvc;
31+
32+
import java.security.Principal;
33+
34+
import static org.mockito.ArgumentMatchers.any;
35+
import static org.mockito.Mockito.mock;
36+
import static org.mockito.Mockito.spy;
37+
import static org.mockito.Mockito.verify;
38+
import static org.mockito.Mockito.when;
39+
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
40+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
41+
42+
/**
43+
* Tests for {@link JeeConfigurer}
44+
*
45+
* @author Rob Winch
46+
* @author Eleftheria Stein
47+
*/
48+
public class JeeConfigurerTests {
49+
50+
@Rule
51+
public final SpringTestRule spring = new SpringTestRule();
52+
53+
@Autowired
54+
MockMvc mvc;
55+
56+
@Test
57+
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnJ2eePreAuthenticatedProcessingFilter() {
58+
ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
59+
this.spring.register(ObjectPostProcessorConfig.class).autowire();
60+
61+
verify(ObjectPostProcessorConfig.objectPostProcessor)
62+
.postProcess(any(J2eePreAuthenticatedProcessingFilter.class));
63+
}
64+
65+
@Test
66+
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnJ2eeBasedPreAuthenticatedWebAuthenticationDetailsSource() {
67+
ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
68+
this.spring.register(ObjectPostProcessorConfig.class).autowire();
69+
70+
verify(ObjectPostProcessorConfig.objectPostProcessor)
71+
.postProcess(any(J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.class));
72+
}
73+
74+
@EnableWebSecurity
75+
static class ObjectPostProcessorConfig extends WebSecurityConfigurerAdapter {
76+
static ObjectPostProcessor<Object> objectPostProcessor;
77+
78+
@Override
79+
protected void configure(HttpSecurity http) throws Exception {
80+
// @formatter:off
81+
http
82+
.jee();
83+
// @formatter:on
84+
}
85+
86+
@Bean
87+
static ObjectPostProcessor<Object> objectPostProcessor() {
88+
return objectPostProcessor;
89+
}
90+
}
91+
92+
static class ReflectingObjectPostProcessor implements ObjectPostProcessor<Object> {
93+
@Override
94+
public <O> O postProcess(O object) {
95+
return object;
96+
}
97+
}
98+
99+
@Test
100+
public void jeeWhenInvokedTwiceThenUsesOriginalMappableRoles() throws Exception {
101+
this.spring.register(InvokeTwiceDoesNotOverride.class).autowire();
102+
Principal user = mock(Principal.class);
103+
when(user.getName()).thenReturn("user");
104+
105+
this.mvc.perform(get("/")
106+
.principal(user)
107+
.with(request -> {
108+
request.addUserRole("ROLE_ADMIN");
109+
request.addUserRole("ROLE_USER");
110+
return request;
111+
}))
112+
.andExpect(authenticated().withRoles("USER"));
113+
}
114+
115+
@EnableWebSecurity
116+
static class InvokeTwiceDoesNotOverride extends WebSecurityConfigurerAdapter {
117+
@Override
118+
protected void configure(HttpSecurity http) throws Exception {
119+
// @formatter:off
120+
http
121+
.jee()
122+
.mappableRoles("USER")
123+
.and()
124+
.jee();
125+
// @formatter:on
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)