Skip to content

Latest commit

 

History

History
166 lines (148 loc) · 4.3 KB

File metadata and controls

166 lines (148 loc) · 4.3 KB

Spring Boot Demo

  1. Create application using start.spring.io

    http https://start.spring.io/starter.zip \
    dependencies==data-jpa,data-rest,h2,web,devtools,security -d
  2. Run application with ./mvnw spring-boot:run.

  3. Create a Blog entity class.

    @Entity
    class Blog {
    
        @Id
        @GeneratedValue
        private Long id;
        private String name;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Blog{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
  4. Create a JPA Repository.

    interface BlogRepository extends JpaRepository<Blog, Long> {
    }
  5. Create a CommandLineRunner to populate the database.

    @Component
    class BlogCommandLineRunner implements CommandLineRunner {
        private BlogRepository repository;
    
        public BlogCommandLineRunner(BlogRepository repository) {
            this.repository = repository;
        }
    
        @Override
        public void run(String... strings) throws Exception {
            System.out.println(repository.findAll());
        }
    }
  6. Create src/main/resources/data.sql and populate it with same data.

    insert into blog (name) values ('First');
    insert into blog (name) values ('Second');
  7. Create BlogController for your REST API.

    @RestController
    class BlogController {
        private BlogRepository repository;
    
        public BlogController(BlogRepository repository) {
            this.repository = repository;
        }
    
        @RequestMapping("/blogs")
        Collection<Blog> list() {
            return repository.findAll();
        }
    }
  8. Access the API using http localhost:8080/blogs --auth user:<password>.

    Note
    You can change the user’s password by adding security.user.password=user in application.properties.
  9. Delete BlogController and change BlogRepository to extend PagingAndSortingRepository. Annotate with @RepositoryRestResource.

  10. Signup for an Okta Developer account and setup environment variables as recommended in Getting Started with Okta.

  11. Integrate Stormpath’s SDK for Okta by adding the following dependency to pom.xml.

    <dependency>
        <groupId>com.stormpath.spring</groupId>
        <artifactId>stormpath-default-spring-boot-starter</artifactId>
    </dependency>
  12. Add the stormpath-bom to the dependency management section to manage the versions imported by Stormpath.

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.stormpath.sdk</groupId>
                <artifactId>stormpath-bom</artifactId>
                <version>2.0.0-okta-rc1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    Navigate to http://localhost:8080/blogs, login and see JSON in your browser. Can also view from the command line using:

    http localhost:8080/blogs --auth <stormpath-user>:<password>

Deploy to Cloud Foundry

  1. Sign up for Pivotal Web Services.

  2. Download and install the Cloud Foundry CLI.

  3. Login from the command line:

    cf login -a api.run.pivotal.io
  4. Run mvn package and cf push -p target/*jar blog-demo to deploy.

  5. This will fail because you don’t have Okta API variables defined. Remove the Stormpath Java SDK and redeploy or add STORMPATH_CLIENT_BASEURL, OKTA_APPLICATION_ID, and OKTA_API_TOKEN environment variables with the appropriate values.

  6. Restart your application and view it at https://blog-demo.cfapps.io/ (or something similar).

Caution
If you’re using Stormpath, make sure and use HTTPS when accessing your app. If you use HTTP, you won’t be able to login.