Skip to content

Commit b62aa9a

Browse files
Merge branch 'pr/585-christian-mutual-auth'
2 parents a8179fa + b6ef8de commit b62aa9a

17 files changed

Lines changed: 364 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
<module>spring-security-rest-custom</module>
100100
<module>spring-security-rest-digest-auth</module>
101101
<module>spring-security-rest-full</module>
102+
<module>spring-security-x509</module>
102103
<module>spring-thymeleaf</module>
103104
<module>spring-zuul</module>
104105
<module>jsf</module>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
<artifactId>basic-secured-server</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>basic-secured-server</name>
11+
<description>Spring x.509 Authentication Demo</description>
12+
13+
<parent>
14+
<groupId>com.baeldung</groupId>
15+
<artifactId>spring-security-x509</artifactId>
16+
<version>0.0.1-SNAPSHOT</version>
17+
</parent>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-maven-plugin</artifactId>
24+
</plugin>
25+
</plugins>
26+
</build>
27+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.spring.security.x509;
2+
3+
import org.springframework.security.access.prepost.PreAuthorize;
4+
import org.springframework.security.core.Authentication;
5+
import org.springframework.security.core.userdetails.UserDetails;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
10+
import java.security.Principal;
11+
12+
@Controller
13+
public class UserController {
14+
@PreAuthorize("hasAuthority('ROLE_USER')")
15+
@RequestMapping(value = "/user")
16+
public String user(Model model, Principal principal) {
17+
UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal();
18+
model.addAttribute("username", currentUser.getUsername());
19+
return "user";
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.spring.security.x509;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
10+
import org.springframework.security.core.authority.AuthorityUtils;
11+
import org.springframework.security.core.userdetails.User;
12+
import org.springframework.security.core.userdetails.UserDetails;
13+
import org.springframework.security.core.userdetails.UserDetailsService;
14+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
15+
16+
@SpringBootApplication
17+
@EnableWebSecurity
18+
public class X509AuthenticationServer extends WebSecurityConfigurerAdapter {
19+
public static void main(String[] args) {
20+
SpringApplication.run(X509AuthenticationServer.class, args);
21+
}
22+
}
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=changeit
3+
server.ssl.key-alias=localhost
4+
server.ssl.key-password=changeit
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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
<artifactId>client-auth-server</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>client-auth-server</name>
11+
<description>Spring x.509 Client Authentication Demo</description>
12+
13+
<parent>
14+
<groupId>com.baeldung</groupId>
15+
<artifactId>spring-security-x509</artifactId>
16+
<version>0.0.1-SNAPSHOT</version>
17+
</parent>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-maven-plugin</artifactId>
24+
</plugin>
25+
</plugins>
26+
</build>
27+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.spring.security.x509;
2+
3+
import org.springframework.security.access.prepost.PreAuthorize;
4+
import org.springframework.security.core.Authentication;
5+
import org.springframework.security.core.userdetails.UserDetails;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
10+
import java.security.Principal;
11+
12+
@Controller
13+
public class UserController {
14+
@PreAuthorize("hasAuthority('ROLE_USER')")
15+
@RequestMapping(value = "/user")
16+
public String user(Model model, Principal principal) {
17+
UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal();
18+
model.addAttribute("username", currentUser.getUsername());
19+
return "user";
20+
}
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.spring.security.x509;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
10+
import org.springframework.security.core.authority.AuthorityUtils;
11+
import org.springframework.security.core.userdetails.User;
12+
import org.springframework.security.core.userdetails.UserDetails;
13+
import org.springframework.security.core.userdetails.UserDetailsService;
14+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
15+
16+
@SpringBootApplication
17+
@EnableWebSecurity
18+
@EnableGlobalMethodSecurity(prePostEnabled = true)
19+
public class X509AuthenticationServer extends WebSecurityConfigurerAdapter {
20+
public static void main(String[] args) {
21+
SpringApplication.run(X509AuthenticationServer.class, args);
22+
}
23+
24+
@Override
25+
protected void configure(HttpSecurity http) throws Exception {
26+
http.authorizeRequests().anyRequest().authenticated()
27+
.and()
28+
.x509().subjectPrincipalRegex("CN=(.*?)(?:,|$)").userDetailsService(userDetailsService());
29+
}
30+
31+
@Bean
32+
public UserDetailsService userDetailsService() {
33+
return new UserDetailsService() {
34+
@Override
35+
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
36+
if (username.equals("cid")) {
37+
return new User(username, "", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
38+
}
39+
throw new UsernameNotFoundException("User not found!");
40+
}
41+
};
42+
}
43+
}

0 commit comments

Comments
 (0)