|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package grails.plugin.springsecurity |
| 20 | + |
| 21 | +import spock.lang.Specification |
| 22 | +import spock.lang.Subject |
| 23 | +import spock.lang.Unroll |
| 24 | + |
| 25 | +/** |
| 26 | + * Tests for {@link SecurityAutoConfigurationExcluder}. |
| 27 | + * |
| 28 | + * Verifies that Spring Boot security auto-configuration classes that conflict |
| 29 | + * with the Grails Spring Security plugin are filtered out during the |
| 30 | + * auto-configuration discovery phase. |
| 31 | + */ |
| 32 | +class SecurityAutoConfigurationExcluderSpec extends Specification { |
| 33 | + |
| 34 | + @Subject |
| 35 | + SecurityAutoConfigurationExcluder excluder = new SecurityAutoConfigurationExcluder() |
| 36 | + |
| 37 | + @Unroll |
| 38 | + def "match excludes conflicting auto-configuration: #className"() { |
| 39 | + given: |
| 40 | + String[] autoConfigs = [className] as String[] |
| 41 | + |
| 42 | + when: |
| 43 | + boolean[] results = excluder.match(autoConfigs, null) |
| 44 | + |
| 45 | + then: 'the conflicting auto-configuration is excluded (false = filtered out)' |
| 46 | + !results[0] |
| 47 | + |
| 48 | + where: |
| 49 | + className << [ |
| 50 | + 'org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration', |
| 51 | + 'org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration', |
| 52 | + 'org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration', |
| 53 | + 'org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration', |
| 54 | + 'org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientAutoConfiguration', |
| 55 | + 'org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration', |
| 56 | + 'org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration', |
| 57 | + ] |
| 58 | + } |
| 59 | + |
| 60 | + @Unroll |
| 61 | + def "match preserves non-security auto-configuration: #className"() { |
| 62 | + given: |
| 63 | + String[] autoConfigs = [className] as String[] |
| 64 | + |
| 65 | + when: |
| 66 | + boolean[] results = excluder.match(autoConfigs, null) |
| 67 | + |
| 68 | + then: 'non-security auto-configurations pass through (true = included)' |
| 69 | + results[0] |
| 70 | + |
| 71 | + where: |
| 72 | + className << [ |
| 73 | + 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration', |
| 74 | + 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration', |
| 75 | + 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration', |
| 76 | + 'org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration', |
| 77 | + 'org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration', |
| 78 | + ] |
| 79 | + } |
| 80 | + |
| 81 | + def "match handles mixed array of included and excluded auto-configurations"() { |
| 82 | + given: |
| 83 | + String[] autoConfigs = [ |
| 84 | + 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration', |
| 85 | + 'org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration', |
| 86 | + 'org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration', |
| 87 | + 'org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration', |
| 88 | + 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration', |
| 89 | + ] as String[] |
| 90 | + |
| 91 | + when: |
| 92 | + boolean[] results = excluder.match(autoConfigs, null) |
| 93 | + |
| 94 | + then: |
| 95 | + results[0] // DataSource — included |
| 96 | + !results[1] // SecurityAutoConfiguration — excluded |
| 97 | + results[2] // Jackson — included |
| 98 | + !results[3] // SecurityFilterAutoConfiguration — excluded |
| 99 | + results[4] // DispatcherServlet — included |
| 100 | + } |
| 101 | + |
| 102 | + def "match handles empty array"() { |
| 103 | + given: |
| 104 | + String[] autoConfigs = [] as String[] |
| 105 | + |
| 106 | + when: |
| 107 | + boolean[] results = excluder.match(autoConfigs, null) |
| 108 | + |
| 109 | + then: |
| 110 | + results.length == 0 |
| 111 | + } |
| 112 | + |
| 113 | + def "match handles null metadata parameter gracefully"() { |
| 114 | + given: 'autoConfigurationMetadata is null (not used by this filter)' |
| 115 | + String[] autoConfigs = [ |
| 116 | + 'org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration', |
| 117 | + ] as String[] |
| 118 | + |
| 119 | + when: |
| 120 | + boolean[] results = excluder.match(autoConfigs, null) |
| 121 | + |
| 122 | + then: 'still works correctly' |
| 123 | + !results[0] |
| 124 | + } |
| 125 | + |
| 126 | + def "getExcludedAutoConfigurations returns all 7 known conflicting classes"() { |
| 127 | + when: |
| 128 | + Set<String> excluded = SecurityAutoConfigurationExcluder.excludedAutoConfigurations |
| 129 | + |
| 130 | + then: |
| 131 | + excluded.size() == 7 |
| 132 | + excluded.contains('org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration') |
| 133 | + excluded.contains('org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration') |
| 134 | + excluded.contains('org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration') |
| 135 | + excluded.contains('org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration') |
| 136 | + excluded.contains('org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientAutoConfiguration') |
| 137 | + excluded.contains('org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration') |
| 138 | + excluded.contains('org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration') |
| 139 | + } |
| 140 | + |
| 141 | + def "getExcludedAutoConfigurations returns unmodifiable set"() { |
| 142 | + when: |
| 143 | + Set<String> excluded = SecurityAutoConfigurationExcluder.excludedAutoConfigurations |
| 144 | + excluded.add('some.new.AutoConfiguration') |
| 145 | + |
| 146 | + then: |
| 147 | + thrown(UnsupportedOperationException) |
| 148 | + } |
| 149 | + |
| 150 | + def "spring.factories registers the filter correctly"() { |
| 151 | + when: 'reading the spring.factories resource from the classpath' |
| 152 | + URL resource = getClass().getClassLoader().getResource('META-INF/spring.factories') |
| 153 | + |
| 154 | + then: 'the resource exists' |
| 155 | + resource != null |
| 156 | + |
| 157 | + when: |
| 158 | + String content = resource.text |
| 159 | + |
| 160 | + then: 'it registers SecurityAutoConfigurationExcluder as an AutoConfigurationImportFilter' |
| 161 | + content.contains('org.springframework.boot.autoconfigure.AutoConfigurationImportFilter') |
| 162 | + content.contains('grails.plugin.springsecurity.SecurityAutoConfigurationExcluder') |
| 163 | + } |
| 164 | +} |
0 commit comments