Skip to content

Commit dbe34b4

Browse files
committed
Spring Boot Web Security Maven
1 parent 4bf57ab commit dbe34b4

File tree

9 files changed

+195
-26
lines changed

9 files changed

+195
-26
lines changed

Chapter4_Securing Spring/src/main/java/spring/security/Ingredient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import javax.persistence.Id;
1010

1111
/**
12-
* @Created 17 / 03 / 2020 - 6:43 PM
12+
* @Created 23 / 03 / 2020 - 5:17 PM
1313
* @project BootSecure
1414
* @Author Hamdamboy
1515
*/
1616

1717
@Data
1818
@RequiredArgsConstructor
19-
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
19+
//@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
2020
@Entity
2121
public class Ingredient {
2222
//
@@ -25,7 +25,7 @@ public class Ingredient {
2525
private final String name;
2626
private final Type type;
2727

28-
public static enum Type{
28+
public static enum Type {
2929
WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
3030
}
3131
}

Chapter4_Securing Spring/src/main/java/spring/security/Taco.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
@Data
1818
@Entity
19+
1920
public class Taco {
2021
//
2122

@@ -31,7 +32,7 @@ public class Taco {
3132

3233
@ManyToMany(targetEntity = Ingredient.class)
3334
@Size(min=1, message = "You must choose at least 1 ingredient")
34-
private List <Ingredient> ingredients;
35+
private List<Ingredient> ingredients;
3536

3637
@PrePersist
3738
void createAt(){

Chapter4_Securing Spring/src/main/java/spring/security/data/IngredientRepository.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package spring.security.data;
22

33
import org.springframework.data.repository.CrudRepository;
4-
import spring.security.Ingredient;
54

65
/**
76
* @Created 17 / 03 / 2020 - 6:45 PM
Lines changed: 87 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
package spring.security.web;
22

3-
import lombok.extern.slf4j.Slf4j;
3+
4+
import java.security.Principal;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
import javax.validation.Valid;
10+
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.security.core.userdetails.User;
413
import org.springframework.stereotype.Controller;
514
import org.springframework.ui.Model;
15+
import org.springframework.validation.Errors;
616
import org.springframework.web.bind.annotation.GetMapping;
17+
import org.springframework.web.bind.annotation.ModelAttribute;
18+
import org.springframework.web.bind.annotation.PostMapping;
719
import org.springframework.web.bind.annotation.RequestMapping;
20+
import org.springframework.web.bind.annotation.SessionAttributes;
21+
22+
import lombok.extern.slf4j.Slf4j;
823
import spring.security.Ingredient;
24+
import spring.security.Order;
25+
import spring.security.Taco;
26+
import spring.security.data.IngredientRepository;
27+
import spring.security.data.TacoRepository;
28+
import spring.security.data.UserRepository;
929

10-
import java.util.Arrays;
11-
import java.util.List;
1230

1331
/**
1432
* @Created 17 / 03 / 2020 - 6:44 PM
@@ -19,30 +37,79 @@
1937
@Slf4j
2038
@Controller
2139
@RequestMapping("/design")
40+
@SessionAttributes("order")
41+
2242
public class DesignTacoController {
2343
//
44+
private final IngredientRepository ingredientRepo;
45+
46+
private TacoRepository tacoRepo;
2447

25-
GetMapping
26-
public String showDesignForm(Model model) {
27-
List<Ingredient> ingredients = Arrays.asList(
28-
new Ingredient("FLTO", "Flour Tortilla", Ingredient.Type.WRAP),
29-
new Ingredient("COTO", "Corn Tortilla", Ingredient.Type.WRAP),
30-
new Ingredient("GRBF", "Ground Beef", Ingredient.Type.PROTEIN),
31-
new Ingredient("CARN", "Carnitas", Ingredient.Type.PROTEIN),
32-
new Ingredient("TMTO", "Diced Tomatoes", Ingredient.Type.VEGGIES),
33-
new Ingredient("LETC", "Lettuce", Ingredient.Type.VEGGIES),
34-
new Ingredient("CHED", "Cheddar", Ingredient.Type.CHEESE),
35-
new Ingredient("JACK", "Monterrey Jack", Ingredient.Type.CHEESE),
36-
new Ingredient("SLSA", "Salsa", Ingredient.Type.SAUCE),
37-
new Ingredient("SRCR", "Sour Cream", Ingredient.Type.SAUCE)
38-
);
48+
private UserRepository userRepo;
49+
50+
@Autowired
51+
public DesignTacoController(
52+
IngredientRepository ingredientRepo,
53+
TacoRepository tacoRepo,
54+
UserRepository userRepo) {
55+
this.ingredientRepo = ingredientRepo;
56+
this.tacoRepo = tacoRepo;
57+
this.userRepo = userRepo;
58+
}
59+
60+
@ModelAttribute(name = "order")
61+
public Order order() {
62+
return new Order();
63+
}
64+
65+
@ModelAttribute(name = "design")
66+
public Taco design() {
67+
return new Taco();
68+
}
69+
70+
@GetMapping
71+
public String showDesignForm(Model model, Principal principal) {
72+
log.info(" --- Designing taco");
73+
List<Ingredient> ingredients = new ArrayList<>();
74+
ingredientRepo.findAll().forEach(i -> ingredients.add(i));
3975

4076
Ingredient.Type[] types = Ingredient.Type.values();
41-
for (Type type : types) {
42-
model.addAttribute(type.toString().toLowerCase(), filterByType(ingredients, type));
77+
for (Ingredient.Type type : types) {
78+
model.addAttribute(type.toString().toLowerCase(),
79+
filterByType(ingredients, type));
4380
}
44-
model.addAttribute("design", new Taco());
81+
82+
String username = principal.getName();
83+
User user = userRepo.findByUsername(username);
84+
model.addAttribute("user", user);
85+
4586
return "design";
4687
}
88+
89+
@PostMapping
90+
public String processDesign(
91+
@Valid Taco taco, Errors errors,
92+
@ModelAttribute Order order) {
93+
94+
log.info(" --- Saving taco");
95+
96+
if (errors.hasErrors()) {
97+
return "design";
98+
}
99+
100+
Taco saved = tacoRepo.save(taco);
101+
order.addDesign(saved);
102+
103+
return "redirect:/orders/current";
104+
}
105+
106+
private List<Ingredient> filterByType(
107+
List<Ingredient> ingredients, Ingredient.Type type) {
108+
return ingredients
109+
.stream()
110+
.filter(x -> x.getType().equals(type))
111+
.collect(Collectors.toList());
47112
}
113+
114+
48115
}
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
package spring.security.web;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.core.convert.converter.Converter;
5+
import org.springframework.stereotype.Component;
6+
7+
8+
import spring.security.Ingredient;
9+
import spring.security.data.IngredientRepository;
10+
11+
import java.util.Optional;
12+
313
/**
414
* @Created 17 / 03 / 2020 - 6:44 PM
515
* @project BootSecure
616
* @Author Hamdamboy
717
*/
8-
public class IngredientByIdConverter {
18+
19+
@Component
20+
public class IngredientByIdConverter implements Converter<String, Ingredient> {
21+
22+
private IngredientRepository ingredientRepo;
23+
@Autowired
24+
private IngredientByIdConverter(IngredientRepository ingredientRepository) {
25+
this.ingredientRepo = ingredientRepository;
26+
}
27+
@Override
28+
public Ingredient convert(String id) {
29+
Optional<Ingredient> optionalIngredient = ingredientRepo.findById(id);
30+
return optionalIngredient.isPresent() ?
31+
optionalIngredient.get() : null;
32+
}
933
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,74 @@
11
package spring.security.web;
22

3+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
4+
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.validation.Errors;
7+
import org.springframework.web.bind.annotation.*;
8+
import org.springframework.web.bind.support.SessionStatus;
9+
import spring.security.Order;
10+
import spring.security.data.OrderRepository;
11+
import spring.security.User;
12+
13+
import javax.validation.Valid;
14+
15+
316
/**
417
* @Created 17 / 03 / 2020 - 6:44 PM
518
* @project BootSecure
619
* @Author Hamdamboy
720
*/
21+
22+
@Controller
23+
@RequestMapping("/orders")
24+
@SessionAttributes("order")
25+
826
public class OrderController {
27+
28+
private OrderRepository orderRepo;
29+
30+
public OrderController(OrderRepository orderRepo) {
31+
this.orderRepo = orderRepo;
32+
}
33+
34+
@GetMapping("/current")
35+
public String orderForm(@AuthenticationPrincipal User user,
36+
@ModelAttribute Order order) {
37+
if (order.getDeliveryName() == null) {
38+
order.setDeliveryName(user.getFullname());
39+
}
40+
if (order.getDeliveryStreet() == null) {
41+
order.setDeliveryStreet(user.getStreet());
42+
}
43+
if (order.getDeliveryCity() == null) {
44+
order.setDeliveryCity(user.getCity());
45+
}
46+
if (order.getDeliveryState() == null) {
47+
order.setDeliveryState(user.getState());
48+
}
49+
if (order.getDeliveryZip() == null) {
50+
order.setDeliveryZip(user.getZip());
51+
}
52+
53+
return "orderForm";
54+
}
55+
56+
// tag::processOrderWithAuthenticationPrincipal[]
57+
@PostMapping
58+
public String processOrder(@Valid Order order, Errors errors,
59+
SessionStatus sessionStatus,
60+
@AuthenticationPrincipal User user) {
61+
62+
if (errors.hasErrors()) {
63+
return "orderForm";
64+
}
65+
66+
order.setUser(user);
67+
68+
orderRepo.save(order);
69+
sessionStatus.setComplete();
70+
71+
return "redirect:/";
72+
}
73+
// end::processOrderWithAuthenticationPrincipal[]
974
}

Chapter4_Securing Spring/src/main/java/spring/security/web/WebConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class WebConfig extends WebMvcConfigurerAdapter {
1414
@Override
1515
public void addViewControllers(ViewControllerRegistry registry) {
1616
registry.addViewController("/").setViewName("home");
17+
registry.addViewController("/abc").setViewName("home");
1718
registry.addViewController("/login");
1819
}
1920
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{"properties": [
2+
{
3+
"name": "taco.orders.page-size",
4+
"type": "java.lang.String",
5+
"description": "Sets the maximum number of orders to display in a list."
6+
},
7+
{
8+
"name": "taco.discount.codes",
9+
"type": "java.util.Map<String, Integer>",
10+
"description": "A map of discount codes to a discount percentage."
11+
}
12+
]}
Binary file not shown.

0 commit comments

Comments
 (0)