commit
ac5e179e6a
|
@ -58,6 +58,11 @@
|
||||||
<artifactId>javase</artifactId>
|
<artifactId>javase</artifactId>
|
||||||
<version>${qrgen.version}</version>
|
<version>${qrgen.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.rvesse</groupId>
|
||||||
|
<artifactId>airline</artifactId>
|
||||||
|
<version>${airline.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.cactoos</groupId>
|
<groupId>org.cactoos</groupId>
|
||||||
<artifactId>cactoos</artifactId>
|
<artifactId>cactoos</artifactId>
|
||||||
|
@ -82,5 +87,6 @@
|
||||||
<qrgen.version>2.6.0</qrgen.version>
|
<qrgen.version>2.6.0</qrgen.version>
|
||||||
|
|
||||||
<cactoos.version>0.43</cactoos.version>
|
<cactoos.version>0.43</cactoos.version>
|
||||||
|
<airline.version>2.7.2</airline.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.airline;
|
||||||
|
|
||||||
|
import com.github.rvesse.airline.annotations.Cli;
|
||||||
|
import com.github.rvesse.airline.help.Help;
|
||||||
|
|
||||||
|
@Cli(name = "baeldung-cli",
|
||||||
|
description = "Baeldung Airline Tutorial",
|
||||||
|
defaultCommand = Help.class,
|
||||||
|
commands = { DatabaseSetupCommand.class, LoggingCommand.class, Help.class })
|
||||||
|
public class CommandLine {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
com.github.rvesse.airline.Cli<Runnable> cli = new com.github.rvesse.airline.Cli<>(CommandLine.class);
|
||||||
|
Runnable cmd = cli.parse(args);
|
||||||
|
cmd.run();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package com.baeldung.airline;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import com.github.rvesse.airline.HelpOption;
|
||||||
|
import com.github.rvesse.airline.annotations.Command;
|
||||||
|
import com.github.rvesse.airline.annotations.Option;
|
||||||
|
import com.github.rvesse.airline.annotations.OptionType;
|
||||||
|
import com.github.rvesse.airline.annotations.restrictions.AllowedRawValues;
|
||||||
|
import com.github.rvesse.airline.annotations.restrictions.MutuallyExclusiveWith;
|
||||||
|
import com.github.rvesse.airline.annotations.restrictions.Pattern;
|
||||||
|
import com.github.rvesse.airline.annotations.restrictions.RequiredOnlyIf;
|
||||||
|
|
||||||
|
@Command(name = "setup-db", description = "Setup our database")
|
||||||
|
public class DatabaseSetupCommand implements Runnable {
|
||||||
|
@Inject
|
||||||
|
private HelpOption<DatabaseSetupCommand> help;
|
||||||
|
|
||||||
|
@Option(type = OptionType.COMMAND,
|
||||||
|
name = {"-d", "--database"},
|
||||||
|
description = "Type of RDBMS.",
|
||||||
|
title = "RDBMS type: mysql|postgresql|mongodb")
|
||||||
|
@AllowedRawValues(allowedValues = { "mysql", "postgres", "mongodb" })
|
||||||
|
protected String rdbmsMode = "mysql";
|
||||||
|
|
||||||
|
@Option(type = OptionType.COMMAND,
|
||||||
|
name = {"--rdbms:url", "--url"},
|
||||||
|
description = "URL to use for connection to RDBMS.",
|
||||||
|
title = "RDBMS URL")
|
||||||
|
@MutuallyExclusiveWith(tag="mode")
|
||||||
|
@Pattern(pattern="^(http://.*):(d*)(.*)u=(.*)&p=(.*)")
|
||||||
|
protected String rdbmsUrl = "";
|
||||||
|
|
||||||
|
@Option(type = OptionType.COMMAND,
|
||||||
|
name = {"--rdbms:host", "--host"},
|
||||||
|
description = "Host to use for connection to RDBMS.",
|
||||||
|
title = "RDBMS host")
|
||||||
|
@MutuallyExclusiveWith(tag="mode")
|
||||||
|
protected String rdbmsHost = "";
|
||||||
|
|
||||||
|
@RequiredOnlyIf(names={"--rdbms:host", "--host"})
|
||||||
|
@Option(type = OptionType.COMMAND,
|
||||||
|
name = {"--rdbms:user", "-u", "--user"},
|
||||||
|
description = "User for login to RDBMS.",
|
||||||
|
title = "RDBMS user")
|
||||||
|
protected String rdbmsUser;
|
||||||
|
|
||||||
|
@RequiredOnlyIf(names={"--rdbms:host", "--host"})
|
||||||
|
@Option(type = OptionType.COMMAND,
|
||||||
|
name = {"--rdbms:password", "--password"},
|
||||||
|
description = "Password for login to RDBMS.",
|
||||||
|
title = "RDBMS password")
|
||||||
|
protected String rdbmsPassword;
|
||||||
|
|
||||||
|
@Option(type = OptionType.COMMAND,
|
||||||
|
name = {"--driver", "--jars"},
|
||||||
|
description = "List of drivers",
|
||||||
|
title = "--driver <PATH_TO_YOUR_JAR> --driver <PATH_TO_YOUR_JAR>")
|
||||||
|
protected List<String> jars = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
//skipping store our choices...
|
||||||
|
if (!help.showHelpIfRequested()) {
|
||||||
|
if(!"".equals(rdbmsHost)) {
|
||||||
|
System.out.println("Connecting to database host: " + rdbmsHost);
|
||||||
|
System.out.println("Credential: " + rdbmsUser + " / " + rdbmsPassword);
|
||||||
|
} else {
|
||||||
|
System.out.println("Connecting to database url: " + rdbmsUrl);
|
||||||
|
}
|
||||||
|
System.out.println(jars.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.airline;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import com.github.rvesse.airline.HelpOption;
|
||||||
|
import com.github.rvesse.airline.annotations.Command;
|
||||||
|
import com.github.rvesse.airline.annotations.Option;
|
||||||
|
|
||||||
|
@Command(name = "setup-log", description = "Setup our log")
|
||||||
|
public class LoggingCommand implements Runnable {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private HelpOption<LoggingCommand> help;
|
||||||
|
|
||||||
|
@Option(name = { "-v", "--verbose" }, description = "Set log verbosity on/off")
|
||||||
|
private boolean verbose = false;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
//skipping store user choice
|
||||||
|
if (!help.showHelpIfRequested())
|
||||||
|
System.out.println("Verbosity: " + verbose);
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,6 +24,16 @@
|
||||||
<artifactId>spring-security-oauth2</artifactId>
|
<artifactId>spring-security-oauth2</artifactId>
|
||||||
<version>2.4.0.RELEASE</version>
|
<version>2.4.0.RELEASE</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security</groupId>
|
||||||
|
<artifactId>spring-security-core</artifactId>
|
||||||
|
<version>5.2.1.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>2.6</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.security.oauth.boot</groupId>
|
<groupId>org.springframework.security.oauth.boot</groupId>
|
||||||
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
package com.baeldung.springbootsecurity.oauth2server;
|
package com.baeldung.springbootsecurity.oauth2server;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.annotation.CurrentSecurityContext;
|
||||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
|
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
|
||||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
@ -14,6 +18,8 @@ import java.security.Principal;
|
||||||
@SpringBootApplication(scanBasePackages = "com.baeldung.springbootsecurity.oauth2server")
|
@SpringBootApplication(scanBasePackages = "com.baeldung.springbootsecurity.oauth2server")
|
||||||
public class SpringBootAuthorizationServerApplication {
|
public class SpringBootAuthorizationServerApplication {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(SpringBootAuthorizationServerApplication.class);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(SpringBootAuthorizationServerApplication.class, args);
|
SpringApplication.run(SpringBootAuthorizationServerApplication.class, args);
|
||||||
}
|
}
|
||||||
|
@ -26,5 +32,16 @@ public class SpringBootAuthorizationServerApplication {
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/authentication")
|
||||||
|
public Object getAuthentication(@CurrentSecurityContext(expression = "authentication") Authentication authentication) {
|
||||||
|
logger.info("authentication -> {}", authentication);
|
||||||
|
return authentication.getDetails();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/principal")
|
||||||
|
public String getPrincipal(@CurrentSecurityContext(expression = "authentication.principal") Principal principal) {
|
||||||
|
logger.info("principal -> {}", principal);
|
||||||
|
return principal.getName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package com.baeldung.springbootsecurity.oauth2server;
|
package com.baeldung.springbootsecurity.oauth2server;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.web.server.LocalServerPort;
|
||||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||||
import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException;
|
import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException;
|
||||||
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
|
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
|
||||||
|
@ -10,8 +12,13 @@ import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
|
@ -19,6 +26,14 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||||
@ActiveProfiles("authz")
|
@ActiveProfiles("authz")
|
||||||
public class CustomConfigAuthorizationServerIntegrationTest extends OAuth2IntegrationTestSupport {
|
public class CustomConfigAuthorizationServerIntegrationTest extends OAuth2IntegrationTestSupport {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
base = new URL("http://localhost:" + port);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
|
public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
|
||||||
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("read"));
|
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("read"));
|
||||||
|
@ -27,7 +42,29 @@ public class CustomConfigAuthorizationServerIntegrationTest extends OAuth2Integr
|
||||||
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
|
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
|
||||||
|
|
||||||
assertNotNull(accessToken);
|
assertNotNull(accessToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenOAuth2Context_whenAccessingAuthentication_ThenRespondTokenDetails() {
|
||||||
|
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("read"));
|
||||||
|
OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
|
||||||
|
|
||||||
|
String authentication = executeGetRequest(restTemplate, "/authentication");
|
||||||
|
|
||||||
|
Pattern pattern = Pattern.compile("\\{\"remoteAddress\":\".*" +
|
||||||
|
"\",\"sessionId\":null,\"tokenValue\":\".*" +
|
||||||
|
"\",\"tokenType\":\"Bearer\",\"decodedDetails\":null}");
|
||||||
|
assertTrue("authentication", pattern.matcher(authentication).matches());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenOAuth2Context_whenAccessingPrincipal_ThenRespondBaeldung() {
|
||||||
|
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("read"));
|
||||||
|
OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
|
||||||
|
|
||||||
|
String principal = executeGetRequest(restTemplate, "/principal");
|
||||||
|
|
||||||
|
assertEquals("baeldung", principal);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = OAuth2AccessDeniedException.class)
|
@Test(expected = OAuth2AccessDeniedException.class)
|
||||||
|
|
|
@ -1,19 +1,33 @@
|
||||||
package com.baeldung.springbootsecurity.oauth2server;
|
package com.baeldung.springbootsecurity.oauth2server;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
|
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
|
||||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||||
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
|
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
|
||||||
|
import org.springframework.web.client.RequestCallback;
|
||||||
|
import org.springframework.web.client.ResponseExtractor;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
import static java.lang.String.format;
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
|
import static org.springframework.http.HttpMethod.GET;
|
||||||
|
|
||||||
public class OAuth2IntegrationTestSupport {
|
public class OAuth2IntegrationTestSupport {
|
||||||
|
|
||||||
@Value("${local.server.port}") protected int port;
|
public static final ResponseExtractor<String> EXTRACT_BODY_AS_STRING = clientHttpResponse ->
|
||||||
|
IOUtils.toString(clientHttpResponse.getBody(), Charset.defaultCharset());
|
||||||
|
private static final RequestCallback DO_NOTHING_CALLBACK = request -> {
|
||||||
|
};
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
protected int port;
|
||||||
|
|
||||||
|
protected URL base;
|
||||||
|
|
||||||
protected ClientCredentialsResourceDetails getClientCredentialsResourceDetails(final String clientId, final List<String> scopes) {
|
protected ClientCredentialsResourceDetails getClientCredentialsResourceDetails(final String clientId, final List<String> scopes) {
|
||||||
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
|
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
|
||||||
|
@ -31,4 +45,9 @@ public class OAuth2IntegrationTestSupport {
|
||||||
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
|
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
|
||||||
return restTemplate;
|
return restTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected String executeGetRequest(OAuth2RestTemplate restTemplate, String path) {
|
||||||
|
return restTemplate.execute(base.toString() + path, GET, DO_NOTHING_CALLBACK, EXTRACT_BODY_AS_STRING);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue