Skip to content

Commit 028a822

Browse files
Merge pull request eugenp#3686 from kornelihno/BAEL-1556
Bael 1556
2 parents 92e2b18 + af3141d commit 028a822

11 files changed

Lines changed: 229 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
<module>lucene</module>
285285
<module>vraptor</module>
286286
<module>persistence-modules/java-cockroachdb</module>
287+
<module>spring-security-thymeleaf</module>
287288
<module>persistence-modules/java-jdbi</module>
288289
<module>jersey</module>
289290
<module>java-spi</module>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This module is for Spring Security Thymeleaf tutorial.
2+
Jira BAEL-1556

‎spring-security-thymeleaf/pom.xml‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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</groupId>
7+
<artifactId>spring-security-thymeleaf</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-security-thymeleaf</name>
12+
<description>Spring Security with Thymeleaf tutorial</description>
13+
14+
<parent>
15+
<artifactId>parent-boot-5</artifactId>
16+
<groupId>com.baeldung</groupId>
17+
<version>0.0.1-SNAPSHOT</version>
18+
<relativePath>../parent-boot-5</relativePath>
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-thymeleaf</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</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+
<dependency>
47+
<groupId>org.springframework.security</groupId>
48+
<artifactId>spring-security-test</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.thymeleaf.extras</groupId>
53+
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
54+
</dependency>
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-maven-plugin</artifactId>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
66+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.springsecuritythymeleaf;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
6+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
10+
11+
@Configuration
12+
@EnableWebSecurity
13+
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
14+
15+
@Override
16+
protected void configure(HttpSecurity http) throws Exception {
17+
http.authorizeRequests()
18+
.anyRequest()
19+
.authenticated()
20+
.and()
21+
.formLogin()
22+
.loginPage("/login")
23+
.permitAll()
24+
.successForwardUrl("/index")
25+
.and()
26+
.logout()
27+
.permitAll()
28+
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
29+
.logoutSuccessUrl("/login");
30+
}
31+
32+
@Autowired
33+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
34+
auth.inMemoryAuthentication()
35+
.withUser("user")
36+
.password("password")
37+
.roles("USER")
38+
.and()
39+
.withUser("admin")
40+
.password("admin")
41+
.roles("ADMIN");
42+
}
43+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.springsecuritythymeleaf;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringSecurityThymeleafApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringSecurityThymeleafApplication.class, args);
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.springsecuritythymeleaf;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
6+
@Controller
7+
public class ViewController {
8+
9+
@RequestMapping("/login")
10+
public String login() {
11+
return "login";
12+
}
13+
14+
@RequestMapping({ "/index", "/" })
15+
public String index() {
16+
return "index";
17+
}
18+
}

‎spring-security-thymeleaf/src/main/resources/application.properties‎

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Welcome to Spring Security Thymeleaf tutorial</title>
5+
</head>
6+
<body>
7+
<h2>Welcome</h2>
8+
<p>Spring Security Thymeleaf tutorial</p>
9+
<div sec:authorize="hasRole('USER')">Text visible to user.</div>
10+
<div sec:authorize="hasRole('ADMIN')">Text visible to admin.</div>
11+
<div sec:authorize="isAuthenticated()">Text visible only to
12+
authenticated users.</div>
13+
Authenticated username:
14+
<div sec:authentication="name"></div>
15+
Authenticated user roles:
16+
<div sec:authentication="principal.authorities"></div>
17+
</body>
18+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Custom Login Page</title>
5+
</head>
6+
<body>
7+
<h2>Custom Login Page</h2>
8+
<form th:action="@{/login}" method="post">
9+
<label for="username">Username</label>: <input type="text"
10+
id="username" name="username" autofocus="autofocus" /> <label
11+
for="password">Password</label>: <input type="password" id="password"
12+
name="password" /><input type="submit" value="Log in" />
13+
</form>
14+
</body>
15+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.springsecuritythymeleaf;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
import org.springframework.web.context.WebApplicationContext;
11+
12+
@RunWith(SpringRunner.class)
13+
@SpringBootTest
14+
public class SpringSecurityThymeleafApplicationTests {
15+
16+
@Autowired
17+
ViewController viewController;
18+
@Autowired
19+
WebApplicationContext wac;
20+
21+
@Test
22+
public void whenConfigured_thenLoadsContext() {
23+
assertNotNull(viewController);
24+
assertNotNull(wac);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)