moved PrincipalExtractor and AuthoritiesExtractor example to spring-5-security module

This commit is contained in:
db 2018-07-28 13:06:41 +01:00
parent dab27a059e
commit d1f2917c33
12 changed files with 50 additions and 98 deletions

View File

@ -58,6 +58,13 @@
<artifactId>spring-security-test</artifactId> <artifactId>spring-security-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- -->
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -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";
}
}

View File

@ -1,16 +1,18 @@
package org.baeldung.configuration; package com.baeldung.oauth2extractors.configuration;
import org.baeldung.extractor.CustomAuthoritiesExtractor; import com.baeldung.oauth2extractors.extractor.CustomAuthoritiesExtractor;
import org.baeldung.extractor.CustomPrincipalExtractor; import com.baeldung.oauth2extractors.extractor.CustomPrincipalExtractor;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; 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.AuthoritiesExtractor;
import org.springframework.boot.autoconfigure.security.oauth2.resource.PrincipalExtractor; import org.springframework.boot.autoconfigure.security.oauth2.resource.PrincipalExtractor;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 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.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration @Configuration
@PropertySource("application-oauth2-extractors.properties")
@EnableOAuth2Sso @EnableOAuth2Sso
public class SecurityConfig extends WebSecurityConfigurerAdapter { public class SecurityConfig extends WebSecurityConfigurerAdapter {

View File

@ -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.boot.autoconfigure.security.oauth2.resource.AuthoritiesExtractor;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
@ -10,8 +10,10 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
public class CustomAuthoritiesExtractor implements AuthoritiesExtractor { public class CustomAuthoritiesExtractor implements AuthoritiesExtractor {
private static final List<GrantedAuthority> GITHUB_FREE_AUTHORITIES = AuthorityUtils.commaSeparatedStringToAuthorityList("GITHUB_USER,GITHUB_USER_FREE"); private List<GrantedAuthority> GITHUB_FREE_AUTHORITIES = AuthorityUtils
private static final List<GrantedAuthority> GITHUB_SUBSCRIBED_AUTHORITIES = AuthorityUtils.commaSeparatedStringToAuthorityList("GITHUB_USER,GITHUB_USER_SUBSCRIBED"); .commaSeparatedStringToAuthorityList("GITHUB_USER,GITHUB_USER_FREE");
private List<GrantedAuthority> GITHUB_SUBSCRIBED_AUTHORITIES = AuthorityUtils
.commaSeparatedStringToAuthorityList("GITHUB_USER,GITHUB_USER_SUBSCRIBED");
@Override @Override
public List<GrantedAuthority> extractAuthorities(Map<String, Object> map) { public List<GrantedAuthority> extractAuthorities(Map<String, Object> map) {

View File

@ -1,4 +1,4 @@
package org.baeldung.extractor; package com.baeldung.oauth2extractors.extractor;
import org.springframework.boot.autoconfigure.security.oauth2.resource.PrincipalExtractor; import org.springframework.boot.autoconfigure.security.oauth2.resource.PrincipalExtractor;

View File

@ -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

View File

@ -1,5 +1,6 @@
import org.baeldung.Application; package com.baeldung.oauth2extractors;
import org.baeldung.configuration.SecurityConfig;
import com.baeldung.oauth2extractors.configuration.SecurityConfig;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -18,9 +19,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@RunWith(SpringRunner.class) @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}) @ContextConfiguration(classes = {SecurityConfig.class})
public class ApplicationUnitTest { public class ExtractorsUnitTest {
@Autowired @Autowired
private WebApplicationContext context; private WebApplicationContext context;

View File

@ -19,7 +19,6 @@
<module>spring-security-sso-auth-server</module> <module>spring-security-sso-auth-server</module>
<module>spring-security-sso-ui</module> <module>spring-security-sso-ui</module>
<module>spring-security-sso-ui-2</module> <module>spring-security-sso-ui-2</module>
<module>spring-security-principal-authorities-extractor</module>
</modules> </modules>
<properties> <properties>

View File

@ -1,53 +0,0 @@
<?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">
<parent>
<artifactId>spring-security-sso</artifactId>
<groupId>org.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-security-principal-authorities-extractor</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>${oauth-auto.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -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";
}
}

View File

@ -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