Search code examples
javaspringspring-bootspring-securityoauth-2.0

SpringBoot OAUTH2 always redirecting to default redirecturi


In my SprintBoot OAuth2 application all the endpoints are redirecting to default redirectUri.

Example:

http://localhost:8080/welcome > welcome
http://localhost:8080/secure-welcome > redirect to github > success > welcome (Secure Welcome is expected)
http://localhost:8080/secure-welcome2 > already authenticated/authorized > welcome (Secure Welcome 2 is expected)

@SpringBootApplication
@RestController
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @GetMapping(value = "/welcome")
    public String welcome() {
        return "Welcome";
    }

    @GetMapping(value = "/secure-welcome")
    public String secureWelcome() {
        return "Secure Welcome";
    }

    @GetMapping(value = "/secure-welcome2")
    public String secureWelcome2() {
        return "Secure Welcome 2";
    }
}
@Configuration
public class Config extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/oauth2/authorization/**", "/welcome").permitAll()
                .antMatchers("/secure**").authenticated()
                .and().oauth2Login();
    }
}

Properties File:

spring.security.oauth2.client.registration.github.client-id=<client id>
spring.security.oauth2.client.registration.github.client-secret=<client secret>
spring.security.oauth2.client.registration.github.redirect-uri=http://localhost:8080/welcome
spring.security.oauth2.client.registration.github.authorization-grant-type=authorization_code

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.testing</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

GitHub app registration:

enter image description here


Solution

  • There were couple of issues:

    1. There is no issue with the code but GitHub registration.

    The RedirectURI must be http://localhost:8080/login/oauth2/code/github

    1. As I was under proxy, the above URI didn't work and I ended up using wrong values.

    So in order to make this situation right we need to pass the below VM args accordingly.

    -Dhttp.proxyHost={your proxy}
    -Dhttp.proxyPort={your port number}
    -Dhttps.proxyHost=your proxy}
    -Dhttps.proxyPort={your port number}
    -Dhttp.nonProxyHosts=localhost|127.0.0.1
    

    This made the scenario work.