diff --git a/pom.xml b/pom.xml index f8e1910b9b..a81e92d874 100644 --- a/pom.xml +++ b/pom.xml @@ -284,6 +284,7 @@ lucene vraptor persistence-modules/java-cockroachdb + spring-security-thymeleaf persistence-modules/java-jdbi jersey java-spi diff --git a/spring-security-thymeleaf/README.MD b/spring-security-thymeleaf/README.MD new file mode 100644 index 0000000000..44d118f8a8 --- /dev/null +++ b/spring-security-thymeleaf/README.MD @@ -0,0 +1,2 @@ +This module is for Spring Security Thymeleaf tutorial. +Jira BAEL-1556 \ No newline at end of file diff --git a/spring-security-thymeleaf/pom.xml b/spring-security-thymeleaf/pom.xml new file mode 100644 index 0000000000..f40aabcf8b --- /dev/null +++ b/spring-security-thymeleaf/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + com.baeldung + spring-security-thymeleaf + 0.0.1-SNAPSHOT + jar + + spring-security-thymeleaf + Spring Security with Thymeleaf tutorial + + + parent-boot-5 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-5 + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + + + org.thymeleaf.extras + thymeleaf-extras-springsecurity4 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + 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..687c0c2e39 --- /dev/null +++ b/spring-security-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/SecurityConfiguration.java @@ -0,0 +1,43 @@ +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() + .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..09a4e74988 --- /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..a2337f9db5 --- /dev/null +++ b/spring-security-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/ViewController.java @@ -0,0 +1,18 @@ +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"; + } +} 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..27426ec496 --- /dev/null +++ b/spring-security-thymeleaf/src/main/resources/templates/index.html @@ -0,0 +1,18 @@ + + + +Welcome to Spring Security Thymeleaf tutorial + + +

Welcome

+

Spring Security Thymeleaf tutorial

+
Text visible to user.
+
Text visible to admin.
+
Text visible only to + authenticated users.
+ Authenticated username: +
+ Authenticated user roles: +
+ + \ 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..0177f7d8e8 --- /dev/null +++ b/spring-security-thymeleaf/src/main/resources/templates/login.html @@ -0,0 +1,15 @@ + + + +Custom Login Page + + +

Custom Login 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")); + } + +}