Skip to content

Commit 8f59096

Browse files
committed
add security configuration
1 parent 2f4fcc0 commit 8f59096

9 files changed

Lines changed: 192 additions & 20 deletions

File tree

‎spring-cloud/spring-cloud-rest/spring-cloud-rest-client/pom.xml‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
1717
<version>1.4.3.RELEASE</version>
18-
<relativePath/> <!-- lookup parent from repository -->
18+
<relativePath /> <!-- lookup parent from repository -->
1919
</parent>
2020

2121
<properties>
@@ -27,37 +27,49 @@
2727

2828
<dependencies>
2929
<dependency>
30-
<groupId>org.springframework.cloud</groupId>
31-
<artifactId>spring-cloud-starter-eureka</artifactId>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-starter-eureka</artifactId>
3232
</dependency>
3333
<dependency>
3434
<groupId>org.springframework.boot</groupId>
3535
<artifactId>spring-boot-starter-data-jpa</artifactId>
3636
</dependency>
3737
<dependency>
38-
<groupId>com.h2database</groupId>
39-
<artifactId>h2</artifactId>
38+
<groupId>com.h2database</groupId>
39+
<artifactId>h2</artifactId>
4040
</dependency>
4141
<dependency>
4242
<groupId>org.springframework.boot</groupId>
4343
<artifactId>spring-boot-starter-web</artifactId>
4444
</dependency>
4545
<dependency>
46-
<groupId>org.springframework.boot</groupId>
47-
<artifactId>spring-boot-starter-data-rest</artifactId>
48-
</dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-data-rest</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-security</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.session</groupId>
55+
<artifactId>spring-session</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-data-redis</artifactId>
60+
</dependency>
4961

5062
<dependency>
5163
<groupId>org.springframework.boot</groupId>
5264
<artifactId>spring-boot-starter-test</artifactId>
5365
<scope>test</scope>
5466
</dependency>
5567
<dependency>
56-
<groupId>io.rest-assured</groupId>
57-
<artifactId>rest-assured</artifactId>
58-
<version>${rest-assured.version}</version>
68+
<groupId>io.rest-assured</groupId>
69+
<artifactId>rest-assured</artifactId>
70+
<version>${rest-assured.version}</version>
5971
</dependency>
60-
72+
6173
</dependencies>
6274

6375
<dependencyManagement>
@@ -78,6 +90,16 @@
7890
<groupId>org.springframework.boot</groupId>
7991
<artifactId>spring-boot-maven-plugin</artifactId>
8092
</plugin>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-surefire-plugin</artifactId>
96+
<configuration>
97+
<excludes>
98+
<exclude>**/*IntegrationTest.java</exclude>
99+
<exclude>**/*LiveTest.java</exclude>
100+
</excludes>
101+
</configuration>
102+
</plugin>
81103
</plugins>
82104
</build>
83105

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.baeldung;
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+
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
#### cloud
12
spring.application.name=spring-cloud-eureka-client
23
server.port=0
3-
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}
4+
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://system:systemPass@localhost:8761/eureka}
45
eureka.instance.preferIpAddress=true
56

7+
#### persistence
68
spring.datasource.driver-class-name=org.h2.Driver
79
spring.datasource.url=jdbc:h2:mem:cloud_rest;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
810
spring.datasource.username=sa
9-
spring.datasource.password=
11+
spring.datasource.password=
12+
13+
#### security
14+
security.basic.enabled=true
15+
security.basic.path=/**
16+
security.user.name=user
17+
security.user.password=userPass
18+
security.user.role=USER
19+
security.sessions=always

‎spring-cloud/spring-cloud-rest/spring-cloud-rest-client/src/test/java/org/baeldung/RestApiLiveTest.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.baeldung;
22

3+
import static io.restassured.RestAssured.preemptive;
34
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
45
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
56
import static org.junit.Assert.assertEquals;
@@ -8,6 +9,7 @@
89
import io.restassured.response.Response;
910

1011
import org.baeldung.persistence.model.Book;
12+
import org.junit.Before;
1113
import org.junit.Test;
1214
import org.junit.runner.RunWith;
1315
import org.springframework.boot.test.context.SpringBootTest;
@@ -22,6 +24,11 @@ public class RestApiLiveTest {
2224

2325
private static final String API_URI = "http://localhost:8084/books";
2426

27+
@Before
28+
public void setUp() {
29+
RestAssured.authentication = preemptive().basic("user", "userPass");
30+
}
31+
2532
// GET
2633

2734
@Test
@@ -148,19 +155,20 @@ public void whenDeleteNotExistBook_thenError() {
148155

149156
// =============================== Util
150157

151-
public Book createRandomBook() {
158+
private Book createRandomBook() {
152159
final Book book = new Book();
153160
book.setTitle(randomAlphabetic(10));
154161
book.setAuthor(randomAlphabetic(15));
155162
return book;
156163
}
157164

158-
public String createBookAsUri(Book book) {
165+
private String createBookAsUri(Book book) {
159166
final Response response = RestAssured.given()
160167
.contentType(MediaType.APPLICATION_JSON_VALUE)
161168
.body(book)
162169
.post(API_URI);
163170
return response.jsonPath()
164171
.get("_links.self.href");
165172
}
173+
166174
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.baeldung;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import io.restassured.RestAssured;
6+
import io.restassured.response.Response;
7+
8+
import java.util.Set;
9+
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
15+
import org.springframework.http.HttpStatus;
16+
import org.springframework.test.context.junit4.SpringRunner;
17+
18+
import redis.clients.jedis.Jedis;
19+
20+
@RunWith(SpringRunner.class)
21+
@SpringBootTest(classes = { SpringCloudRestClientApplication.class, SessionConfig.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
22+
public class SessionLiveTest {
23+
24+
private Jedis jedis;
25+
private static final String API_URI = "http://localhost:8084/books";
26+
27+
@Before
28+
public void setUp() {
29+
jedis = new Jedis("localhost", 6379);
30+
jedis.flushAll();
31+
}
32+
33+
@Test
34+
public void whenStart_thenNoSessionsExist() {
35+
final Set<String> result = jedis.keys("*");
36+
assertEquals(0, result.size());
37+
}
38+
39+
@Test
40+
public void givenUnauthorizeUser_whenAccessResources_then_unAuthorized() {
41+
final Response response = RestAssured.get(API_URI);
42+
assertEquals(HttpStatus.UNAUTHORIZED.value(), response.getStatusCode());
43+
}
44+
45+
@Test
46+
public void givenAuthorizedUser_whenDeleteSession_thenUnauthorized() {
47+
// authorize User
48+
Response response = RestAssured.given()
49+
.auth()
50+
.preemptive()
51+
.basic("user", "userPass")
52+
.get(API_URI);
53+
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
54+
final String sessionCookie = response.getCookie("SESSION");
55+
56+
// check redis
57+
final Set<String> redisResult = jedis.keys("*");
58+
assertTrue(redisResult.size() > 0);
59+
60+
// login with cookie
61+
response = RestAssured.given()
62+
.cookie("SESSION", sessionCookie)
63+
.get(API_URI);
64+
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
65+
66+
// empty redis
67+
jedis.flushAll();
68+
69+
// login with cookie again
70+
response = RestAssured.given()
71+
.cookie("SESSION", sessionCookie)
72+
.get(API_URI);
73+
assertEquals(HttpStatus.UNAUTHORIZED.value(), response.getStatusCode());
74+
}
75+
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
#### cloud
12
spring.application.name=spring-cloud-eureka-client
23
server.port=8084
3-
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}
4+
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://system:systemPass@localhost:8761/eureka}
45
eureka.instance.preferIpAddress=true
56

7+
#### persistence
68
spring.datasource.driver-class-name=org.h2.Driver
79
spring.datasource.url=jdbc:h2:mem:cloud_rest;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
810
spring.datasource.username=sa
9-
spring.datasource.password=
11+
spring.datasource.password=
12+
13+
#### security
14+
security.basic.enabled=true
15+
security.basic.path=/**
16+
security.user.name=user
17+
security.user.password=userPass
18+
security.user.role=USER
19+
security.sessions=always

‎spring-cloud/spring-cloud-rest/spring-cloud-rest-server/pom.xml‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,19 @@
3030
<groupId>org.springframework.cloud</groupId>
3131
<artifactId>spring-cloud-starter-eureka-server</artifactId>
3232
</dependency>
33-
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-security</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.session</groupId>
39+
<artifactId>spring-session</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-data-redis</artifactId>
44+
</dependency>
45+
3446
<dependency>
3547
<groupId>org.springframework.boot</groupId>
3648
<artifactId>spring-boot-starter-test</artifactId>
@@ -56,6 +68,16 @@
5668
<groupId>org.springframework.boot</groupId>
5769
<artifactId>spring-boot-maven-plugin</artifactId>
5870
</plugin>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-surefire-plugin</artifactId>
74+
<configuration>
75+
<excludes>
76+
<exclude>**/*IntegrationTest.java</exclude>
77+
<exclude>**/*LiveTest.java</exclude>
78+
</excludes>
79+
</configuration>
80+
</plugin>
5981
</plugins>
6082
</build>
6183

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.baeldung;
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+
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
#### cloud
12
server.port=8761
23
eureka.client.registerWithEureka=false
3-
eureka.client.fetchRegistry=false
4+
eureka.client.fetchRegistry=false
5+
6+
#### security
7+
security.basic.enabled=true
8+
security.basic.path=/**
9+
security.user.name=system
10+
security.user.password=systemPass
11+
security.user.role=ADMIN
12+
security.sessions=always

0 commit comments

Comments
 (0)