From d1f2917c3394157323a5b3668833953193228052 Mon Sep 17 00:00:00 2001 From: db Date: Sat, 28 Jul 2018 13:06:41 +0100 Subject: [PATCH] moved PrincipalExtractor and AuthoritiesExtractor example to spring-5-security module --- spring-5-security/pom.xml | 7 +++ .../ExtractorsApplication.java | 20 +++++++ .../configuration/SecurityConfig.java | 10 ++-- .../extractor/CustomAuthoritiesExtractor.java | 8 +-- .../extractor/CustomPrincipalExtractor.java | 2 +- .../application-oauth2-extractors.properties | 6 +++ .../templates/oauth2_extractors.html | 0 .../oauth2extractors/ExtractorsUnitTest.java | 9 ++-- spring-security-sso/pom.xml | 1 - .../pom.xml | 53 ------------------- .../main/java/org/baeldung/Application.java | 18 ------- .../src/main/resources/application.yml | 14 ----- 12 files changed, 50 insertions(+), 98 deletions(-) create mode 100644 spring-5-security/src/main/java/com/baeldung/oauth2extractors/ExtractorsApplication.java rename {spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung => spring-5-security/src/main/java/com/baeldung/oauth2extractors}/configuration/SecurityConfig.java (79%) rename {spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung => spring-5-security/src/main/java/com/baeldung/oauth2extractors}/extractor/CustomAuthoritiesExtractor.java (67%) rename {spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung => spring-5-security/src/main/java/com/baeldung/oauth2extractors}/extractor/CustomPrincipalExtractor.java (86%) create mode 100644 spring-5-security/src/main/resources/application-oauth2-extractors.properties rename spring-security-sso/spring-security-principal-authorities-extractor/src/main/resources/templates/index.html => spring-5-security/src/main/resources/templates/oauth2_extractors.html (100%) rename spring-security-sso/spring-security-principal-authorities-extractor/src/test/java/ApplicationUnitTest.java => spring-5-security/src/test/java/com/baeldung/oauth2extractors/ExtractorsUnitTest.java (85%) delete mode 100644 spring-security-sso/spring-security-principal-authorities-extractor/pom.xml delete mode 100644 spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/Application.java delete mode 100644 spring-security-sso/spring-security-principal-authorities-extractor/src/main/resources/application.yml diff --git a/spring-5-security/pom.xml b/spring-5-security/pom.xml index 8cca2ed916..d0ea46928f 100644 --- a/spring-5-security/pom.xml +++ b/spring-5-security/pom.xml @@ -58,6 +58,13 @@ spring-security-test test + + + + org.springframework.security.oauth.boot + spring-security-oauth2-autoconfigure + 2.0.1.RELEASE + diff --git a/spring-5-security/src/main/java/com/baeldung/oauth2extractors/ExtractorsApplication.java b/spring-5-security/src/main/java/com/baeldung/oauth2extractors/ExtractorsApplication.java new file mode 100644 index 0000000000..c9a18d1599 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/oauth2extractors/ExtractorsApplication.java @@ -0,0 +1,20 @@ +package com.baeldung.oauth2extractors; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@SpringBootApplication +@Controller +public class ExtractorsApplication { + public static void main(String[] args) { + SpringApplication.run(ExtractorsApplication.class, args); + } + + @RequestMapping("/") + public String index() { + return "oauth2_extractors"; + } + +} diff --git a/spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/configuration/SecurityConfig.java b/spring-5-security/src/main/java/com/baeldung/oauth2extractors/configuration/SecurityConfig.java similarity index 79% rename from spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/configuration/SecurityConfig.java rename to spring-5-security/src/main/java/com/baeldung/oauth2extractors/configuration/SecurityConfig.java index 4de1932392..cc1258d14b 100644 --- a/spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/configuration/SecurityConfig.java +++ b/spring-5-security/src/main/java/com/baeldung/oauth2extractors/configuration/SecurityConfig.java @@ -1,16 +1,18 @@ -package org.baeldung.configuration; +package com.baeldung.oauth2extractors.configuration; -import org.baeldung.extractor.CustomAuthoritiesExtractor; -import org.baeldung.extractor.CustomPrincipalExtractor; +import com.baeldung.oauth2extractors.extractor.CustomAuthoritiesExtractor; +import com.baeldung.oauth2extractors.extractor.CustomPrincipalExtractor; import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; import org.springframework.boot.autoconfigure.security.oauth2.resource.AuthoritiesExtractor; import org.springframework.boot.autoconfigure.security.oauth2.resource.PrincipalExtractor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration +@PropertySource("application-oauth2-extractors.properties") @EnableOAuth2Sso public class SecurityConfig extends WebSecurityConfigurerAdapter { @@ -35,4 +37,4 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { public AuthoritiesExtractor authoritiesExtractor() { return new CustomAuthoritiesExtractor(); } -} +} \ No newline at end of file diff --git a/spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/extractor/CustomAuthoritiesExtractor.java b/spring-5-security/src/main/java/com/baeldung/oauth2extractors/extractor/CustomAuthoritiesExtractor.java similarity index 67% rename from spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/extractor/CustomAuthoritiesExtractor.java rename to spring-5-security/src/main/java/com/baeldung/oauth2extractors/extractor/CustomAuthoritiesExtractor.java index c1a78634aa..ad23f6c32f 100644 --- a/spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/extractor/CustomAuthoritiesExtractor.java +++ b/spring-5-security/src/main/java/com/baeldung/oauth2extractors/extractor/CustomAuthoritiesExtractor.java @@ -1,4 +1,4 @@ -package org.baeldung.extractor; +package com.baeldung.oauth2extractors.extractor; import org.springframework.boot.autoconfigure.security.oauth2.resource.AuthoritiesExtractor; import org.springframework.security.core.GrantedAuthority; @@ -10,8 +10,10 @@ import java.util.Map; import java.util.Objects; public class CustomAuthoritiesExtractor implements AuthoritiesExtractor { - private static final List GITHUB_FREE_AUTHORITIES = AuthorityUtils.commaSeparatedStringToAuthorityList("GITHUB_USER,GITHUB_USER_FREE"); - private static final List GITHUB_SUBSCRIBED_AUTHORITIES = AuthorityUtils.commaSeparatedStringToAuthorityList("GITHUB_USER,GITHUB_USER_SUBSCRIBED"); + private List GITHUB_FREE_AUTHORITIES = AuthorityUtils + .commaSeparatedStringToAuthorityList("GITHUB_USER,GITHUB_USER_FREE"); + private List GITHUB_SUBSCRIBED_AUTHORITIES = AuthorityUtils + .commaSeparatedStringToAuthorityList("GITHUB_USER,GITHUB_USER_SUBSCRIBED"); @Override public List extractAuthorities(Map map) { diff --git a/spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/extractor/CustomPrincipalExtractor.java b/spring-5-security/src/main/java/com/baeldung/oauth2extractors/extractor/CustomPrincipalExtractor.java similarity index 86% rename from spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/extractor/CustomPrincipalExtractor.java rename to spring-5-security/src/main/java/com/baeldung/oauth2extractors/extractor/CustomPrincipalExtractor.java index d356c07e3b..c35522f0f3 100644 --- a/spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/extractor/CustomPrincipalExtractor.java +++ b/spring-5-security/src/main/java/com/baeldung/oauth2extractors/extractor/CustomPrincipalExtractor.java @@ -1,4 +1,4 @@ -package org.baeldung.extractor; +package com.baeldung.oauth2extractors.extractor; import org.springframework.boot.autoconfigure.security.oauth2.resource.PrincipalExtractor; diff --git a/spring-5-security/src/main/resources/application-oauth2-extractors.properties b/spring-5-security/src/main/resources/application-oauth2-extractors.properties new file mode 100644 index 0000000000..51d6ee7d6e --- /dev/null +++ b/spring-5-security/src/main/resources/application-oauth2-extractors.properties @@ -0,0 +1,6 @@ +security.oauth2.client.client-id=89a7c4facbb3434d599d +security.oauth2.client.client-secret=9b3b08e4a340bd20e866787e4645b54f73d74b6a +security.oauth2.client.access-token-uri=https://github.com/login/oauth/access_token +security.oauth2.client.user-authorization-uri=https://github.com/login/oauth/authorize +security.oauth2.client.scope=read:user,user:email +security.oauth2.resource.user-info-uri=https://api.github.com/user \ No newline at end of file diff --git a/spring-security-sso/spring-security-principal-authorities-extractor/src/main/resources/templates/index.html b/spring-5-security/src/main/resources/templates/oauth2_extractors.html similarity index 100% rename from spring-security-sso/spring-security-principal-authorities-extractor/src/main/resources/templates/index.html rename to spring-5-security/src/main/resources/templates/oauth2_extractors.html diff --git a/spring-security-sso/spring-security-principal-authorities-extractor/src/test/java/ApplicationUnitTest.java b/spring-5-security/src/test/java/com/baeldung/oauth2extractors/ExtractorsUnitTest.java similarity index 85% rename from spring-security-sso/spring-security-principal-authorities-extractor/src/test/java/ApplicationUnitTest.java rename to spring-5-security/src/test/java/com/baeldung/oauth2extractors/ExtractorsUnitTest.java index c14cbc9866..164bc4933f 100644 --- a/spring-security-sso/spring-security-principal-authorities-extractor/src/test/java/ApplicationUnitTest.java +++ b/spring-5-security/src/test/java/com/baeldung/oauth2extractors/ExtractorsUnitTest.java @@ -1,5 +1,6 @@ -import org.baeldung.Application; -import org.baeldung.configuration.SecurityConfig; +package com.baeldung.oauth2extractors; + +import com.baeldung.oauth2extractors.configuration.SecurityConfig; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -18,9 +19,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @RunWith(SpringRunner.class) -@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(classes = ExtractorsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ContextConfiguration(classes = {SecurityConfig.class}) -public class ApplicationUnitTest { +public class ExtractorsUnitTest { @Autowired private WebApplicationContext context; diff --git a/spring-security-sso/pom.xml b/spring-security-sso/pom.xml index 0cf468c2e3..764e899640 100644 --- a/spring-security-sso/pom.xml +++ b/spring-security-sso/pom.xml @@ -19,7 +19,6 @@ spring-security-sso-auth-server spring-security-sso-ui spring-security-sso-ui-2 - spring-security-principal-authorities-extractor diff --git a/spring-security-sso/spring-security-principal-authorities-extractor/pom.xml b/spring-security-sso/spring-security-principal-authorities-extractor/pom.xml deleted file mode 100644 index 5bd8de9c16..0000000000 --- a/spring-security-sso/spring-security-principal-authorities-extractor/pom.xml +++ /dev/null @@ -1,53 +0,0 @@ - - - - spring-security-sso - org.baeldung - 1.0.0-SNAPSHOT - - 4.0.0 - - spring-security-principal-authorities-extractor - - - - org.springframework.boot - spring-boot-starter-security - - - - org.springframework.security.oauth.boot - spring-security-oauth2-autoconfigure - ${oauth-auto.version} - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - - org.thymeleaf.extras - thymeleaf-extras-springsecurity4 - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.springframework.security - spring-security-test - test - - - \ No newline at end of file diff --git a/spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/Application.java b/spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/Application.java deleted file mode 100644 index 0dfbbef86e..0000000000 --- a/spring-security-sso/spring-security-principal-authorities-extractor/src/main/java/org/baeldung/Application.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.baeldung; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; - -@SpringBootApplication -public class Application { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } - - @GetMapping("/") - public String homePage(Model model) { - return "index"; - } -} diff --git a/spring-security-sso/spring-security-principal-authorities-extractor/src/main/resources/application.yml b/spring-security-sso/spring-security-principal-authorities-extractor/src/main/resources/application.yml deleted file mode 100644 index 324df694df..0000000000 --- a/spring-security-sso/spring-security-principal-authorities-extractor/src/main/resources/application.yml +++ /dev/null @@ -1,14 +0,0 @@ -security: - oauth2: - client: - clientId: 89a7c4facbb3434d599d - clientSecret: 9b3b08e4a340bd20e866787e4645b54f73d74b6a - accessTokenUri: https://github.com/login/oauth/access_token - userAuthorizationUri: https://github.com/login/oauth/authorize - clientAuthenticationScheme: form - scope: read:user,user:email - resource: - userInfoUri: https://api.github.com/user -spring: - thymeleaf: - cache: false \ No newline at end of file