Skip to content

Commit 2a7b598

Browse files
committed
BAEL-315 Adding spring session as mechanism for sharing authentication tokens created by the edge service.
1 parent a310184 commit 2a7b598

19 files changed

Lines changed: 355 additions & 7 deletions

File tree

‎spring-cloud/spring-cloud-bootstrap/application-config/discovery.properties‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ eureka.instance.hostname=localhost
66
eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/
77
eureka.client.register-with-eureka=false
88
eureka.client.fetch-registry=false
9+
10+
spring.redis.host=localhost
11+
spring.redis.port=6379

‎spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ eureka.client.region = default
55
eureka.client.registryFetchIntervalSeconds = 5
66

77
zuul.routes.resource.path=/resource/**
8+
zuul.routes.resource.sensitiveHeaders=Set-Cookie,Authorization,Cookie
89
hystrix.command.resource.execution.isolation.thread.timeoutInMilliseconds: 5000
910

11+
spring.redis.host=localhost
12+
spring.redis.port=6379
13+

‎spring-cloud/spring-cloud-bootstrap/application-config/resource.properties‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ spring.application.name=resource
22
server.port=8083
33

44
resource.returnString=hello cloud
5+
resource.user.returnString=hello cloud user
6+
resource.admin.returnString=hello cloud admin
57

68
eureka.client.region = default
79
eureka.client.registryFetchIntervalSeconds = 5
10+
11+
spring.redis.host=localhost
12+
spring.redis.port=6379

‎spring-cloud/spring-cloud-bootstrap/config/pom.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
<groupId>org.springframework.boot</groupId>
5353
<artifactId>spring-boot-maven-plugin</artifactId>
5454
</plugin>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-compiler-plugin</artifactId>
58+
<version>3.3</version>
59+
<configuration>
60+
<source>1.8</source>
61+
<target>1.8</target>
62+
</configuration>
63+
</plugin>
5564
</plugins>
5665
</build>
5766

‎spring-cloud/spring-cloud-bootstrap/discovery/pom.xml‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
<groupId>org.springframework.boot</groupId>
3333
<artifactId>spring-boot-starter-security</artifactId>
3434
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework.session</groupId>
38+
<artifactId>spring-session</artifactId>
39+
<version>1.2.1.RELEASE</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-data-redis</artifactId>
44+
</dependency>
3545
</dependencies>
3646

3747
<dependencyManagement>
@@ -52,6 +62,15 @@
5262
<groupId>org.springframework.boot</groupId>
5363
<artifactId>spring-boot-maven-plugin</artifactId>
5464
</plugin>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-compiler-plugin</artifactId>
68+
<version>3.3</version>
69+
<configuration>
70+
<source>1.8</source>
71+
<target>1.8</target>
72+
</configuration>
73+
</plugin>
5574
</plugins>
5675
</build>
5776
</project>

‎spring-cloud/spring-cloud-bootstrap/discovery/src/main/java/com/baeldung/spring/cloud/bootstrap/discovery/SecurityConfig.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
1717
auth.inMemoryAuthentication()
1818
.withUser("discUser")
1919
.password("discPassword")
20-
.roles("SYSTEM")
21-
.and()
22-
.withUser("admin")
23-
.password("password")
24-
.roles("ADMIN");
20+
.roles("SYSTEM");
2521
}
2622

2723
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.spring.cloud.bootstrap.discovery;
2+
3+
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
4+
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
5+
6+
@EnableRedisHttpSession
7+
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
8+
}

‎spring-cloud/spring-cloud-bootstrap/gateway/pom.xml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@
2727
<groupId>org.springframework.cloud</groupId>
2828
<artifactId>spring-cloud-starter-zuul</artifactId>
2929
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-security</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.session</groupId>
37+
<artifactId>spring-session</artifactId>
38+
<version>1.2.1.RELEASE</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-data-redis</artifactId>
43+
</dependency>
44+
3045
<dependency>
3146
<groupId>org.springframework.boot</groupId>
3247
<artifactId>spring-boot-starter-test</artifactId>
@@ -52,6 +67,15 @@
5267
<groupId>org.springframework.boot</groupId>
5368
<artifactId>spring-boot-maven-plugin</artifactId>
5469
</plugin>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-compiler-plugin</artifactId>
73+
<version>3.3</version>
74+
<configuration>
75+
<source>1.8</source>
76+
<target>1.8</target>
77+
</configuration>
78+
</plugin>
5579
</plugins>
5680
</build>
5781
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.spring.cloud.bootstrap.gateway;
2+
3+
import org.springframework.security.core.Authentication;
4+
import org.springframework.security.web.WebAttributes;
5+
import org.springframework.security.web.authentication.AbstractAuthenticationTargetUrlRequestHandler;
6+
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
7+
8+
import javax.servlet.ServletException;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
import javax.servlet.http.HttpSession;
12+
import java.io.IOException;
13+
14+
public class CustomAuthenticationHandler extends AbstractAuthenticationTargetUrlRequestHandler implements AuthenticationSuccessHandler{
15+
16+
public CustomAuthenticationHandler() {
17+
}
18+
19+
/**
20+
* Constructor which sets the <tt>defaultTargetUrl</tt> property of the base class.
21+
* @param defaultTargetUrl the URL to which the user should be redirected on
22+
* successful authentication.
23+
*/
24+
public CustomAuthenticationHandler(String defaultTargetUrl) {
25+
setDefaultTargetUrl(defaultTargetUrl);
26+
}
27+
28+
/**
29+
* Calls the parent class {@code handle()} method to forward or redirect to the target
30+
* URL, and then calls {@code clearAuthenticationAttributes()} to remove any leftover
31+
* session data.
32+
*/
33+
public void onAuthenticationSuccess(HttpServletRequest request,
34+
HttpServletResponse response, Authentication authentication)
35+
throws IOException, ServletException {
36+
response.setStatus(303);
37+
handle(request, response, authentication);
38+
clearAuthenticationAttributes(request);
39+
}
40+
41+
/**
42+
* Removes temporary authentication-related data which may have been stored in the
43+
* session during the authentication process.
44+
*/
45+
protected final void clearAuthenticationAttributes(HttpServletRequest request) {
46+
HttpSession session = request.getSession(false);
47+
48+
if (session == null) {
49+
return;
50+
}
51+
52+
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
53+
}
54+
}

‎spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplication.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package com.baeldung.spring.cloud.bootstrap.gateway;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
57
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
8+
import org.springframework.cloud.netflix.ribbon.RibbonClientSpecification;
9+
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
610
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
716

817
@SpringBootApplication
918
@EnableZuulProxy
@@ -12,4 +21,20 @@ public class GatewayApplication {
1221
public static void main(String[] args) {
1322
SpringApplication.run(GatewayApplication.class, args);
1423
}
24+
25+
@Autowired(required = false)
26+
private List<RibbonClientSpecification> configurations = new ArrayList<>();
27+
28+
@Bean
29+
@LoadBalanced RestTemplate restTemplate(){
30+
return new RestTemplate();
31+
}
32+
33+
34+
@Bean
35+
public SpringClientFactory springClientFactory() {
36+
SpringClientFactory factory = new SpringClientFactory();
37+
factory.setConfigurations(this.configurations);
38+
return factory;
39+
}
1540
}

0 commit comments

Comments
 (0)