|
| 1 | +/* |
| 2 | + * Copyright 2004-present 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.docs.servlet.addingcustomfilter; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.context.annotation.Configuration; |
| 26 | +import org.springframework.security.access.AccessDeniedException; |
| 27 | +import org.springframework.security.core.userdetails.User; |
| 28 | +import org.springframework.security.core.userdetails.UserDetails; |
| 29 | +import org.springframework.security.core.userdetails.UserDetailsService; |
| 30 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 31 | +import org.springframework.test.context.ContextConfiguration; |
| 32 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 33 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 34 | +import org.springframework.test.web.servlet.MockMvc; |
| 35 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 36 | +import org.springframework.web.context.WebApplicationContext; |
| 37 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 38 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; |
| 39 | +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; |
| 40 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 41 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 42 | +import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated; |
| 43 | +import org.springframework.web.bind.annotation.GetMapping; |
| 44 | +import org.springframework.web.bind.annotation.RestController; |
| 45 | + |
| 46 | +@ExtendWith(SpringExtension.class) |
| 47 | +@ContextConfiguration(classes = { |
| 48 | + CustomFilterAfterTests.UserDetailsConfig.class, |
| 49 | + CustomFilterAfterTests.ApiController.class, |
| 50 | + SecurityConfigAfter.class }) |
| 51 | +@WebAppConfiguration |
| 52 | +public class CustomFilterAfterTests { |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private WebApplicationContext context; |
| 56 | + |
| 57 | + private MockMvc mvc; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void setup() { |
| 61 | + this.mvc = MockMvcBuilders.webAppContextSetup(this.context) |
| 62 | + .defaultRequest(get("/api").with(user("user"))) |
| 63 | + .apply(springSecurity()) |
| 64 | + .build(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void tenantFilterWhenHeaderMissingThenAccessDenied() { |
| 69 | + assertThatExceptionOfType(AccessDeniedException.class) |
| 70 | + .isThrownBy(() -> this.mvc.perform(get("/api")).andReturn()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void tenantFilterWhenHeaderPresentThenContinuesFilterChain() throws Exception { |
| 75 | + this.mvc.perform(get("/api").header("X-Tenant-Id", "some-tenant-id")) |
| 76 | + .andExpect(status().isOk()) |
| 77 | + .andExpect(authenticated().withUsername("user")); |
| 78 | + } |
| 79 | + |
| 80 | + @Configuration |
| 81 | + static class UserDetailsConfig { |
| 82 | + @Bean |
| 83 | + UserDetailsService userDetailsService() { |
| 84 | + UserDetails user = User.withDefaultPasswordEncoder() |
| 85 | + .username("user") |
| 86 | + .password("password") |
| 87 | + .roles("USER") |
| 88 | + .build(); |
| 89 | + return new InMemoryUserDetailsManager(user); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @RestController |
| 94 | + static class ApiController { |
| 95 | + |
| 96 | + @GetMapping("/api") |
| 97 | + String api() { |
| 98 | + return "ok"; |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | +} |
0 commit comments