Skip to content

Added Spring Security for authentication and role-based authorization #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package guru.springframework.configuration;

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.WebSecurityConfigurerAdapter;

Expand All @@ -9,11 +11,23 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
.authorizeRequests().antMatchers("/console/**").permitAll();
httpSecurity
.authorizeRequests().antMatchers("/","/products","/product/show/*","/console/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.and()
.logout().permitAll();

httpSecurity.csrf().disable();
httpSecurity.headers().frameOptions().disable();
httpSecurity.csrf().disable();
httpSecurity.headers().frameOptions().disable();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password("admin").roles("ADMIN")
.and().withUser("user").password("user").roles("USER");;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public String list(Model model){
return "products";
}

@RequestMapping("product/{id}")
@RequestMapping("product/show/{id}")
public String showProduct(@PathVariable Integer id, Model model){
model.addAttribute("product", productService.getProductById(id));
return "productshow";
Expand All @@ -46,7 +46,7 @@ public String newProduct(Model model){
@RequestMapping(value = "product", method = RequestMethod.POST)
public String saveProduct(Product product){
productService.saveProduct(product);
return "redirect:/product/" + product.getId();
return "redirect:/product/show/" + product.getId();
}

@RequestMapping("product/delete/{id}")
Expand All @@ -55,4 +55,9 @@ public String delete(@PathVariable Integer id){
return "redirect:/products";
}

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(){
return "login";
}

}
8 changes: 8 additions & 0 deletions src/main/resources/static/css/guru.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
input[type=submit] {
background:none!important;
border:none;
padding:0!important;
color: blue;
text-decoration: underline;
cursor:pointer;
}
13 changes: 8 additions & 5 deletions src/main/resources/templates/fragments/header.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head lang="en">

<link rel="stylesheet" type="text/css" href="../static/css/guru.css" />
</head>
<body>

Expand All @@ -13,13 +13,15 @@
<a class="navbar-brand" href="#" th:href="@{/}">Home</a>
<ul class="nav navbar-nav">
<li><a href="#" th:href="@{/products}">Products</a></li>
<li><a href="#" th:href="@{/product/new}">Create Product</a></li>
<li><a href="#" th:href="@{/product/new}" sec:authorize="hasRole('ROLE_ADMIN')">Create Product</a></li>
<li><a href="#" th:href="@{/login}">Sign In</a></li>
</ul>

</div>
</div>
</nav>

<div class="welcome">
<span sec:authorize="isAuthenticated()">Welcome <span sec:authentication="name"></span></span>
</div>
<div class="jumbotron">
<div class="row text-center">
<div class="">
Expand All @@ -35,5 +37,6 @@ <h3>Spring Boot Web App</h3>
</div>
</div>
</div>

</body>
</html>
36 changes: 36 additions & 0 deletions src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Login Form</title>
<!--/*/ <th:block th:include="fragments/headerinc :: head"></th:block> /*/-->
</head>
<body>
<div class="container">
<!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->
<div th:if="${param.error}">
<label style="color:red">Invalid username and password.</label>
</div>
<div th:if="${param.logout}">
<label>
You have been logged out.
</label>
</div>

<form th:action="@{/login}" method="post">

<table class="table table-striped">
<tr>
<td><label> User Name : <input type="text" name="username"/> </label></td>
</tr>
<tr>
<td><label> Password : <input type="password" name="password"/> </label></td>
</tr>
<tr>
<td> <button type="submit" class="btn btn-default">Sign In</button></td>
</tr>
</table>

</form>
</div>
</body>
</html>
15 changes: 12 additions & 3 deletions src/main/resources/templates/productform.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<head>

<title>Spring Framework Guru</title>

<link rel="stylesheet" type="text/css" href="../static/css/guru.css" />
<!--/*/ <th:block th:include="fragments/headerinc :: head"></th:block> /*/-->
</head>
<body>
<div class="container">
<!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->

<h2>Product Details</h2>
<form class="form-horizontal" th:action="@{/logout}" method="post">
<div class="form-group">
<div class="col-sm-10"> <h2>Product Create/Update</h2></div>
<div class="col-sm-2" style="padding-top: 30px;">
<input type="submit" value="Sign Out"/>
</div>
</div>
</form>

<div>
<form class="form-horizontal" th:object="${product}" th:action="@{/product}" method="post">
<input type="hidden" th:field="*{id}"/>
Expand All @@ -37,6 +45,7 @@ <h2>Product Details</h2>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>

</div>
</div>

Expand Down
26 changes: 18 additions & 8 deletions src/main/resources/templates/products.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head lang="en">

<title>Spring Framework Guru</title>
Expand All @@ -9,26 +9,36 @@
<body>
<div class="container">
<!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->



<div th:if="${not #lists.isEmpty(products)}">
<h2>Product List</h2>
<form th:action="@{/logout}" method="post">
<div class="col-sm-10"><h2>Product Listing</h2></div>
<div class="col-sm-2" style="padding-top: 30px;">
<span sec:authorize="isAuthenticated()">
<input type="submit" value="Sign Out"/>
</span>
</div>
</form>
<table class="table table-striped">
<tr>
<th>Id</th>
<th>Product Id</th>
<th>Description</th>
<th>Price</th>
<th>View</th>
<th>Edit</th>
<th>Delete</th>
<th sec:authorize="hasAnyRole('ROLE_USER','ROLE_ADMIN')">View</th>
<th sec:authorize="hasRole('ROLE_ADMIN')">Edit</th>
<th sec:authorize="hasRole('ROLE_ADMIN')">Delete</th>
</tr>
<tr th:each="product : ${products}">
<td th:text="${product.id}"><a href="/product/${product.id}">Id</a></td>
<td th:text="${product.productId}">Product Id</td>
<td th:text="${product.description}">descirption</td>
<td th:text="${product.price}">price</td>
<td><a th:href="${'/product/' + product.id}">View</a></td>
<td><a th:href="${'/product/edit/' + product.id}">Edit</a></td>
<td><a th:href="${'/product/delete/' + product.id}">Delete</a></td>
<td sec:authorize="hasAnyRole('ROLE_USER','ROLE_ADMIN')"><a th:href="${'/product/show/' + product.id}">View</a></td>
<td sec:authorize="hasRole('ROLE_ADMIN')"><a th:href="${'/product/edit/' + product.id}">Edit</a></td>
<td sec:authorize="hasRole('ROLE_ADMIN')"><a th:href="${'/product/delete/' + product.id}">Delete</a></td>
</tr>
</table>

Expand Down
18 changes: 12 additions & 6 deletions src/main/resources/templates/productshow.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head lang="en">

<title>Spring Framework Guru</title>
Expand All @@ -10,9 +10,15 @@
<div class="container">
<!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->

<h2>Product Details</h2>
<div>
<form class="form-horizontal">
<form class="form-horizontal" th:action="@{/logout}" method="post">
<div class="form-group">
<div class="col-sm-10"><h2>Product Details</h2></div>
<div class="col-sm-2" style="padding-top: 25px;">
<span sec:authorize="isAuthenticated()">
<input type="submit" value="Sign Out"/>
</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Product Id:</label>
<div class="col-sm-10">
Expand All @@ -36,8 +42,8 @@ <h2>Product Details</h2>
<p class="form-control-static" th:text="${product.imageUrl}">url....</p>
</div>
</div>
</form>
</div>
</form>

</div>

</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
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.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {RepositoryConfiguration.class})
public class ProductRepositoryTest {

Expand Down