Merge pull request #9221 from SmartyAnsh/BAEL-3995_Spring_Security_Okta

BAEL-3995 - Spring Security with Okta
This commit is contained in:
Eric Martin 2020-05-09 10:56:43 -05:00 committed by GitHub
commit 6020587d90
6 changed files with 143 additions and 0 deletions

View File

@ -31,6 +31,7 @@
<module>spring-security-mvc-persisted-remember-me</module>
<module>spring-security-mvc-socket</module>
<module>spring-security-oidc</module>
<module>spring-security-okta</module>
<module>spring-security-react</module>
<module>spring-security-rest</module>
<module>spring-security-rest-basic-auth</module>

View File

@ -0,0 +1,62 @@
<?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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-security-okta</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring-security-okta</name>
<packaging>war</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.okta.spring</groupId>
<artifactId>okta-spring-boot-starter</artifactId>
<version>${okta.spring.version}</version>
</dependency>
<dependency>
<groupId>com.okta.spring</groupId>
<artifactId>okta-spring-sdk</artifactId>
<version>${okta.spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>spring-security-okta</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<okta.spring.version>1.4.0</okta.spring.version>
</properties>
</project>

View File

@ -0,0 +1,13 @@
package com.baeldung.okta;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.okta.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.okta.sdk.client.Client;
import com.okta.sdk.resource.user.User;
import com.okta.sdk.resource.user.UserBuilder;
import com.okta.sdk.resource.user.UserList;
@RestController
public class AdminController {
@Autowired
public Client client;
@GetMapping("/users")
public UserList getUsers() {
return client.listUsers();
}
@GetMapping("/user")
public UserList searchUserByEmail(@RequestParam String query) {
return client.listUsers(query, null, null, null, null);
}
@GetMapping("/createUser")
public User createUser() {
char[] tempPassword = {'P','a','$','$','w','0','r','d'};
User user = UserBuilder.instance()
.setEmail("norman.lewis@email.com")
.setFirstName("Norman")
.setLastName("Lewis")
.setPassword(tempPassword)
.setActive(true)
.buildAndCreate(client);
return user;
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.okta.controller;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@GetMapping("/")
public String home(@AuthenticationPrincipal OidcUser user) {
return "Welcome, "+ user.getFullName() +"!";
}
}

View File

@ -0,0 +1,8 @@
okta.oauth2.issuer= //Auth server issuer URL
okta.oauth2.client-id= //Client ID of our Okta application
okta.oauth2.client-secret= //Client secret of our Okta application
okta.oauth2.redirect-uri=/authorization-code/callback
#Okta Spring SDK configs
okta.client.orgUrl= //orgURL
okta.client.token= //token generated