Skip to content

Commit e4fa42c

Browse files
author
Rob Winch
committed
SEC-2593: Add test for TestSecurityContext RPP in stateless mode
1 parent 7e3e821 commit e4fa42c

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2002-2014 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+
* http://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+
package org.springframework.security.test.web.servlet.request;
17+
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
24+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
25+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
26+
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
27+
import org.springframework.security.config.http.SessionCreationPolicy;
28+
import org.springframework.security.test.context.DefaultSecurityTestExecutionListeners;
29+
import org.springframework.security.test.context.support.WithMockUser;
30+
import org.springframework.test.context.ContextConfiguration;
31+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32+
import org.springframework.test.context.web.WebAppConfiguration;
33+
import org.springframework.test.web.servlet.MockMvc;
34+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
35+
import org.springframework.web.bind.annotation.RequestMapping;
36+
import org.springframework.web.bind.annotation.RestController;
37+
import org.springframework.web.context.WebApplicationContext;
38+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
39+
40+
import javax.servlet.Filter;
41+
42+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.testSecurityContext;
43+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
44+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
45+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
46+
47+
@RunWith(SpringJUnit4ClassRunner.class)
48+
@ContextConfiguration
49+
@WebAppConfiguration
50+
@DefaultSecurityTestExecutionListeners
51+
public class SecurityMockMvcRequestPostProcessorsTestSecurityContextStatelessTests {
52+
53+
@Autowired
54+
private WebApplicationContext context;
55+
56+
@Autowired
57+
private Filter springSecurityFilterChain;
58+
59+
private MockMvc mvc;
60+
61+
@Before
62+
public void setup() {
63+
mvc = MockMvcBuilders
64+
.webAppContextSetup(context)
65+
.addFilters(springSecurityFilterChain)
66+
.defaultRequest(get("/").with(testSecurityContext()))
67+
.build();
68+
}
69+
70+
@Test
71+
@WithMockUser
72+
public void testSecurityContextWithMockUserWorksWithStateless() throws Exception {
73+
mvc
74+
.perform(get("/"))
75+
.andExpect(status().is2xxSuccessful());
76+
}
77+
78+
@Configuration
79+
@EnableWebMvcSecurity
80+
@EnableWebMvc
81+
static class Config extends WebSecurityConfigurerAdapter {
82+
83+
@Override
84+
protected void configure(HttpSecurity http) throws Exception {
85+
super.configure(http);
86+
87+
http
88+
.sessionManagement()
89+
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
90+
}
91+
92+
@Autowired
93+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
94+
auth
95+
.inMemoryAuthentication();
96+
}
97+
98+
@RestController
99+
static class Controller {
100+
@RequestMapping
101+
public String hello() {
102+
return "Hello";
103+
}
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)