diff --git a/spring-security-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/SecurityConfiguration.java b/spring-security-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/SecurityConfiguration.java
new file mode 100644
index 0000000000..185093adfa
--- /dev/null
+++ b/spring-security-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/SecurityConfiguration.java
@@ -0,0 +1,38 @@
+package com.baeldung.springsecuritythymeleaf;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
+
+@Configuration
+@EnableWebSecurity
+public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
+
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ http
+ .authorizeRequests()
+ .anyRequest().authenticated()
+ .and()
+ .formLogin()
+ .loginPage("/login")
+ .permitAll().failureUrl("/loginError").successForwardUrl("/index")
+ .and()
+ .logout()
+ .permitAll()
+ .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
+ .logoutSuccessUrl("/login");
+ }
+
+ @Autowired
+ public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
+ auth
+ .inMemoryAuthentication()
+ .withUser("user").password("password").roles("USER").and()
+ .withUser("admin").password("admin").roles("ADMIN");
+ }
+}
diff --git a/spring-security-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/SpringSecurityThymeleafApplication.java b/spring-security-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/SpringSecurityThymeleafApplication.java
new file mode 100644
index 0000000000..c6e4dc1469
--- /dev/null
+++ b/spring-security-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/SpringSecurityThymeleafApplication.java
@@ -0,0 +1,12 @@
+package com.baeldung.springsecuritythymeleaf;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class SpringSecurityThymeleafApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(SpringSecurityThymeleafApplication.class, args);
+ }
+}
diff --git a/spring-security-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/ViewController.java b/spring-security-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/ViewController.java
new file mode 100644
index 0000000000..2f2fa69608
--- /dev/null
+++ b/spring-security-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/ViewController.java
@@ -0,0 +1,24 @@
+package com.baeldung.springsecuritythymeleaf;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+@Controller
+public class ViewController {
+
+ @RequestMapping("/login")
+ public String login() {
+ return "login";
+ }
+
+ @RequestMapping({"/index", "/"})
+ public String index() {
+ return "index";
+ }
+
+ @RequestMapping("/loginError")
+ public String loginError() {
+ return "loginError";
+ }
+
+}
diff --git a/spring-security-thymeleaf/src/main/resources/application.properties b/spring-security-thymeleaf/src/main/resources/application.properties
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/spring-security-thymeleaf/src/main/resources/templates/index.html b/spring-security-thymeleaf/src/main/resources/templates/index.html
new file mode 100644
index 0000000000..6cbc8f867e
--- /dev/null
+++ b/spring-security-thymeleaf/src/main/resources/templates/index.html
@@ -0,0 +1,12 @@
+
+
+
+Welcome to Spring Security Thymeleaf tutorial
+
+
+ Welcome
+ Spring Security Thymeleaf tutorial
+ Text visible to user.
+ Text visible to admin.
+
+
\ No newline at end of file
diff --git a/spring-security-thymeleaf/src/main/resources/templates/login.html b/spring-security-thymeleaf/src/main/resources/templates/login.html
new file mode 100644
index 0000000000..49cbedabf2
--- /dev/null
+++ b/spring-security-thymeleaf/src/main/resources/templates/login.html
@@ -0,0 +1,15 @@
+
+
+
+Insert title here
+
+
+ Custom Login Page
+
+
+
\ No newline at end of file
diff --git a/spring-security-thymeleaf/src/main/resources/templates/loginError.html b/spring-security-thymeleaf/src/main/resources/templates/loginError.html
new file mode 100644
index 0000000000..1e05cb4a87
--- /dev/null
+++ b/spring-security-thymeleaf/src/main/resources/templates/loginError.html
@@ -0,0 +1,9 @@
+
+
+
+Login error page
+
+
+ Login Error Page
+
+
\ No newline at end of file
diff --git a/spring-security-thymeleaf/src/test/java/com/baeldung/springsecuritythymeleaf/SpringSecurityThymeleafApplicationTests.java b/spring-security-thymeleaf/src/test/java/com/baeldung/springsecuritythymeleaf/SpringSecurityThymeleafApplicationTests.java
new file mode 100644
index 0000000000..dea254dd31
--- /dev/null
+++ b/spring-security-thymeleaf/src/test/java/com/baeldung/springsecuritythymeleaf/SpringSecurityThymeleafApplicationTests.java
@@ -0,0 +1,27 @@
+package com.baeldung.springsecuritythymeleaf;
+
+import static org.junit.Assert.assertNotNull;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.web.context.WebApplicationContext;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class SpringSecurityThymeleafApplicationTests {
+
+ @Autowired
+ ViewController viewController;
+ @Autowired
+ WebApplicationContext wac;
+
+ @Test
+ public void whenConfigured_thenLoadsContext() {
+ assertNotNull(viewController);
+ assertNotNull(wac);
+ }
+
+}
diff --git a/spring-security-thymeleaf/src/test/java/com/baeldung/springsecuritythymeleaf/ViewControllerIntegrationTest.java b/spring-security-thymeleaf/src/test/java/com/baeldung/springsecuritythymeleaf/ViewControllerIntegrationTest.java
new file mode 100644
index 0000000000..d2e7354d2d
--- /dev/null
+++ b/spring-security-thymeleaf/src/test/java/com/baeldung/springsecuritythymeleaf/ViewControllerIntegrationTest.java
@@ -0,0 +1,27 @@
+package com.baeldung.springsecuritythymeleaf;
+
+import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.web.servlet.MockMvc;
+
+@RunWith(SpringRunner.class)
+@WebMvcTest
+public class ViewControllerIntegrationTest {
+
+ @Autowired
+ MockMvc mockMvc;
+
+ @Test
+ public void givenUser_whenPerformingGet_thenReturnsIndex() throws Exception {
+ mockMvc.perform(get("/index").with(user("user").password("password"))).andExpect(status().isOk()).andExpect(view().name("index"));
+ }
+
+}