apache / apache/grails-core

How to wire in custom WebSecurityConfigurer in grails 5

Open
#15,891 8 comments 2 reactions 0 assignees View on GitHub
relates-to:spring-security
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

### Issue description

I decided to post here, as I am not getting anything from the [Stackoverflow post](https://stackoverflow.com/questions/70861769/how-to-wire-a-custom-websecurityconfigureradapter-in-grails-5).

As part of moving our environments to Kubernetes, we are using Ambassador as the ingress service.
The app itself (the API side) is using Grails spring-security plugin. Having these two together, the preflight requests are not passing as the implementation will deny these requests. (see: https://www.getambassador.io/docs/edge-stack/latest/topics/using/cors/#authservice-and-cross-origin-resource-sharing).

I was digging for a solution for quite some time now, and the one that stands out in many places is to create a custom WebSecurityConfigurer (also, as suggested by Ambassador).

We I created the following:

```
package priz.api.security

import grails.compiler.GrailsCompileStatic
import org.springframework.boot.autoconfigure.security.SecurityProperties
import org.springframework.context.annotation.Configuration
import org.springframework.core.annotation.Order
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.CorsConfigurationSource

import javax.servlet.http.HttpServletRequest

@GrailsCompileStatic
@Configuration
@EnableWebSecurity
@Order(SecurityProperties.DEFAULT_FILTER_ORDER)
class SecurityConfig extends WebSecurityConfigurerAdapter {

public void configure(final HttpSecurity http) throws Exception {
http
.cors().configurationSource(new PermissiveCorsConfigurationSource()).and()
.csrf().disable()
.authorizeRequests()
.antMatchers("**").permitAll();
}

private static class PermissiveCorsConfigurationSource implements CorsConfigurationSource {
/**
* Return a {@link CorsConfiguration} based on the incoming request.
*
* @param request
* @return the associated {@link CorsConfiguration}, or {@code null} if none
*/
@Override
public CorsConfiguration getCorsConfiguration(final HttpServletRequest request) {
final CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(Collections.singletonList("*"));
configuration.setAllowedMethods(Collections.singletonList("*"));
configuration.setAllowedOrigins(Collections.singletonList("*"));
return configuration;
}
}
}
```

However, this config is not getting picked up. Is there anything additional that I have to do to register it? My expectation was that the annotations are taking care of that.
Also tried `@EnableGlobalMethodSecurity` instead, the same result.

Thanks for the advice.

I could also create an interceptor, but Grails interceptors cannot intercept the endpoints provided by the Spring Security core/rest plugins since the priority of their interceptors are higher than that of Grails interceptors

Contributor guide

Open the contributing guide

Research direction

Start with the SecurityConfig example in the issue and trace Grails 5's Spring Security plugin configuration and application-context discovery. Compare the behavior with the linked Stack Overflow scenario and the Ambassador CORS requirements. Done means establishing why the configuration is not applied and documenting or implementing a verified registration path.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, kubernetes, spring
Domain
api, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.