-
Create application using start.spring.io
http https://start.spring.io/starter.zip \ dependencies==data-jpa,data-rest,h2,web,devtools,security -d
-
Run application with
./mvnw spring-boot:run
. -
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 + '\'' + '}'; } }
-
Create a JPA Repository.
interface BlogRepository extends JpaRepository<Blog, Long> { }
-
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()); } }
-
Create
src/main/resources/data.sql
and populate it with same data.insert into blog (name) values ('First'); insert into blog (name) values ('Second');
-
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(); } }
-
Access the API using
http localhost:8080/blogs --auth user:<password>
.NoteYou can change the user’s password by adding security.user.password=user
inapplication.properties
. -
Delete BlogController and change
BlogRepository
to extendPagingAndSortingRepository
. Annotate with@RepositoryRestResource
. -
Signup for an Okta Developer account and setup environment variables as recommended in Getting Started with Okta.
-
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>
-
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>
-
Sign up for Pivotal Web Services.
-
Download and install the Cloud Foundry CLI.
-
Login from the command line:
cf login -a api.run.pivotal.io
-
Run
mvn package
andcf push -p target/*jar blog-demo
to deploy. -
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
, andOKTA_API_TOKEN
environment variables with the appropriate values. -
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. |