Skip to content

Commit c4dd800

Browse files
authored
Merge pull request #6944 from eleftherias/gh-4939-channel-security-groovy-to-java
Migrate ChannelSecurityConfigurerTests groovy->java
2 parents c43e632 + 4f042a4 commit c4dd800

File tree

2 files changed

+138
-78
lines changed

2 files changed

+138
-78
lines changed

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

Lines changed: 0 additions & 78 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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.access.channel.ChannelDecisionManagerImpl;
29+
import org.springframework.security.web.access.channel.ChannelProcessingFilter;
30+
import org.springframework.security.web.access.channel.InsecureChannelProcessor;
31+
import org.springframework.security.web.access.channel.SecureChannelProcessor;
32+
import org.springframework.test.web.servlet.MockMvc;
33+
34+
import static org.mockito.ArgumentMatchers.any;
35+
import static org.mockito.Mockito.spy;
36+
import static org.mockito.Mockito.verify;
37+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
38+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
39+
40+
/**
41+
* Tests for {@link ChannelSecurityConfigurer}
42+
*
43+
* @author Rob Winch
44+
* @author Eleftheria Stein
45+
*/
46+
public class ChannelSecurityConfigurerTests {
47+
48+
@Rule
49+
public final SpringTestRule spring = new SpringTestRule();
50+
51+
@Autowired
52+
MockMvc mvc;
53+
54+
@Test
55+
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnInsecureChannelProcessor() {
56+
ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
57+
this.spring.register(ObjectPostProcessorConfig.class).autowire();
58+
59+
verify(ObjectPostProcessorConfig.objectPostProcessor)
60+
.postProcess(any(InsecureChannelProcessor.class));
61+
}
62+
63+
@Test
64+
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnSecureChannelProcessor() {
65+
ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
66+
this.spring.register(ObjectPostProcessorConfig.class).autowire();
67+
68+
verify(ObjectPostProcessorConfig.objectPostProcessor)
69+
.postProcess(any(SecureChannelProcessor.class));
70+
}
71+
72+
@Test
73+
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnChannelDecisionManagerImpl() {
74+
ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
75+
this.spring.register(ObjectPostProcessorConfig.class).autowire();
76+
77+
verify(ObjectPostProcessorConfig.objectPostProcessor)
78+
.postProcess(any(ChannelDecisionManagerImpl.class));
79+
}
80+
81+
@Test
82+
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnChannelProcessingFilter() {
83+
ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
84+
this.spring.register(ObjectPostProcessorConfig.class).autowire();
85+
86+
verify(ObjectPostProcessorConfig.objectPostProcessor)
87+
.postProcess(any(ChannelProcessingFilter.class));
88+
}
89+
90+
@EnableWebSecurity
91+
static class ObjectPostProcessorConfig extends WebSecurityConfigurerAdapter {
92+
static ObjectPostProcessor<Object> objectPostProcessor;
93+
94+
@Override
95+
protected void configure(HttpSecurity http) throws Exception {
96+
// @formatter:off
97+
http
98+
.requiresChannel()
99+
.anyRequest().requiresSecure();
100+
// @formatter:on
101+
}
102+
103+
@Bean
104+
static ObjectPostProcessor<Object> objectPostProcessor() {
105+
return objectPostProcessor;
106+
}
107+
}
108+
109+
static class ReflectingObjectPostProcessor implements ObjectPostProcessor<Object> {
110+
@Override
111+
public <O> O postProcess(O object) {
112+
return object;
113+
}
114+
}
115+
116+
@Test
117+
public void requiresChannelWhenInvokesTwiceThenUsesOriginalRequiresSecure() throws Exception {
118+
this.spring.register(DuplicateInvocationsDoesNotOverrideConfig.class).autowire();
119+
120+
mvc.perform(get("/"))
121+
.andExpect(redirectedUrl("https://localhost/"));
122+
}
123+
124+
@EnableWebSecurity
125+
static class DuplicateInvocationsDoesNotOverrideConfig extends WebSecurityConfigurerAdapter {
126+
127+
@Override
128+
protected void configure(HttpSecurity http) throws Exception {
129+
// @formatter:off
130+
http
131+
.requiresChannel()
132+
.anyRequest().requiresSecure()
133+
.and()
134+
.requiresChannel();
135+
// @formatter:on
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)