Skip to content

Commit 8dc92aa

Browse files
Working demo to demonstrate keystore creation and SSL.
1 parent acf7ec2 commit 8dc92aa

8 files changed

Lines changed: 134 additions & 9 deletions

File tree

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>basic-secured-server</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>basic-secured-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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 UserController {
13+
@RequestMapping(value = "/user")
14+
public String user(Model model, Principal principal) {
15+
UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal();
16+
model.addAttribute("username", currentUser.getUsername());
17+
return "user";
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
public static void main(String[] args) {
9+
SpringApplication.run(X509AuthenticationServer.class, args);
10+
}
11+
}
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=${HOSTNAME}
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
@Test
12+
public void contextLoads() {
13+
}
14+
}

‎spring-security-x509/keystore/Makefile‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
PASSWORD=changeit
22
KEYSTORE=keystore.jks
33
HOSTNAME=localhost
4+
CLIENTNAME=cid
45
# CN = Common Name
56
# OU = Organization Unit
67
# O = Organization Name
@@ -11,14 +12,14 @@ HOSTNAME=localhost
1112
DNAME_CA='CN=Baeldung CA,OU=baeldung.com,O=Baeldung,L=SomeCity,ST=SomeState,C=CC'
1213
# For server certificates, the Common Name (CN) must be the hostname
1314
DNAME_HOST='CN=$(HOSTNAME),OU=baeldung.com,O=Baeldung,L=SomeCity,ST=SomeState,C=CC'
15+
DNAME_CLIENT='CN=$(CLIENTNAME),OU=baeldung.com,O=Baeldung,L=SomeCity,ST=SomeState,C=CC'
1416
TRUSTSTORE=truststore.jks
15-
CLIENTNAME=cid
1617

1718
all: clean create-keystore add-host create-truststore add-client
1819

1920
create-keystore:
2021
# Generate a certificate authority (CA)
21-
keytool -genkey -alias ca \
22+
keytool -genkey -alias ca -ext BC=ca:true \
2223
-keyalg RSA -keysize 4096 -sigalg SHA512withRSA -keypass $(PASSWORD) \
2324
-validity 3650 -dname $(DNAME_CA) \
2425
-keystore $(KEYSTORE) -storepass $(PASSWORD)
@@ -30,7 +31,7 @@ add-host:
3031
-validity 3650 -dname $(DNAME_HOST) \
3132
-keystore $(KEYSTORE) -storepass $(PASSWORD)
3233
# Generate a host certificate signing request
33-
keytool -certreq -alias $(HOSTNAME) \
34+
keytool -certreq -alias $(HOSTNAME) -ext BC=ca:true \
3435
-keyalg RSA -keysize 4096 -sigalg SHA512withRSA \
3536
-validity 3650 -file "$(HOSTNAME).csr" \
3637
-keystore $(KEYSTORE) -storepass $(PASSWORD)
@@ -44,21 +45,25 @@ add-host:
4445
-file "$(HOSTNAME).crt" \
4546
-keystore $(KEYSTORE) -storepass $(PASSWORD)
4647

47-
create-truststore:
48-
# Export certificate authority into truststore
49-
keytool -export -alias ca -file ca.crt \
48+
export-authority:
49+
# Export certificate authority
50+
keytool -export -alias ca -file ca.crt -rfc \
5051
-keystore $(KEYSTORE) -storepass $(PASSWORD)
52+
53+
54+
create-truststore: export-authority
55+
# Import certificate authority into a new truststore
5156
keytool -import -trustcacerts -noprompt -alias ca -file ca.crt \
5257
-keystore $(TRUSTSTORE) -storepass $(PASSWORD)
5358

5459
add-client:
5560
# Generate client certificate
5661
keytool -genkey -alias $(CLIENTNAME) \
5762
-keyalg RSA -keysize 4096 -sigalg SHA512withRSA -keypass $(PASSWORD) \
58-
-validity 3650 -dname $(DNAME_HOST) \
63+
-validity 3650 -dname $(DNAME_CLIENT) \
5964
-keystore $(TRUSTSTORE) -storepass $(PASSWORD)
6065
# Generate a host certificate signing request
61-
keytool -certreq -alias $(CLIENTNAME) \
66+
keytool -certreq -alias $(CLIENTNAME) -ext BC=ca:true \
6267
-keyalg RSA -keysize 4096 -sigalg SHA512withRSA \
6368
-validity 3650 -file "$(CLIENTNAME).csr" \
6469
-keystore $(TRUSTSTORE) -storepass $(PASSWORD)
@@ -73,4 +78,5 @@ add-client:
7378
-keystore $(TRUSTSTORE) -storepass $(PASSWORD)
7479

7580
clean:
76-
rm -f $(KEYSTORE) *.csr *.crt $(TRUSTSTORE)
81+
# Remove generated artifacts
82+
find . ! -name Makefile -type f -exec rm -f {} \;

spring-security-x509/server/src/main/java/com/baeldung/spring/security/x509/UserResource.java renamed to spring-security-x509/server/src/main/java/com/baeldung/spring/security/x509/UserController.java

File renamed without changes.

0 commit comments

Comments
 (0)