소스 검색

Custom form login and role policies

Daniel Garcia Costa 2 년 전
부모
커밋
55345f811d

+ 2 - 0
src/main/java/es/uv/garcosda/config/WebConfig.java

@@ -20,6 +20,8 @@ public class WebConfig implements WebMvcConfigurer {
 		registry.addViewController("/").setViewName("index");
         registry.addViewController("/messages").setViewName("messages");
         registry.addViewController("/admin").setViewName("admin");
+        registry.addViewController("/login").setViewName("login");
+        registry.addViewController("/403").setViewName("403");
 	}
 	
     @Override

+ 17 - 4
src/main/java/es/uv/garcosda/config/WebSecurityConfig.java

@@ -15,6 +15,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.security.provisioning.InMemoryUserDetailsManager;
 import org.springframework.security.web.SecurityFilterChain;
+import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
 
 @Configuration
 @EnableWebSecurity
@@ -31,9 +32,21 @@ public class WebSecurityConfig {
 		http.authorizeHttpRequests()
 			.requestMatchers("/").permitAll()
 			.requestMatchers("/resources/**", "/webjars/**","/dist/**").permitAll()
-			.anyRequest().authenticated()
-			.and()
-            .formLogin();
+			.requestMatchers("/admin/**").hasRole("ADMIN")
+            .anyRequest()
+            .authenticated()
+            .and()
+            .formLogin()
+	            .loginPage("/login")
+	            .defaultSuccessUrl("/messages")
+	            .failureUrl("/login?error")
+				.permitAll()
+            .and()
+            .logout()
+	        	.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
+	        	.logoutSuccessUrl("/login?logout")
+	        	.deleteCookies("my-remember-me-cookie")
+	            .permitAll();
 		
 		return http.build();
 	}
@@ -47,7 +60,7 @@ public class WebSecurityConfig {
 					.build(),
 				User.withUsername("admin")
 					.password(passwordEncoder().encode("1234"))
-					.roles("USER")
+					.roles("ADMIN")
 					.build()));
 	}
 }

+ 23 - 0
src/main/resources/static/error/403.html

@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" 
+	  xmlns:th="http://www.thymeleaf.org"
+	  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
+  <head th:replace="layout.html :: head"> </head>
+  
+  <body>
+     <nav class="navbar navbar-inverse navbar-fixed-top" th:replace="layout.html :: navbar"> </nav>
+
+    <div class="container">
+      <div th:fragment="content">
+      
+	     <h3>Access Denied</h3>
+	     <h5>You are not authorized to view this page!!</h5>
+	     
+	  </div>
+    </div>
+
+    <script th:src="@{'/assets/js/jquery-2.1.4.min.js'}"></script>
+    <script th:src="@{'/assets/bootstrap/js/bootstrap.min.js'}"></script>
+    
+  </body>
+</html>

+ 23 - 0
src/main/resources/static/error/404.html

@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" 
+	  xmlns:th="http://www.thymeleaf.org"
+	  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
+  <head th:replace="layout.html :: head"> </head>
+  
+  <body>
+     <nav class="navbar navbar-inverse navbar-fixed-top" th:replace="layout.html :: navbar"> </nav>
+
+    <div class="container">
+      <div th:fragment="content">
+      
+	     <h3>Not Found</h3>
+	     <h5>Ooops the page you are looking for is not here!!</h5>
+	     
+	  </div>
+    </div>
+
+    <script th:src="@{'/assets/js/jquery-2.1.4.min.js'}"></script>
+    <script th:src="@{'/assets/bootstrap/js/bootstrap.min.js'}"></script>
+    
+  </body>
+</html>

+ 80 - 0
src/main/resources/templates/login.html

@@ -0,0 +1,80 @@
+<!doctype html>
+<html lang="es" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+ <head>
+	<meta charset="utf-8">
+	<meta name="viewport" content="width=device-width, initial-scale=1">
+	<title>Meetup</title>
+	<link href="/dist/css/bootstrap.min.css" rel="stylesheet">    
+</head>
+<body>
+	<header>
+	  <div class="navbar navbar-dark bg-dark box-shadow">
+	        <div class="container d-flex justify-content-between">
+	          <a href="/" class="navbar-brand d-flex align-items-center">
+	            <strong>Messages</strong>
+	          </a>
+	        </div>
+      </div>
+	</header>
+	<main>
+	  <div class="container">
+	    <div class="panel col-md-5">
+			<div class="panel panel-primary" style="margin-top:50px;">
+				<div class="panel-heading">Login Form</div>
+				<div class="panel-body">
+
+					<form action="home" th:action="@{/login}" method="post">
+						<div class="form-group has-feedback">
+							<input type="text" class="form-control" name="username" placeholder="Email" />
+							<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
+						</div>
+						<div class="form-group has-feedback">
+							<input type="password" class="form-control" name="password" placeholder="Password"/> 
+							<span class="glyphicon glyphicon-lock form-control-feedback"></span>
+						</div>
+						<div class="form-group">
+							<label>
+						      <input type="checkbox" name="remember-me"> Remember Me
+						    </label>
+						</div>
+						<div class="row" style="margin-top:25px;">
+							<div class="form-group col-xs-offset-8 col-xs-4">
+								<button type="submit" class="btn btn-primary btn-block btn-flat">Login</button>
+							</div>
+						</div>
+						<div class="row">
+							<div class="col-xs-12">
+								<div th:if="${param.error}"
+									class="alert alert-danger alert-dismissable">
+									<p>
+										<i class="icon fa fa-ban"></i> 
+										<span>Invalid Email and Password.</span>
+									</p>
+								</div>
+								<div th:if="${param.logout}"
+									class="alert alert-info alert-dismissable">
+									<p>
+										<i class="icon fa fa-info"></i> 
+										<span>You have been logged out.</span>
+									</p>
+								</div>
+								<div th:if="${msg!=null}"
+									class="alert alert-warning alert-dismissable">
+									<p>
+										<i class="icon fa fa-warning"></i> <span th:text="${msg}"></span>
+									</p>
+								</div>
+							</div>
+
+						</div>
+					</form>
+				</div>
+			</div>
+		</div>
+	  </div>
+	</main>
+	
+    <script src="/dist/js/bootstrap.bundle.min.js"></script>   
+</body>
+</html>
+