Skip to content

Commit acf7ec2

Browse files
First version of secured demo-server, keystore Makefile included.
1 parent 7bcfe24 commit acf7ec2

7 files changed

Lines changed: 199 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
PASSWORD=changeit
2+
KEYSTORE=keystore.jks
3+
HOSTNAME=localhost
4+
# CN = Common Name
5+
# OU = Organization Unit
6+
# O = Organization Name
7+
# L = Locality Name
8+
# ST = State Name
9+
# C = Country (2-letter Country Code)
10+
# E = Email
11+
DNAME_CA='CN=Baeldung CA,OU=baeldung.com,O=Baeldung,L=SomeCity,ST=SomeState,C=CC'
12+
# For server certificates, the Common Name (CN) must be the hostname
13+
DNAME_HOST='CN=$(HOSTNAME),OU=baeldung.com,O=Baeldung,L=SomeCity,ST=SomeState,C=CC'
14+
TRUSTSTORE=truststore.jks
15+
CLIENTNAME=cid
16+
17+
all: clean create-keystore add-host create-truststore add-client
18+
19+
create-keystore:
20+
# Generate a certificate authority (CA)
21+
keytool -genkey -alias ca \
22+
-keyalg RSA -keysize 4096 -sigalg SHA512withRSA -keypass $(PASSWORD) \
23+
-validity 3650 -dname $(DNAME_CA) \
24+
-keystore $(KEYSTORE) -storepass $(PASSWORD)
25+
26+
add-host:
27+
# Generate a host certificate
28+
keytool -genkey -alias $(HOSTNAME) \
29+
-keyalg RSA -keysize 4096 -sigalg SHA512withRSA -keypass $(PASSWORD) \
30+
-validity 3650 -dname $(DNAME_HOST) \
31+
-keystore $(KEYSTORE) -storepass $(PASSWORD)
32+
# Generate a host certificate signing request
33+
keytool -certreq -alias $(HOSTNAME) \
34+
-keyalg RSA -keysize 4096 -sigalg SHA512withRSA \
35+
-validity 3650 -file "$(HOSTNAME).csr" \
36+
-keystore $(KEYSTORE) -storepass $(PASSWORD)
37+
# Generate signed certificate with the certificate authority
38+
keytool -gencert -alias ca \
39+
-validity 3650 -sigalg SHA512withRSA \
40+
-infile "$(HOSTNAME).csr" -outfile "$(HOSTNAME).crt" -rfc \
41+
-keystore $(KEYSTORE) -storepass $(PASSWORD)
42+
# Import signed certificate into the keystore
43+
keytool -import -trustcacerts -alias $(HOSTNAME) \
44+
-file "$(HOSTNAME).crt" \
45+
-keystore $(KEYSTORE) -storepass $(PASSWORD)
46+
47+
create-truststore:
48+
# Export certificate authority into truststore
49+
keytool -export -alias ca -file ca.crt \
50+
-keystore $(KEYSTORE) -storepass $(PASSWORD)
51+
keytool -import -trustcacerts -noprompt -alias ca -file ca.crt \
52+
-keystore $(TRUSTSTORE) -storepass $(PASSWORD)
53+
54+
add-client:
55+
# Generate client certificate
56+
keytool -genkey -alias $(CLIENTNAME) \
57+
-keyalg RSA -keysize 4096 -sigalg SHA512withRSA -keypass $(PASSWORD) \
58+
-validity 3650 -dname $(DNAME_HOST) \
59+
-keystore $(TRUSTSTORE) -storepass $(PASSWORD)
60+
# Generate a host certificate signing request
61+
keytool -certreq -alias $(CLIENTNAME) \
62+
-keyalg RSA -keysize 4096 -sigalg SHA512withRSA \
63+
-validity 3650 -file "$(CLIENTNAME).csr" \
64+
-keystore $(TRUSTSTORE) -storepass $(PASSWORD)
65+
# Generate signed certificate with the certificate authority
66+
keytool -gencert -alias ca \
67+
-validity 3650 -sigalg SHA512withRSA \
68+
-infile "$(CLIENTNAME).csr" -outfile "$(CLIENTNAME).crt" -rfc \
69+
-keystore $(KEYSTORE) -storepass $(PASSWORD)
70+
# Import signed certificate into the truststore
71+
keytool -import -trustcacerts -alias $(CLIENTNAME) \
72+
-file "$(CLIENTNAME).crt" \
73+
-keystore $(TRUSTSTORE) -storepass $(PASSWORD)
74+
75+
clean:
76+
rm -f $(KEYSTORE) *.csr *.crt $(TRUSTSTORE)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung.spring.security</groupId>
7+
<artifactId>server</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>server</name>
12+
<description>Spring x.509 Authentication Demo</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.4.0.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-security</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-maven-plugin</artifactId>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
58+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.spring.security.x509;
2+
3+
import org.springframework.security.core.Authentication;
4+
import org.springframework.security.core.userdetails.UserDetails;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
9+
import java.security.Principal;
10+
11+
@Controller
12+
public class UserResource {
13+
14+
@RequestMapping(value = "/user")
15+
public String user(Model model, Principal principal) {
16+
UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal();
17+
model.addAttribute("username", currentUser.getUsername());
18+
return "user";
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.spring.security.x509;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class X509AuthenticationServer {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(X509AuthenticationServer.class, args);
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
server.ssl.key-store=../keystore/keystore.jks
2+
server.ssl.key-store-password=${PASSWORD}
3+
server.ssl.key-alias=localhost
4+
server.ssl.key-password=${PASSWORD}
5+
server.ssl.enabled=true
6+
server.port=8443
7+
security.user.name=Admin
8+
security.user.password=admin
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>X.509 Authentication Demo</title>
5+
</head>
6+
<body>
7+
<h2>Hello <span th:text="${username}"/>!</h2>
8+
</body>
9+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.spring.security.x509;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class X509AuthenticationServerTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)