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



Read more here: https://stackoverflow.com/questions/66320787/springboot-oauth2-always-redirecting-to-default-redirecturi

Content Attribution

This content was originally published by Manoj Reddy Mettu at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: