Skip to content

Commit 953bea2

Browse files
committed
Reticulating splines... (C) whatthecommit.com
1 parent 349b11e commit 953bea2

File tree

15 files changed

+405
-9
lines changed

15 files changed

+405
-9
lines changed

.github/workflows/ci.yml

+64-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: CI
22
on: [push]
33
env:
4-
CI: 'true'
4+
SPRING_PROFILES_ACTIVE: ci
55
jobs:
66
step-0-application-without-security:
77
strategy:
@@ -57,13 +57,74 @@ jobs:
5757
with:
5858
java-version: ${{ matrix.java }}
5959
- run: command -v docker >/dev/null 2>&1 || { echo >&2 "I require docker but it's not installed. Aborting."; exit 1; }
60-
#- run: type docker >/dev/null 2>&1 || { echo >&2 "I require docker but it's not installed. Aborting."; exit 1; }
6160
#- run: hash docker 2>/dev/null || { echo >&2 "I require docker but it's not installed. Aborting."; exit 1; }
6261
- run: sudo apt-get install -y httpie
6362
- run: npm i -g wait-port
6463
- run: cd $GITHUB_WORKSPACE && ./mvnw -f step-0-application-without-security
6564
- run: ( bash $GITHUB_WORKSPACE/step-0-application-without-security/target/*jar --spring.profiles.active=ci & ) || echo ...
6665
- run: wait-port 8080
6766
- run: http get :8080
68-
- run: cd $GITHUB_WORKSPACE && ./mvnw -f step-0-test-application-without-security -Dgroups=e2e -Pe2e
67+
- run: cd $GITHUB_WORKSPACE && ./mvnw -f step-0-test-application-without-security -Dgroups=e2e
6968
- run: http --ignore-stdin post :8080/actuator/shutdown
69+
step-1-application-with-default-spring-security:
70+
strategy:
71+
matrix:
72+
java: [11]
73+
os: [ubuntu-latest]
74+
runs-on: ${{ matrix.os }}
75+
name: java-${{ matrix.java }} step-1-application-with-default-spring-security
76+
steps:
77+
- uses: actions/checkout@v1
78+
- uses: actions/cache@v1
79+
with:
80+
path: ~/.m2
81+
key: ${{ runner.os }}-maven-${{ hashFiles('**/mvnw') }}
82+
restore-keys: |
83+
${{ runner.os }}-docker-
84+
${{ runner.os }}-maven-
85+
${{ runner.os }}-node-
86+
${{ runner.os }}-npm-
87+
${{ runner.os }}-
88+
- uses: actions/cache@v1
89+
with:
90+
path: ~/.docker
91+
key: ${{ runner.os }}-docker-${{ hashFiles('**/mvnw') }}
92+
restore-keys: |
93+
${{ runner.os }}-docker-
94+
${{ runner.os }}-maven-
95+
${{ runner.os }}-node-
96+
${{ runner.os }}-npm-
97+
${{ runner.os }}-
98+
- uses: actions/cache@v1
99+
with:
100+
path: ~/.npm
101+
key: ${{ runner.os }}-npm-${{ hashFiles('**/mvnw') }}
102+
restore-keys: |
103+
${{ runner.os }}-docker-
104+
${{ runner.os }}-maven-
105+
${{ runner.os }}-node-
106+
${{ runner.os }}-npm-
107+
${{ runner.os }}-
108+
- uses: actions/cache@v1
109+
with:
110+
path: ~/.node
111+
key: ${{ runner.os }}-maven-${{ hashFiles('**/mvnw') }}
112+
restore-keys: |
113+
${{ runner.os }}-docker-
114+
${{ runner.os }}-maven-
115+
${{ runner.os }}-node-
116+
${{ runner.os }}-npm-
117+
${{ runner.os }}-
118+
- uses: actions/setup-node@v1
119+
- uses: actions/setup-java@v1
120+
with:
121+
java-version: ${{ matrix.java }}
122+
- run: type docker >/dev/null 2>&1 || { echo >&2 "I require docker but it's not installed. Aborting."; exit 1; }
123+
- run: sudo apt-get install -y httpie
124+
- run: npm i -g wait-port
125+
- run: cd $GITHUB_WORKSPACE && ./mvnw -f step-1-application-with-default-spring-security
126+
- run: ( bash $GITHUB_WORKSPACE/step-1-application-with-default-spring-security/target/*jar & ) || echo ...
127+
- run: wait-port 8080
128+
- run: http get :8080
129+
- run: cd $GITHUB_WORKSPACE && ./mvnw -f step-1-test-application-with-default-spring-security -Dgroups=e2e -Pci
130+
- run: http --ignore-stdin -a user:pwd post :8080/actuator/shutdown

.idea/runConfigurations/Step_1_app.xml

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/Step_2_test.xml

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+61-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ Learn Spring Security by baby steps from zero to pro!
33

44
## Table of Content
55
* [Step 0: No security](#step-0)
6+
* [Step 1: Add authentication](#step-1)
67
* [Versioning and releasing](#maven)
78
* [Resources and used links](#resources)
89

910
## step: 0
1011

11-
let's use simple spring boot web app
12+
let's use simple spring boot web app without security at all!
1213

1314
### application
1415

@@ -57,10 +58,12 @@ finally, to gracefully shutdown application under test on CI builds,
5758
add actuator dependency:
5859

5960
```xml
61+
<dependencies>
6062
<dependency>
6163
<groupId>org.springframework.boot</groupId>
6264
<artifactId>spring-boot-starter-actuator</artifactId>
6365
</dependency>
66+
</dependencies>
6467
```
6568

6669
with according configurations in `application.yaml` file:
@@ -113,11 +116,12 @@ class AppTest extends AbstractTest {
113116

114117
@Test
115118
void test() {
116-
open("http://127.0.0.1:8080");
117-
var h1 = $("h1");
119+
open("http://127.0.0.1:8080"); // open home page...
120+
var h1 = $("h1"); // find there <h1> tag...
118121
log.info("h1 html: {}", h1);
119-
h1.shouldBe(exist, visible)
120-
.shouldHave(text("hello"));
122+
h1.shouldBe(exist, visible) // element should be inside DOM
123+
.shouldHave(text("hello")); // textContent of the tag should
124+
// contains expected content...
121125
}
122126
}
123127
```
@@ -133,6 +137,58 @@ java -jar ./step-0-application-without-security/target/*jar --spring.profiles.ac
133137
http post :8080/actuator/shutdown
134138
```
135139

140+
## step: 1
141+
142+
in this step we are going to implement simple authentication.
143+
it's mean everyone who logged in, can access all available
144+
resources.
145+
146+
### application
147+
148+
add required dependencies:
149+
150+
```xml
151+
<dependencies>
152+
<dependency>
153+
<groupId>org.springframework.boot</groupId>
154+
<artifactId>spring-boot-starter-security</artifactId>
155+
</dependency>
156+
</dependencies>
157+
```
158+
159+
update `application.yaml` configuration with desired user password:
160+
161+
```yaml
162+
spring:
163+
security:
164+
user:
165+
password: pwd
166+
```
167+
168+
### test application
169+
170+
now, let's update test according to configured security as follows:
171+
172+
```java
173+
@Log4j2
174+
@AllArgsConstructor
175+
class AppTest extends AbstractTest {
176+
177+
@Test
178+
void test() {
179+
open("http://127.0.0.1:8080");
180+
// we should be redirected to login page, so lets authenticate!
181+
$("#username").setValue("user");
182+
$("#password").setValue("pwd").submit();
183+
// everything else is with no changes...
184+
var h1 = $("h1");
185+
log.info("h1 html: {}", h1);
186+
h1.shouldBe(exist, visible)
187+
.shouldHave(text("hello"));
188+
}
189+
}
190+
```
191+
136192
## maven
137193

138194
we will be releasing after each important step! so it will be easy simply checkout needed version from git tag.

pom.xml

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<modules>
1212
<module>step-0-application-without-security</module>
1313
<module>step-0-test-application-without-security</module>
14+
<module>step-1-application-with-default-spring-security</module>
15+
<module>step-1-test-application-with-default-spring-security</module>
1416
</modules>
1517
<properties>
1618
<encoding>UTF-8</encoding>
@@ -164,7 +166,13 @@
164166
</build>
165167
<profiles>
166168
<profile>
167-
<id>e2e</id>
169+
<id>ci</id>
170+
<activation>
171+
<property>
172+
<name>env.SPRING_PROFILES_ACTIVE</name>
173+
<value>ci</value>
174+
</property>
175+
</activation>
168176
<build>
169177
<plugins>
170178
<plugin>

step-0-application-without-security/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<parent>
66
<artifactId>spring-security-basics</artifactId>
77
<groupId>com.giuthub.daggerok</groupId>
8+
<relativePath>..</relativePath>
89
<version>1.0.1</version>
910
</parent>
1011
<packaging>jar</packaging>

step-0-test-application-without-security/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<parent>
66
<artifactId>spring-security-basics</artifactId>
77
<groupId>com.giuthub.daggerok</groupId>
8+
<relativePath>..</relativePath>
89
<version>1.0.1</version>
910
</parent>
1011
<packaging>jar</packaging>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-security-basics</artifactId>
7+
<groupId>com.giuthub.daggerok</groupId>
8+
<relativePath>..</relativePath>
9+
<version>1.0.1</version>
10+
</parent>
11+
<packaging>jar</packaging>
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>step-1-application-with-default-spring-security</artifactId>
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-web</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-actuator</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-security</artifactId>
26+
</dependency>
27+
</dependencies>
28+
<build>
29+
<plugins>
30+
<plugin>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-maven-plugin</artifactId>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package daggerok;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
5+
import org.springframework.boot.actuate.context.ShutdownEndpoint;
6+
import org.springframework.boot.actuate.health.HealthEndpoint;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
9+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
10+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
11+
import org.springframework.stereotype.Controller;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
14+
@Controller
15+
class IndexPage {
16+
17+
@GetMapping("/")
18+
String index() {
19+
return "index.html";
20+
}
21+
}
22+
23+
@EnableWebSecurity
24+
class MyWebSecurity extends WebSecurityConfigurerAdapter {
25+
@Override
26+
protected void configure(HttpSecurity http) throws Exception {
27+
http.authorizeRequests()
28+
.requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
29+
.anyRequest().authenticated()//.fullyAuthenticated()//
30+
.and()
31+
.csrf().disable()
32+
.formLogin()
33+
;
34+
}
35+
}
36+
37+
@SpringBootApplication
38+
public class App {
39+
public static void main(String[] args) {
40+
SpringApplication.run(App.class, args);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
spring:
2+
security:
3+
user:
4+
password: pwd
5+
output:
6+
ansi:
7+
enabled: always
8+
---
9+
spring:
10+
profiles: ci
11+
management:
12+
endpoint:
13+
shutdown:
14+
enabled: true
15+
endpoints:
16+
web:
17+
exposure:
18+
include: >
19+
shutdown
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Home page | spring-security baby-steps</title>
8+
</head>
9+
<body>
10+
<h1>Hello!</h1>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)