parent
dbd0104863
commit
b840f15629
1
pom.xml
1
pom.xml
|
@ -192,7 +192,6 @@
|
||||||
<module>spring-security-mvc-session</module>
|
<module>spring-security-mvc-session</module>
|
||||||
<module>spring-security-rest-basic-auth</module>
|
<module>spring-security-rest-basic-auth</module>
|
||||||
<module>spring-security-rest-custom</module>
|
<module>spring-security-rest-custom</module>
|
||||||
<module>spring-security-rest-digest-auth</module>
|
|
||||||
<module>spring-security-rest-full</module>
|
<module>spring-security-rest-full</module>
|
||||||
<module>spring-security-rest</module>
|
<module>spring-security-rest</module>
|
||||||
<module>spring-security-x509</module>
|
<module>spring-security-x509</module>
|
||||||
|
|
|
@ -7,3 +7,5 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com
|
||||||
|
|
||||||
### Relevant Article:
|
### Relevant Article:
|
||||||
- [Spring Security Digest Authentication](http://www.baeldung.com/spring-security-digest-authentication)
|
- [Spring Security Digest Authentication](http://www.baeldung.com/spring-security-digest-authentication)
|
||||||
|
- [RestTemplate with Digest Authentication](http://www.baeldung.com/resttemplate-digest-authentication)
|
||||||
|
|
||||||
|
|
|
@ -84,6 +84,18 @@
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-oxm</artifactId>
|
||||||
|
<version>${org.springframework.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-web</artifactId>
|
||||||
|
<version>${org.springframework.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- web -->
|
<!-- web -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -108,6 +120,39 @@
|
||||||
<version>${guava.version}</version>
|
<version>${guava.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpcore</artifactId>
|
||||||
|
<version>${httpcore.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>${httpclient.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<version>${org.springframework.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package org.baeldung.client;
|
package org.baeldung.client;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
import org.apache.http.client.AuthCache;
|
import org.apache.http.client.AuthCache;
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.HttpClient;
|
||||||
|
@ -13,6 +11,8 @@ import org.apache.http.protocol.HttpContext;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
public class HttpComponentsClientHttpRequestFactoryDigestAuth extends HttpComponentsClientHttpRequestFactory {
|
public class HttpComponentsClientHttpRequestFactoryDigestAuth extends HttpComponentsClientHttpRequestFactory {
|
||||||
HttpHost host;
|
HttpHost host;
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package org.baeldung.spring;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.auth.AuthScope;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.client.CredentialsProvider;
|
||||||
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.baeldung.client.HttpComponentsClientHttpRequestFactoryDigestAuth;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class ClientConfig {
|
||||||
|
private static final String DEFAULT_USER = "user1";
|
||||||
|
private static final String DEFAULT_PASS = "user1Pass";
|
||||||
|
|
||||||
|
public ClientConfig() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplate() {
|
||||||
|
HttpHost host = new HttpHost("localhost", 8080, "http");
|
||||||
|
CloseableHttpClient client = HttpClientBuilder.create().
|
||||||
|
setDefaultCredentialsProvider(provider()).useSystemProperties().build();
|
||||||
|
HttpComponentsClientHttpRequestFactory requestFactory =
|
||||||
|
new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client);
|
||||||
|
|
||||||
|
return new RestTemplate(requestFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CredentialsProvider provider() {
|
||||||
|
CredentialsProvider provider = new BasicCredentialsProvider();
|
||||||
|
UsernamePasswordCredentials credentials =
|
||||||
|
new UsernamePasswordCredentials("user1", "user1Pass");
|
||||||
|
provider.setCredentials(AuthScope.ANY, credentials);
|
||||||
|
return provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,22 +1,21 @@
|
||||||
package org.baeldung.client;
|
package org.baeldung.client;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
import org.baeldung.client.spring.ClientConfig;
|
import org.baeldung.spring.ClientConfig;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = {ClientConfig.class}, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = {ClientConfig.class}, loader = AnnotationConfigContextLoader.class)
|
||||||
public class RawClientLiveTest {
|
public class RawClientLiveTest {
|
||||||
|
@ -24,26 +23,25 @@ public class RawClientLiveTest {
|
||||||
// tests
|
// tests
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSecuredRestApiIsConsumed_then200OK() throws ClientProtocolException, IOException {
|
public void whenSecuredRestApiIsConsumed_then200OK() throws IOException {
|
||||||
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||||||
|
|
||||||
final int timeout = 20; // seconds
|
int timeout = 20; // seconds
|
||||||
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setConnectTimeout(timeout).setSocketTimeout(timeout).build();
|
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout)
|
||||||
final HttpGet getMethod = new HttpGet("http://localhost:8082/spring-security-rest-basic-auth/api/bars/1");
|
.setConnectTimeout(timeout).setSocketTimeout(timeout).build();
|
||||||
|
HttpGet getMethod = new HttpGet("http://localhost:8082/spring-security-rest-basic-auth/api/bars/1");
|
||||||
getMethod.setConfig(requestConfig);
|
getMethod.setConfig(requestConfig);
|
||||||
|
|
||||||
final int hardTimeout = 5; // seconds
|
int hardTimeout = 5; // seconds
|
||||||
final TimerTask task = new TimerTask() {
|
TimerTask task = new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (getMethod != null) {
|
|
||||||
getMethod.abort();
|
getMethod.abort();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
new Timer(true).schedule(task, hardTimeout * 1000);
|
new Timer(true).schedule(task, hardTimeout * 1000);
|
||||||
|
|
||||||
final HttpResponse response = httpClient.execute(getMethod);
|
HttpResponse response = httpClient.execute(getMethod);
|
||||||
System.out.println("HTTP Status of response: " + response.getStatusLine().getStatusCode());
|
System.out.println("HTTP Status of response: " + response.getStatusLine().getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
*.class
|
|
||||||
|
|
||||||
#folders#
|
|
||||||
/target
|
|
||||||
/neoDb*
|
|
||||||
/data
|
|
||||||
/src/main/webapp/WEB-INF/classes
|
|
||||||
*/META-INF/*
|
|
||||||
|
|
||||||
# Packaged files #
|
|
||||||
*.jar
|
|
||||||
*.war
|
|
||||||
*.ear
|
|
|
@ -1,9 +0,0 @@
|
||||||
=========
|
|
||||||
|
|
||||||
## REST API with Digest Authentication - Example Project
|
|
||||||
|
|
||||||
###The Course
|
|
||||||
The "Learn Spring Security" Classes: http://github.learnspringsecurity.com
|
|
||||||
|
|
||||||
### Relevant Articles:
|
|
||||||
- [RestTemplate with Digest Authentication](http://www.baeldung.com/resttemplate-digest-authentication)
|
|
|
@ -1,299 +0,0 @@
|
||||||
<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>com.baeldung</groupId>
|
|
||||||
<artifactId>spring-security-rest-digest-auth</artifactId>
|
|
||||||
<version>0.1-SNAPSHOT</version>
|
|
||||||
|
|
||||||
<name>spring-security-rest-digest-auth</name>
|
|
||||||
<packaging>war</packaging>
|
|
||||||
|
|
||||||
<parent>
|
|
||||||
<groupId>com.baeldung</groupId>
|
|
||||||
<artifactId>parent-modules</artifactId>
|
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
|
|
||||||
<!-- Spring Security -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.security</groupId>
|
|
||||||
<artifactId>spring-security-web</artifactId>
|
|
||||||
<version>${org.springframework.security.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.security</groupId>
|
|
||||||
<artifactId>spring-security-config</artifactId>
|
|
||||||
<version>${org.springframework.security.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Spring -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-core</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-context</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-jdbc</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-beans</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-aop</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-tx</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-expression</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-web</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-webmvc</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-oxm</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-web</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- marshalling -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
|
||||||
<artifactId>jackson-databind</artifactId>
|
|
||||||
<version>${jackson.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- http -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
|
||||||
<artifactId>httpcore</artifactId>
|
|
||||||
<version>${httpcore.version}</version>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
|
||||||
<artifactId>httpclient</artifactId>
|
|
||||||
<version>${httpclient.version}</version>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- web -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet</groupId>
|
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
|
||||||
<version>${javax.servlet.version}</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet</groupId>
|
|
||||||
<artifactId>jstl</artifactId>
|
|
||||||
<version>${jstl.version}</version>
|
|
||||||
<scope>runtime</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- util -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.google.guava</groupId>
|
|
||||||
<artifactId>guava</artifactId>
|
|
||||||
<version>${guava.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- test scoped -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-test</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<finalName>spring-security-rest-digest-auth</finalName>
|
|
||||||
<resources>
|
|
||||||
<resource>
|
|
||||||
<directory>src/main/resources</directory>
|
|
||||||
<filtering>true</filtering>
|
|
||||||
</resource>
|
|
||||||
</resources>
|
|
||||||
|
|
||||||
<plugins>
|
|
||||||
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
|
||||||
<version>${maven-war-plugin.version}</version>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.codehaus.cargo</groupId>
|
|
||||||
<artifactId>cargo-maven2-plugin</artifactId>
|
|
||||||
<version>${cargo-maven2-plugin.version}</version>
|
|
||||||
<configuration>
|
|
||||||
<wait>true</wait>
|
|
||||||
<container>
|
|
||||||
<containerId>jetty8x</containerId>
|
|
||||||
<type>embedded</type>
|
|
||||||
<systemProperties>
|
|
||||||
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
|
|
||||||
</systemProperties>
|
|
||||||
</container>
|
|
||||||
<configuration>
|
|
||||||
<properties>
|
|
||||||
<cargo.servlet.port>8082</cargo.servlet.port>
|
|
||||||
</properties>
|
|
||||||
</configuration>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
</plugins>
|
|
||||||
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<profiles>
|
|
||||||
<profile>
|
|
||||||
<id>live</id>
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.codehaus.cargo</groupId>
|
|
||||||
<artifactId>cargo-maven2-plugin</artifactId>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<id>start-server</id>
|
|
||||||
<phase>pre-integration-test</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>start</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
<execution>
|
|
||||||
<id>stop-server</id>
|
|
||||||
<phase>post-integration-test</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>stop</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<phase>integration-test</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>test</goal>
|
|
||||||
</goals>
|
|
||||||
<configuration>
|
|
||||||
<excludes>
|
|
||||||
<exclude>none</exclude>
|
|
||||||
</excludes>
|
|
||||||
<includes>
|
|
||||||
<include>**/*LiveTest.java</include>
|
|
||||||
</includes>
|
|
||||||
<systemPropertyVariables>
|
|
||||||
<webTarget>cargo</webTarget>
|
|
||||||
</systemPropertyVariables>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</profile>
|
|
||||||
</profiles>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<!-- Spring -->
|
|
||||||
<org.springframework.version>4.3.4.RELEASE</org.springframework.version>
|
|
||||||
<org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version>
|
|
||||||
|
|
||||||
<!-- persistence -->
|
|
||||||
<hibernate.version>5.2.5.Final</hibernate.version>
|
|
||||||
<mysql-connector-java.version>5.1.40</mysql-connector-java.version>
|
|
||||||
|
|
||||||
<!-- http -->
|
|
||||||
<httpcore.version>4.4.5</httpcore.version>
|
|
||||||
<httpclient.version>4.5.2</httpclient.version>
|
|
||||||
|
|
||||||
<!-- marshalling -->
|
|
||||||
<jackson.version>2.8.5</jackson.version>
|
|
||||||
|
|
||||||
<!-- various -->
|
|
||||||
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
|
||||||
<javax.servlet.version>3.1.0</javax.servlet.version>
|
|
||||||
<jstl.version>1.2</jstl.version>
|
|
||||||
|
|
||||||
<!-- util -->
|
|
||||||
<guava.version>19.0</guava.version>
|
|
||||||
<commons-lang3.version>3.5</commons-lang3.version>
|
|
||||||
|
|
||||||
<!-- testing -->
|
|
||||||
<rest-assured.version>2.9.0</rest-assured.version>
|
|
||||||
|
|
||||||
<!-- Maven plugins -->
|
|
||||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
|
||||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
|
||||||
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
</project>
|
|
|
@ -1,75 +0,0 @@
|
||||||
package org.baeldung.client;
|
|
||||||
|
|
||||||
import org.apache.http.HttpEntity;
|
|
||||||
import org.apache.http.HttpHost;
|
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.auth.AuthScope;
|
|
||||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
|
||||||
import org.apache.http.client.AuthCache;
|
|
||||||
import org.apache.http.client.CredentialsProvider;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
|
||||||
import org.apache.http.client.protocol.HttpClientContext;
|
|
||||||
import org.apache.http.impl.auth.DigestScheme;
|
|
||||||
import org.apache.http.impl.client.BasicAuthCache;
|
|
||||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClients;
|
|
||||||
import org.apache.http.protocol.BasicHttpContext;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An example of HttpClient can be customized to authenticate
|
|
||||||
* preemptively using DIGEST scheme.
|
|
||||||
* <b/>
|
|
||||||
* Generally, preemptive authentication can be considered less
|
|
||||||
* secure than a response to an authentication challenge
|
|
||||||
* and therefore discouraged.
|
|
||||||
*/
|
|
||||||
public class ClientPreemptiveDigestAuthentication {
|
|
||||||
|
|
||||||
public static void main(final String[] args) throws Exception {
|
|
||||||
final HttpHost targetHost = new HttpHost("localhost", 8080, "http");
|
|
||||||
|
|
||||||
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
||||||
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user1", "user1Pass"));
|
|
||||||
|
|
||||||
final CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
|
|
||||||
try {
|
|
||||||
|
|
||||||
// Create AuthCache instance
|
|
||||||
final AuthCache authCache = new BasicAuthCache();
|
|
||||||
// Generate DIGEST scheme object, initialize it and add it to the local auth cache
|
|
||||||
final DigestScheme digestAuth = new DigestScheme();
|
|
||||||
// Suppose we already know the realm name
|
|
||||||
digestAuth.overrideParamter("realm", "Custom Realm Name");
|
|
||||||
|
|
||||||
// digestAuth.overrideParamter("nonce", "whatever");
|
|
||||||
authCache.put(targetHost, digestAuth);
|
|
||||||
|
|
||||||
// Add AuthCache to the execution context
|
|
||||||
final BasicHttpContext localcontext = new BasicHttpContext();
|
|
||||||
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
|
|
||||||
|
|
||||||
final HttpGet httpget = new HttpGet("http://localhost:8080/spring-security-rest-digest-auth/api/foos/1");
|
|
||||||
|
|
||||||
System.out.println("executing request: " + httpget.getRequestLine());
|
|
||||||
System.out.println("to target: " + targetHost);
|
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
final HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
|
|
||||||
final HttpEntity entity = response.getEntity();
|
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
System.out.println(response.getStatusLine());
|
|
||||||
if (entity != null) {
|
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
|
||||||
}
|
|
||||||
EntityUtils.consume(entity);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
// When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
|
|
||||||
httpclient.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,66 +0,0 @@
|
||||||
package org.baeldung.client.spring;
|
|
||||||
|
|
||||||
import org.apache.http.HttpHost;
|
|
||||||
import org.apache.http.auth.AuthScope;
|
|
||||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
|
||||||
import org.apache.http.client.CredentialsProvider;
|
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
import org.baeldung.client.HttpComponentsClientHttpRequestFactoryDigestAuth;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class ClientConfig {
|
|
||||||
private static final String DEFAULT_USER = "user1";
|
|
||||||
private static final String DEFAULT_PASS = "user1Pass";
|
|
||||||
|
|
||||||
public ClientConfig() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
// beans
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public RestTemplate restTemplate() {
|
|
||||||
final HttpHost host = new HttpHost("localhost", 8080, "http");
|
|
||||||
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).useSystemProperties().build();
|
|
||||||
|
|
||||||
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client);
|
|
||||||
final RestTemplate restTemplate = new RestTemplate(requestFactory);
|
|
||||||
|
|
||||||
final int timeout = 5;
|
|
||||||
configureTimeouts(requestFactory, timeout);
|
|
||||||
|
|
||||||
return restTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
// util
|
|
||||||
|
|
||||||
private final void configureTimeouts(final HttpComponentsClientHttpRequestFactory requestFactory, final int timeout) {
|
|
||||||
final HttpClient httpClient = requestFactory.getHttpClient();
|
|
||||||
// - note: timeout via raw String parameters
|
|
||||||
// httpClient.getParams().setParameter("http.connection.timeout", timeout * 1000);
|
|
||||||
// httpClient.getParams().setParameter("http.socket.timeout", timeout * 1000);
|
|
||||||
|
|
||||||
// httpClient.getParams().setParameter("http.connection-manager.timeout", new Long(timeout * 1000));
|
|
||||||
// httpClient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000);
|
|
||||||
|
|
||||||
// - note: timeout via the API
|
|
||||||
// final HttpParams httpParams = httpClient.getParams();
|
|
||||||
// HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
|
|
||||||
// HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
private final CredentialsProvider provider() {
|
|
||||||
final CredentialsProvider provider = new BasicCredentialsProvider();
|
|
||||||
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
|
|
||||||
provider.setCredentials(AuthScope.ANY, credentials);
|
|
||||||
return provider;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,48 +0,0 @@
|
||||||
package org.baeldung.security;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
import org.springframework.security.core.Authentication;
|
|
||||||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
|
|
||||||
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
|
|
||||||
import org.springframework.security.web.savedrequest.RequestCache;
|
|
||||||
import org.springframework.security.web.savedrequest.SavedRequest;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
public class MySavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
|
|
||||||
|
|
||||||
private RequestCache requestCache = new HttpSessionRequestCache();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws ServletException, IOException {
|
|
||||||
final SavedRequest savedRequest = requestCache.getRequest(request, response);
|
|
||||||
|
|
||||||
if (savedRequest == null) {
|
|
||||||
super.onAuthenticationSuccess(request, response, authentication);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final String targetUrlParameter = getTargetUrlParameter();
|
|
||||||
if (isAlwaysUseDefaultTargetUrl() || (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
|
|
||||||
requestCache.removeRequest(request, response);
|
|
||||||
super.onAuthenticationSuccess(request, response, authentication);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
clearAuthenticationAttributes(request);
|
|
||||||
|
|
||||||
// Use the DefaultSavedRequest URL
|
|
||||||
// final String targetUrl = savedRequest.getRedirectUrl();
|
|
||||||
// logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
|
|
||||||
// getRedirectStrategy().sendRedirect(request, response, targetUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRequestCache(final RequestCache requestCache) {
|
|
||||||
this.requestCache = requestCache;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,23 +0,0 @@
|
||||||
package org.baeldung.security;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
import org.springframework.security.core.AuthenticationException;
|
|
||||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The Entry Point will not redirect to any sort of Login - it will return the 401
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public final class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException {
|
|
||||||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
package org.baeldung.spring;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.ImportResource;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@ImportResource({ "classpath:webSecurityConfig.xml" })
|
|
||||||
@ComponentScan("org.baeldung.security")
|
|
||||||
public class SecSecurityConfig {
|
|
||||||
|
|
||||||
public SecSecurityConfig() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,31 +0,0 @@
|
||||||
package org.baeldung.spring;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
|
||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@EnableWebMvc
|
|
||||||
@ComponentScan("org.baeldung.web")
|
|
||||||
public class WebConfig extends WebMvcConfigurerAdapter {
|
|
||||||
|
|
||||||
public WebConfig() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
// beans
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
|
|
||||||
super.configureMessageConverters(converters);
|
|
||||||
converters.add(new MappingJackson2HttpMessageConverter());
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,31 +0,0 @@
|
||||||
package org.baeldung.web.controller;
|
|
||||||
|
|
||||||
import org.baeldung.web.dto.Bar;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
|
|
||||||
@Controller
|
|
||||||
@RequestMapping(value = "/bars")
|
|
||||||
public class BarController {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ApplicationEventPublisher eventPublisher;
|
|
||||||
|
|
||||||
public BarController() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
// API
|
|
||||||
|
|
||||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
|
||||||
@ResponseBody
|
|
||||||
public Bar findOne(@PathVariable("id") final Long id) {
|
|
||||||
return new Bar();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
package org.baeldung.web.controller;
|
|
||||||
|
|
||||||
import org.baeldung.web.dto.Foo;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
|
|
||||||
@Controller
|
|
||||||
@RequestMapping(value = "/foos")
|
|
||||||
public class FooController {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ApplicationEventPublisher eventPublisher;
|
|
||||||
|
|
||||||
public FooController() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
// API
|
|
||||||
|
|
||||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
|
||||||
@ResponseBody
|
|
||||||
@PreAuthorize("hasRole('ROLE_USER')")
|
|
||||||
public Foo findOne(@PathVariable("id") final Long id) {
|
|
||||||
return new Foo();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
package org.baeldung.web.dto;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
|
||||||
|
|
||||||
@XmlRootElement
|
|
||||||
public class Bar implements Serializable {
|
|
||||||
|
|
||||||
public Bar() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
package org.baeldung.web.dto;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
|
||||||
|
|
||||||
@XmlRootElement
|
|
||||||
public class Foo implements Serializable {
|
|
||||||
|
|
||||||
public Foo() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<configuration>
|
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
|
||||||
<encoder>
|
|
||||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
|
||||||
</pattern>
|
|
||||||
</encoder>
|
|
||||||
</appender>
|
|
||||||
|
|
||||||
<logger name="org.springframework" level="WARN" />
|
|
||||||
<logger name="org.springframework.transaction" level="WARN" />
|
|
||||||
|
|
||||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
|
||||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
|
||||||
|
|
||||||
<root level="INFO">
|
|
||||||
<appender-ref ref="STDOUT" />
|
|
||||||
</root>
|
|
||||||
</configuration>
|
|
|
@ -1,34 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
|
|
||||||
xsi:schemaLocation="
|
|
||||||
http://www.springframework.org/schema/security
|
|
||||||
http://www.springframework.org/schema/security/spring-security.xsd
|
|
||||||
http://www.springframework.org/schema/beans
|
|
||||||
http://www.springframework.org/schema/beans/spring-beans.xsd"
|
|
||||||
>
|
|
||||||
|
|
||||||
<beans:bean id="digestFilter" class="org.springframework.security.web.authentication.www.DigestAuthenticationFilter">
|
|
||||||
<beans:property name="userDetailsService" ref="userService"/>
|
|
||||||
<beans:property name="authenticationEntryPoint" ref="digestEntryPoint"/>
|
|
||||||
</beans:bean>
|
|
||||||
<beans:bean id="digestEntryPoint" class="org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint">
|
|
||||||
<beans:property name="realmName" value="Custom Realm Name"/>
|
|
||||||
<beans:property name="key" value="uniqueAndSecret"/>
|
|
||||||
</beans:bean>
|
|
||||||
|
|
||||||
<http use-expressions="true" create-session="stateless" entry-point-ref="digestEntryPoint">
|
|
||||||
<custom-filter ref="digestFilter" after="BASIC_AUTH_FILTER"/>
|
|
||||||
</http>
|
|
||||||
|
|
||||||
<authentication-manager>
|
|
||||||
<authentication-provider>
|
|
||||||
<user-service id="userService">
|
|
||||||
<user name="user1" password="user1Pass" authorities="ROLE_USER"/>
|
|
||||||
<user name="admin1" password="admin1Pass" authorities="ROLE_ADMIN"/>
|
|
||||||
</user-service>
|
|
||||||
</authentication-provider>
|
|
||||||
</authentication-manager>
|
|
||||||
|
|
||||||
<global-method-security pre-post-annotations="enabled"/>
|
|
||||||
|
|
||||||
</beans:beans>
|
|
|
@ -1,6 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd" >
|
|
||||||
|
|
||||||
</beans>
|
|
|
@ -1,52 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
|
||||||
xsi:schemaLocation="
|
|
||||||
http://java.sun.com/xml/ns/javaee
|
|
||||||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"
|
|
||||||
>
|
|
||||||
|
|
||||||
<display-name>Spring Security Custom Application</display-name>
|
|
||||||
|
|
||||||
<!-- Spring root -->
|
|
||||||
<context-param>
|
|
||||||
<param-name>contextClass</param-name>
|
|
||||||
<param-value>
|
|
||||||
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
|
||||||
</param-value>
|
|
||||||
</context-param>
|
|
||||||
<context-param>
|
|
||||||
<param-name>contextConfigLocation</param-name>
|
|
||||||
<param-value>org.baeldung.spring</param-value>
|
|
||||||
</context-param>
|
|
||||||
|
|
||||||
<listener>
|
|
||||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
|
||||||
</listener>
|
|
||||||
|
|
||||||
<!-- Spring child -->
|
|
||||||
<servlet>
|
|
||||||
<servlet-name>api</servlet-name>
|
|
||||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
|
||||||
<load-on-startup>1</load-on-startup>
|
|
||||||
</servlet>
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>api</servlet-name>
|
|
||||||
<url-pattern>/api/*</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Spring Security -->
|
|
||||||
<filter>
|
|
||||||
<filter-name>springSecurityFilterChain</filter-name>
|
|
||||||
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
|
|
||||||
</filter>
|
|
||||||
<filter-mapping>
|
|
||||||
<filter-name>springSecurityFilterChain</filter-name>
|
|
||||||
<url-pattern>/*</url-pattern>
|
|
||||||
</filter-mapping>
|
|
||||||
|
|
||||||
<!-- <welcome-file-list> -->
|
|
||||||
<!-- <welcome-file>index.html</welcome-file> -->
|
|
||||||
<!-- </welcome-file-list> -->
|
|
||||||
|
|
||||||
</web-app>
|
|
|
@ -1,64 +0,0 @@
|
||||||
package org.baeldung.client;
|
|
||||||
|
|
||||||
import org.apache.http.HttpHost;
|
|
||||||
import org.apache.http.auth.AuthScope;
|
|
||||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
|
||||||
import org.apache.http.client.CredentialsProvider;
|
|
||||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
import org.baeldung.web.dto.Foo;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
public class ClientNoSpringLiveTest {
|
|
||||||
private static final String DEFAULT_USER = "user1";
|
|
||||||
private static final String DEFAULT_PASS = "user1Pass";
|
|
||||||
|
|
||||||
// tests - no Spring
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenUsingCustomHttpRequestFactory_whenSecuredRestApiIsConsumed_then200OK() {
|
|
||||||
final HttpHost host = new HttpHost("localhost", 8082, "http");
|
|
||||||
|
|
||||||
final CredentialsProvider credentialsProvider = provider();
|
|
||||||
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build();
|
|
||||||
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client);
|
|
||||||
final RestTemplate restTemplate = new RestTemplate(requestFactory);
|
|
||||||
|
|
||||||
// credentialsProvider.setCredentials(new AuthScope("localhost", 8082, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
|
|
||||||
|
|
||||||
final String uri = "http://localhost:8082/spring-security-rest-digest-auth/api/foos/1";
|
|
||||||
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
|
|
||||||
|
|
||||||
System.out.println(responseEntity.getStatusCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenUsingStandardRequestFactory_whenSecuredRestApiIsConsumed_then200OK() {
|
|
||||||
final CredentialsProvider credentialsProvider = provider();
|
|
||||||
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build();
|
|
||||||
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);
|
|
||||||
final RestTemplate restTemplate = new RestTemplate(requestFactory);
|
|
||||||
|
|
||||||
// credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
|
|
||||||
|
|
||||||
final String uri = "http://localhost:8082/spring-security-rest-digest-auth/api/foos/1";
|
|
||||||
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
|
|
||||||
|
|
||||||
System.out.println(responseEntity.getStatusCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
// UTIL
|
|
||||||
|
|
||||||
private final CredentialsProvider provider() {
|
|
||||||
final CredentialsProvider provider = new BasicCredentialsProvider();
|
|
||||||
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
|
|
||||||
provider.setCredentials(AuthScope.ANY, credentials);
|
|
||||||
return provider;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
package org.baeldung.client;
|
|
||||||
|
|
||||||
import org.baeldung.client.spring.ClientConfig;
|
|
||||||
import org.baeldung.web.dto.Foo;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
|
||||||
@ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
|
||||||
public class ClientWithSpringLiveTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private RestTemplate restTemplate;
|
|
||||||
|
|
||||||
// tests - no Spring
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void whenSecuredRestApiIsConsumed_then200OK() {
|
|
||||||
final String uri = "http://localhost:8082/spring-security-rest-digest-auth/api/foos/1";
|
|
||||||
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
|
|
||||||
|
|
||||||
System.out.println(responseEntity.getStatusCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,18 +0,0 @@
|
||||||
package org.baeldung.test;
|
|
||||||
|
|
||||||
import org.baeldung.client.ClientNoSpringLiveTest;
|
|
||||||
import org.baeldung.client.ClientWithSpringLiveTest;
|
|
||||||
import org.baeldung.client.RawClientLiveTest;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.junit.runners.Suite;
|
|
||||||
|
|
||||||
@RunWith(Suite.class)
|
|
||||||
@Suite.SuiteClasses({
|
|
||||||
// @formatter:off
|
|
||||||
RawClientLiveTest.class
|
|
||||||
,ClientWithSpringLiveTest.class
|
|
||||||
,ClientNoSpringLiveTest.class
|
|
||||||
}) //
|
|
||||||
public class LiveTestSuite {
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
*.class
|
|
||||||
|
|
||||||
#folders#
|
|
||||||
/target
|
|
||||||
/neoDb*
|
|
||||||
/data
|
|
||||||
/src/main/webapp/WEB-INF/classes
|
|
||||||
*/META-INF/*
|
|
||||||
|
|
||||||
# Packaged files #
|
|
||||||
*.jar
|
|
||||||
*.war
|
|
||||||
*.ear
|
|
Loading…
Reference in New Issue