init commit
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,6 @@
|
|||||||
|
.gradle/
|
||||||
|
.idea/
|
||||||
|
target/
|
||||||
# ---> Java
|
# ---> Java
|
||||||
# Compiled class file
|
# Compiled class file
|
||||||
*.class
|
*.class
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
# spring-oidc-application
|
# spring-oidc-application
|
||||||
|
|
||||||
Spring Boot OIDC Client Application
|
Spring Boot OIDC Client Application
|
||||||
|
|
||||||
|
|
||||||
|
> https://www.baeldung.com/spring-security-openid-connect
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
7
build.json
Normal file
7
build.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"java": "15",
|
||||||
|
"builder": {
|
||||||
|
"name": "maven",
|
||||||
|
"version": "3.8.4"
|
||||||
|
}
|
||||||
|
}
|
||||||
50
pom.xml
Normal file
50
pom.xml
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<?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/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>me.hatter</groupId>
|
||||||
|
<artifactId>spring-oidc-application</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>oidc-example</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.4.1</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>15</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-oauth2-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<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.security</groupId>
|
||||||
|
<artifactId>spring-security-openid</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
18
src/main/java/me/hatter/sample/Application.java
Normal file
18
src/main/java/me/hatter/sample/Application.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package me.hatter.sample;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see "https://codetinkering.com/spring-security-oauth2-oidc/"
|
||||||
|
* @see "https://github.com/code-tinkering/spring-boot-oauth2-oidc-example"
|
||||||
|
*/
|
||||||
|
@ComponentScan(basePackages = "me.hatter.sample")
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
28
src/main/java/me/hatter/sample/SampleController.java
Normal file
28
src/main/java/me/hatter/sample/SampleController.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package me.hatter.sample;
|
||||||
|
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
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 SampleController {
|
||||||
|
@GetMapping("/oidc-principal")
|
||||||
|
public OidcUser getOidcUserPrincipal(
|
||||||
|
@AuthenticationPrincipal OidcUser principal) {
|
||||||
|
return principal;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/oidc-principal2")
|
||||||
|
public Object getOidcUserPrincipal2() {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
if (authentication.getPrincipal() instanceof OidcUser) {
|
||||||
|
OidcUser principal = ((OidcUser) authentication.getPrincipal());
|
||||||
|
return principal;
|
||||||
|
} else {
|
||||||
|
return authentication.getPrincipal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/main/java/me/hatter/sample/SecurityConfig.java
Normal file
19
src/main/java/me/hatter/sample/SecurityConfig.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package me.hatter.sample;
|
||||||
|
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
|
||||||
|
@EnableWebSecurity
|
||||||
|
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
@Override
|
||||||
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
|
http// only disable these during testing or for non-browser clients
|
||||||
|
// .cors().disable()
|
||||||
|
// .csrf().disable()
|
||||||
|
.authorizeRequests()
|
||||||
|
.anyRequest().authenticated()
|
||||||
|
.and()
|
||||||
|
.oauth2Login().loginPage("/oauth2/authorization/google");
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/main/resources/application.properties
Normal file
8
src/main/resources/application.properties
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
application.name=SpringBoot Sample
|
||||||
|
application.version=0.0.1
|
||||||
|
application.title=This is SpringBoot sample application
|
||||||
|
spring.mvc.throw-exception-if-no-handler-found=true
|
||||||
|
spring.resources.add-mappings=false
|
||||||
|
|
||||||
|
spring.security.oauth2.client.registration.google.client-id=test
|
||||||
|
spring.security.oauth2.client.registration.google.client-secret=test
|
||||||
Reference in New Issue
Block a user