Using OAuth2 in Spring Boot App with {okta}




  • Add the following dependencies in POM file


<dependency>
    <groupId>com.okta.spring</groupId>
    <artifactId>okta-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>


        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>


  • In spring boot main class add the following Bold and Underlined code


           import org.springframework.boot.autoconfigure.SpringBootApplication;
           import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;


         @EnableResourceServer
         @SpringBootApplication
        public class DemoApplication {

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

  • Log in to your Okta developer account (or sign up if you don't have an account) and navigate to Applications > Add Applications. Click Single-Page App, click Next and give the app name. Change all instances of localhost:8080 to localhost:3000 (or localhost:4200 if you have angular client) and click Done.
  • Copy the client Id into your server/src/main/resources/application.properties file as:
              okta.oauth2.issuer=https://dev-396769.oktapreview.com/oauth2/default
                  okta.oauth2.clientId=0axfedeaf8D2NZakn0h7
                    security.oauth2.resource.tokenInfoUri=https://dev-396769.oktapreview.com/oauth2/default

      Make sure you don't include -admin in these urls.

      After setting, run your spring boot app and access and you will get the following page.




      Comments

      Popular posts from this blog

      JavaScript - ES2015 (aka ES6)

      Data Bound Controls in ASP.Net - Part 4 (FormView and DetailsView controls)

      The Clickjacking attack and X-Frame-Options