Guest User

Untitled

a guest
Jun 21st, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. <a href="login">LogIn</a>
  2.  
  3. <h2>Digita il tuo username e la tua password per accedere al sistema </h2>
  4. <form th:action="@{/login}" method="post" th:object="${responsabile}">
  5. <div class="field half first">
  6. <label for="name"><span class="icon fa-user"></span>Username:</label>
  7. <input name="name" id="username" type="text" placeholder="Username" th:field="*{nomeUtente}"/>
  8. </div>
  9. <div class="field half">
  10. <label for="email"><span class="icon fa-code"></span> Password:</label>
  11. <input name="email" id="email" type="password" placeholder="Password" th:field="*{chiaveAccesso}"/>
  12. </div>
  13. <ul class="actions">
  14. <li><input value="Login" class="button" type="submit"/></li>
  15. </ul>
  16. </form>
  17.  
  18. package it.uniroma3.controller;
  19.  
  20. import it.uniroma3.model.Centro;
  21. import it.uniroma3.model.Responsabile;
  22. import it.uniroma3.service.CentroService;
  23. import it.uniroma3.service.ResponsabileService;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.security.core.Authentication;
  26. import org.springframework.security.core.context.SecurityContextHolder;
  27. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  28. import org.springframework.stereotype.Controller;
  29. import org.springframework.ui.Model;
  30. import org.springframework.web.bind.annotation.ModelAttribute;
  31. import org.springframework.web.bind.annotation.RequestMapping;
  32. import org.springframework.web.bind.annotation.RequestMethod;
  33. import org.springframework.web.bind.annotation.RequestParam;
  34.  
  35. import javax.servlet.http.HttpSession;
  36. import javax.validation.Valid;
  37.  
  38. @Controller
  39. public class LoginController {
  40.  
  41. @Autowired
  42. private ResponsabileService responsabileService;
  43. @Autowired
  44. private CentroService centroService;
  45.  
  46. @RequestMapping("/login")
  47. public String login(Model model) {
  48. model.addAttribute("responsabile", new Responsabile());
  49. return "login";
  50. }
  51.  
  52. @RequestMapping("/role")
  53. public String loginRole(HttpSession session, Model model) {
  54. Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  55. String role = auth.getAuthorities().toString();
  56. Responsabile responsabile = this.responsabileService.findByNomeUtente(auth.getName());
  57.  
  58. String targetUrl = "";
  59. if(role.contains("RESPONSABILE")) {
  60. session.setAttribute("responsabile", responsabile);
  61. Centro centro=this.centroService.findById(responsabile.getCentro().getId());
  62. session.setAttribute("centro", centro);
  63. model.addAttribute("username",responsabile.getNomeUtente());
  64. targetUrl = "/responsabile/respPanel";
  65. } else if(role.contains("DIRETTORE")) {
  66. session.setAttribute("responsabile", responsabile);
  67. model.addAttribute("username", responsabile.getNomeUtente());
  68. targetUrl = "/direttore/direttorePanel";
  69. }
  70.  
  71. return targetUrl;
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78. }
  79.  
  80. @Entity
  81.  
  82. @Id
  83. @GeneratedValue(strategy=GenerationType.AUTO)
  84. private Long id;
  85.  
  86. @Column(nullable=false)
  87. private String nome;
  88.  
  89. @Column(nullable=false)
  90. private String cognome;
  91.  
  92. @Column(nullable=false, unique=true)
  93. private String nomeUtente;
  94.  
  95. @Column(nullable=false)
  96. private String chiaveAccesso;
  97.  
  98. @ManyToOne //ok
  99. private Azienda azienda;
  100.  
  101. @OneToOne //ok
  102. private Azienda aziendadiretta;
  103.  
  104. @OneToOne(cascade=CascadeType.ALL)
  105. private Centro centro;
  106.  
  107. @Column(nullable=false)
  108. private String role;
  109.  
  110. package it.uniroma3.error;
  111.  
  112. import org.slf4j.Logger;
  113. import org.slf4j.LoggerFactory;
  114. import org.springframework.security.access.AccessDeniedException;
  115. import org.springframework.security.core.Authentication;
  116. import org.springframework.security.core.context.SecurityContextHolder;
  117. import org.springframework.security.web.access.AccessDeniedHandler;
  118. import org.springframework.stereotype.Component;
  119.  
  120. import javax.servlet.ServletException;
  121. import javax.servlet.http.HttpServletRequest;
  122. import javax.servlet.http.HttpServletResponse;
  123. import java.io.IOException;
  124.  
  125. // handle 403 page
  126. @Component
  127. public class MyAccessDeniedHandler implements AccessDeniedHandler {
  128.  
  129. private static Logger logger = LoggerFactory.getLogger(MyAccessDeniedHandler.class);
  130.  
  131. @Override
  132. public void handle(HttpServletRequest httpServletRequest,
  133. HttpServletResponse httpServletResponse,
  134. AccessDeniedException e) throws IOException, ServletException {
  135.  
  136. Authentication auth
  137. = SecurityContextHolder.getContext().getAuthentication();
  138.  
  139. if (auth != null) {
  140. logger.info("User '" + auth.getName()
  141. + "' attempted to access the protected URL: "
  142. + httpServletRequest.getRequestURI());
  143. }
  144.  
  145. httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/403");
  146.  
  147. }
  148. }
Add Comment
Please, Sign In to add comment