-
Notifications
You must be signed in to change notification settings - Fork 230
Auto-exclude conflicting Spring Boot security auto-configurations #1205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jdaugherty
merged 8 commits into
apache:7.0.x
from
jamesfredley:fix/auto-exclude-security-autoconfig
Feb 20, 2026
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a783a42
Auto-exclude conflicting Spring Boot security auto-configurations
jamesfredley db38e2c
feat: add opt-out property for auto-configuration exclusion filter
jamesfredley d9e4337
docs: update README with auto-exclusion opt-out instructions
jamesfredley 6588157
docs: add auto-configuration section to installation guide
jamesfredley 9656c94
test: add integration test and document excludeSpringSecurityAutoConf…
jamesfredley 2e17b1d
build: clean up and sort dependencies
matrei e2d33f9
style: groovify autoconfiguration exclusion logic
matrei b1341f5
test: clean up and more readable
matrei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...gin/src/main/groovy/grails/plugin/springsecurity/SecurityAutoConfigurationExcluder.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package grails.plugin.springsecurity | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter | ||
| import org.springframework.boot.autoconfigure.AutoConfigurationMetadata | ||
| import org.springframework.context.EnvironmentAware | ||
| import org.springframework.core.env.Environment | ||
|
|
||
| /** | ||
| * Automatically excludes Spring Boot security auto-configuration classes that | ||
| * conflict with the Grails Spring Security plugin. | ||
| * | ||
| * <p>When the Grails Spring Security plugin is on the classpath, Spring Boot's | ||
| * security auto-configurations (e.g. {@code SecurityAutoConfiguration}, | ||
| * {@code SecurityFilterAutoConfiguration}) create duplicate | ||
| * {@code SecurityFilterChain} beans and other security infrastructure that | ||
| * conflicts with the plugin's own bean definitions in | ||
| * {@link SpringSecurityCoreGrailsPlugin#doWithSpring}.</p> | ||
| * | ||
| * <p>Previously, users had to manually exclude up to 7 auto-configuration classes | ||
| * in {@code application.yml}. This filter removes that requirement by | ||
| * automatically filtering them out during Spring Boot's auto-configuration | ||
| * discovery phase.</p> | ||
| * | ||
| * <p>To disable this filter and allow Spring Boot's security auto-configurations | ||
| * to run, set the following property in {@code application.yml}:</p> | ||
| * | ||
| * <pre> | ||
| * grails: | ||
| * plugin: | ||
| * springsecurity: | ||
| * excludeSpringSecurityAutoConfiguration: false | ||
| * </pre> | ||
| * | ||
| * <p>Registered via {@code META-INF/spring.factories} as an | ||
| * {@link AutoConfigurationImportFilter}. This runs before auto-configuration | ||
| * bytecode is loaded, so there is no performance overhead from excluded classes.</p> | ||
| * | ||
| * @since 7.0.2 | ||
| * @see AutoConfigurationImportFilter | ||
| */ | ||
| @CompileStatic | ||
| class SecurityAutoConfigurationExcluder implements AutoConfigurationImportFilter, EnvironmentAware { | ||
|
|
||
| static final String ENABLED_PROPERTY = 'grails.plugin.springsecurity.excludeSpringSecurityAutoConfiguration' | ||
|
|
||
| private boolean enabled = true | ||
|
|
||
| @Override | ||
| void setEnvironment(Environment environment) { | ||
| this.enabled = environment.getProperty(ENABLED_PROPERTY, Boolean, true) | ||
jdaugherty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Spring Boot security auto-configuration classes that conflict with the | ||
| * Grails Spring Security plugin. These are excluded unconditionally when the | ||
| * plugin is on the classpath. | ||
| * | ||
| * <ul> | ||
| * <li>{@code SecurityAutoConfiguration} — creates a default {@code SecurityFilterChain} | ||
| * that conflicts with the plugin's {@code FilterChainProxy}</li> | ||
| * <li>{@code SecurityFilterAutoConfiguration} — registers a | ||
| * {@code DelegatingFilterProxyRegistrationBean} that duplicates the plugin's | ||
| * {@code springSecurityFilterChainRegistrationBean}</li> | ||
| * <li>{@code UserDetailsServiceAutoConfiguration} — creates an in-memory | ||
| * {@code UserDetailsService} that conflicts with the plugin's | ||
| * {@code GormUserDetailsService}</li> | ||
| * <li>{@code OAuth2ClientAutoConfiguration} ({@code ...oauth2.client.servlet}) — | ||
| * conflicts when the plugin-oauth2 module manages OAuth2 configuration</li> | ||
| * <li>{@code OAuth2ClientAutoConfiguration} ({@code ...oauth2.client}) — | ||
| * non-servlet variant of the above; also conflicts with plugin-oauth2</li> | ||
| * <li>{@code OAuth2ResourceServerAutoConfiguration} — conflicts with the | ||
| * plugin's resource server security setup</li> | ||
| * <li>{@code ManagementWebSecurityAutoConfiguration} — Actuator security | ||
| * that conflicts when Actuator is on the classpath</li> | ||
| * </ul> | ||
jamesfredley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| private static final Set<String> EXCLUDED_AUTO_CONFIGURATIONS = [ | ||
| 'org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration', | ||
| 'org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration', | ||
| 'org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration', | ||
| 'org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration', | ||
| 'org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientAutoConfiguration', | ||
| 'org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration', | ||
| 'org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration', | ||
| ] as Set<String> | ||
|
|
||
| @Override | ||
| boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) { | ||
| boolean[] matches = new boolean[autoConfigurationClasses.length] | ||
| for (int i = 0; i < autoConfigurationClasses.length; i++) { | ||
| matches[i] = !enabled || !EXCLUDED_AUTO_CONFIGURATIONS.contains(autoConfigurationClasses[i]) | ||
| } | ||
| return matches | ||
| } | ||
|
|
||
| /** | ||
| * Returns the set of auto-configuration class names that this filter excludes. | ||
| * Exposed for testing and diagnostic purposes. | ||
| * | ||
| * @return unmodifiable set of excluded class names | ||
| */ | ||
| static Set<String> getExcludedAutoConfigurations() { | ||
| return Collections.unmodifiableSet(EXCLUDED_AUTO_CONFIGURATIONS) | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
plugin-core/plugin/src/main/resources/META-INF/spring.factories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # Automatically exclude Spring Boot security auto-configurations that conflict | ||
| # with the Grails Spring Security plugin's bean definitions. | ||
| # See: SecurityAutoConfigurationExcluder javadoc for the full list and rationale. | ||
| org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\ | ||
| grails.plugin.springsecurity.SecurityAutoConfigurationExcluder |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.