BAEL-1556

This commit is contained in:
lol 2018-02-16 22:31:53 +01:00
parent 49d880bf58
commit 9dd6ca9179
10 changed files with 177 additions and 4 deletions

View File

@ -12,10 +12,10 @@
<description>Spring Security with Thymeleaf tutorial</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
<artifactId>parent-boot-5</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-5</relativePath>
</parent>
<properties>
@ -48,6 +48,15 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -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");
}
}

View File

@ -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);
}
}

View File

@ -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";
}
}

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Welcome to Spring Security Thymeleaf tutorial</title>
</head>
<body>
<h2>Welcome</h2>
<p>Spring Security Thymeleaf tutorial</p>
<div sec:authorize="hasRole('USER')">Text visible to user.</div>
<div sec:authorize="hasRole('ADMIN')">Text visible to admin.</div>
</body>
</html>

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Insert title here</title>
</head>
<body>
<h2>Custom Login Page</h2>
<form th:action="@{/login}" method="post">
<label for="username">Username</label>: <input type="text"
id="username" name="username" autofocus="autofocus" /> <br /> <label
for="password">Password</label>: <input type="password" id="password"
name="password" /> <br /> <input type="submit" value="Log in" />
</form>
</body>
</html>

View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Login error page</title>
</head>
<body>
<h2>Login Error Page</h2>
</body>
</html>

View File

@ -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);
}
}

View File

@ -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"));
}
}