I have a spring-boot service that authenticates users with Okta Platform API using OpenID Connect/OAuth2. When users try to access my service, they are redirected to Okta sign-on page and authenticated, then Okta redirects them back to my service.
Here is relevant part of my pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Here is my controller code:
@RestController
@EnableOAuth2Sso
@SpringBootApplication
public class Application {
@RequestMapping(path = "/", method = RequestMethod.GET)
public String home(Authentication auth) {
return "Home: " + auth.getName();
}
@RequestMapping(path = "/app", method = RequestMethod.POST)
public String app(Authentication auth) {
return "App: " + auth.getName();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
This works perfectly for the first GET controller method but for the second POST method my service requires me to provide CSRF token. I want to disable CSRF check entirely, so I added this to my app
@Configuration
@EnableOAuth2Sso
public class Config extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
However, after adding the above configuration my service stopped authenticating users with Okta (is no longer redirecting unauthenticated requests to Okta). It's directly calling home() method with null parameter.
I followed this blog post to create my service https://developer.okta.com/blog/2017/03/21/spring-boot-oauth
How can I disable CSRF entirely while still using OAuth2 SSO authentication with Okta?