|
| 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 | + * 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.config.annotation.web.configurers; |
| 17 | + |
| 18 | +import ch.qos.logback.classic.Level; |
| 19 | +import ch.qos.logback.classic.Logger; |
| 20 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 21 | +import ch.qos.logback.core.Appender; |
| 22 | +import org.junit.Rule; |
| 23 | +import org.junit.Test; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 28 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 29 | +import org.springframework.security.config.test.SpringTestRule; |
| 30 | +import org.springframework.security.web.debug.DebugFilter; |
| 31 | +import org.springframework.test.web.servlet.MockMvc; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.mockito.ArgumentMatchers.any; |
| 35 | +import static org.mockito.Mockito.atLeastOnce; |
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | +import static org.mockito.Mockito.never; |
| 38 | +import static org.mockito.Mockito.verify; |
| 39 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests to verify {@code EnableWebSecurity(debug)} functionality |
| 43 | + * |
| 44 | + * @author Rob Winch |
| 45 | + * @author Josh Cummings |
| 46 | + */ |
| 47 | +public class NamespaceDebugTests { |
| 48 | + @Rule |
| 49 | + public final SpringTestRule spring = new SpringTestRule(); |
| 50 | + |
| 51 | + @Autowired |
| 52 | + MockMvc mvc; |
| 53 | + |
| 54 | + @Test |
| 55 | + public void requestWhenDebugSetToTrueThenLogsDebugInformation() throws Exception { |
| 56 | + Appender<ILoggingEvent> appender = mockAppenderFor("Spring Security Debugger"); |
| 57 | + this.spring.register(DebugWebSecurity.class).autowire(); |
| 58 | + this.mvc.perform(get("/")); |
| 59 | + assertThat(filterChainClass()).isEqualTo(DebugFilter.class); |
| 60 | + verify(appender, atLeastOnce()).doAppend(any(ILoggingEvent.class)); |
| 61 | + } |
| 62 | + |
| 63 | + @EnableWebSecurity(debug=true) |
| 64 | + static class DebugWebSecurity extends WebSecurityConfigurerAdapter { |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void requestWhenDebugSetToFalseThenDoesNotLogDebugInformation() throws Exception { |
| 69 | + Appender<ILoggingEvent> appender = mockAppenderFor("Spring Security Debugger"); |
| 70 | + this.spring.register(NoDebugWebSecurity.class).autowire(); |
| 71 | + this.mvc.perform(get("/")); |
| 72 | + assertThat(filterChainClass()).isNotEqualTo(DebugFilter.class); |
| 73 | + verify(appender, never()).doAppend(any(ILoggingEvent.class)); |
| 74 | + } |
| 75 | + |
| 76 | + @EnableWebSecurity |
| 77 | + static class NoDebugWebSecurity extends WebSecurityConfigurerAdapter { |
| 78 | + } |
| 79 | + |
| 80 | + private Appender<ILoggingEvent> mockAppenderFor(String name) { |
| 81 | + Appender<ILoggingEvent> appender = mock(Appender.class); |
| 82 | + Logger logger = (Logger) LoggerFactory.getLogger(name); |
| 83 | + logger.setLevel(Level.DEBUG); |
| 84 | + logger.addAppender(appender); |
| 85 | + return appender; |
| 86 | + } |
| 87 | + |
| 88 | + private Class<?> filterChainClass() { |
| 89 | + return this.spring.getContext().getBean("springSecurityFilterChain").getClass(); |
| 90 | + } |
| 91 | +} |
0 commit comments