Advertisement
Guest User

Untitled

a guest
Jan 10th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.36 KB | None | 0 0
  1. <%--
  2. Document : index
  3. Created on : Feb 29, 2016, 7:42:47 PM
  4. Author : human
  5. --%>
  6. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  7.  
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <title>Activation</title>
  12.  
  13. <%@include file="/WEB-INF/views/fragments/bootstrap.jspf" %>
  14. </head>
  15. <body>
  16.  
  17. <%@include file="/WEB-INF/views/fragments/navbar.jspf" %>
  18.  
  19. <div class="container">
  20. <sec:authorize access="isAnonymous()">
  21. <p style="font-size: 16pt">${activationMessage}</p>
  22. </sec:authorize>
  23.  
  24. <sec:authorize access="isAuthenticated()">
  25. <p style="font-size: 16pt">
  26. <strong>Congratulation!</strong> Your account has been activate! Please wait until admin determine your real role!</p>
  27. </sec:authorize>
  28. </div>
  29.  
  30. </body>
  31. </html>
  32.  
  33. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  34. <!DOCTYPE html>
  35. <html>
  36. <head>
  37. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  38. <title>Registration</title>
  39. <style>
  40. body {
  41. background-color: #eeeeee !important;
  42. }
  43. .form-signin {
  44. max-width: 350px;
  45. margin-left: 25px;
  46. }
  47. .error {
  48. color: red;
  49. }
  50. </style>
  51. <%@include file="/WEB-INF/views/fragments/bootstrap.jspf" %>
  52. </head>
  53. <body>
  54. <%@include file="/WEB-INF/views/fragments/navbar.jspf" %>
  55.  
  56. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
  57.  
  58. <div class="container-fluid">
  59. <div class="row">
  60. <div class="col-md-4"></div>
  61. <div class="col-md-4">
  62. <form:form modelAttribute="user" class="form-signin" method="POST" action="${pageContext.servletContext.contextPath}/registr">
  63. <h2 class="form-signin-heading text-center">Registration</h2>
  64. <form:hidden path="id" />
  65. <div class="form-group">
  66. <label for="username">Email: </label>
  67. <form:input path="username" class="form-control" id="username" size="20" />
  68. <form:errors path="username" cssClass="error" />
  69. </div>
  70. <div class="form-group">
  71. <label for="pass">Password: </label>
  72. <form:password path="password" class="form-control" id="pass" size="20" />
  73. <form:errors path="password" cssClass="error" />
  74. </div>
  75. <div class="form-group">
  76. <label for="passRep">Repeat yout Password: </label>
  77. <form:password path="passwordRepeat" class="form-control" id="passRep" size="20" />
  78. <form:errors path="passwordRepeat" cssClass="error" />
  79. </div>
  80. <i>Please, enter your personal info </i>
  81. <div class="form-group">
  82. <label for="name">Name: </label>
  83. <form:input path="userInfo.name" class="form-control" id="name" size="20" />
  84. <form:errors path="userInfo.name" cssClass="error" />
  85. </div>
  86. <div class="form-group">
  87. <label for="surname">Surname: </label>
  88. <form:input path="userInfo.surname" class="form-control" id="surname" size="20" />
  89. <form:errors path="userInfo.surname" cssClass="error" />
  90. </div>
  91. <form:hidden path="enabled" />
  92. <div id="loginBut">
  93. <button class="btn btn-default" name="actionButt" value="sign_up">
  94. <span class="glyphicon glyphicon-ok" style="color: #04db66" aria-hidden="true"></span>
  95. Sign up
  96. </button>
  97. <a href="${pageContext.servletContext.contextPath}/index" class="btn btn-default">
  98. <span class="glyphicon glyphicon-remove" style="color: red" aria-hidden="true"></span>Cancel
  99. </a>
  100. </div>
  101. </form:form>
  102. </div>
  103. <div class="col-md-4"></div>
  104. </div>
  105. </div>
  106. </body>
  107. </html>
  108.  
  109. <%--
  110. Document : index
  111. Created on : Feb 29, 2016, 7:42:47 PM
  112. Author : human
  113. --%>
  114. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  115.  
  116. <!DOCTYPE html>
  117. <html>
  118. <head>
  119. <title>Login form</title>
  120.  
  121. <%@include file="/WEB-INF/views/fragments/bootstrap.jspf" %>
  122. </head>
  123. <body>
  124.  
  125. <%@include file="/WEB-INF/views/fragments/navbar.jspf" %>
  126.  
  127. <div class="container-fluid">
  128. <div class="col-md-4"></div>
  129. <form method="post" class="col-md-4 form-horizontal" action="${pageContext.servletContext.contextPath}/auth">
  130. <div class="form-group" >
  131. <label for="inputEmail" class="col-sm-3 control-label">Username: </label>
  132. <div class="col-sm-9">
  133. <input type="text" name="user" class="form-control" id="inputEmail" placeholder="Email">
  134. </div>
  135. </div>
  136. <div class="form-group">
  137. <label for="inputPassword" class="col-sm-3 control-label">Password: </label>
  138. <div class="col-sm-9">
  139. <input type="password" name="pass" class="form-control" id="inputPassword" placeholder="Password">
  140. </div>
  141. </div>
  142. <div class="form-group">
  143. <div class="col-sm-offset-2 col-sm-10">
  144. <input id = "remember_me" type="checkbox" name="_spring_security_remember_me" />
  145. <label for="remember_me" class="inline">Remember me</label>
  146. </div>
  147. </div>
  148. <div class="form-group">
  149. <div class="col-sm-offset-2 col-sm-10">
  150. <button type="submit" class="btn btn-default">Sign in</button>
  151. </div>
  152. <br><br>
  153. <div class="col-sm-offset-2 col-sm-10" style="color: red">
  154. <strong><c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" /></strong>
  155. </div>
  156. </div>
  157. </form>
  158. <div class="col-md-4"></div>
  159. </div>
  160.  
  161. </body>
  162. </html>
  163.  
  164. /*
  165. * To change this license header, choose License Headers in Project Properties.
  166. * To change this template file, choose Tools | Templates
  167. * and open the template in the editor.
  168. */
  169. package ua.com.codefire.educationerp.web;
  170.  
  171. import java.util.HashSet;
  172. import java.util.Set;
  173. import java.util.logging.Level;
  174. import java.util.logging.Logger;
  175. import javax.mail.MessagingException;
  176. import javax.mail.internet.MimeMessage;
  177. import javax.servlet.http.HttpServletRequest;
  178. import javax.servlet.http.HttpSession;
  179. import javax.validation.Valid;
  180. import org.apache.commons.codec.digest.DigestUtils;
  181. import org.springframework.beans.factory.annotation.Autowired;
  182. import org.springframework.mail.javamail.JavaMailSender;
  183. import org.springframework.mail.javamail.MimeMessageHelper;
  184. import org.springframework.messaging.simp.SimpMessagingTemplate;
  185. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  186. import org.springframework.security.core.GrantedAuthority;
  187. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  188. import org.springframework.security.core.context.SecurityContextHolder;
  189. import org.springframework.stereotype.Controller;
  190. import org.springframework.ui.Model;
  191. import org.springframework.validation.BindingResult;
  192. import org.springframework.web.bind.annotation.RequestMapping;
  193. import org.springframework.web.bind.annotation.RequestMethod;
  194. import ua.com.codefire.educationerp.db.entity.Event;
  195. import ua.com.codefire.educationerp.db.entity.User;
  196. import ua.com.codefire.educationerp.db.repo.*;
  197.  
  198. /**
  199. *
  200. * @author Виталий
  201. */
  202. @Controller
  203. public class RegistrationController {
  204.  
  205. @Autowired
  206. private UserRepository userRepo;
  207.  
  208. @Autowired
  209. private UserRolesRepository userRolesRepo;
  210.  
  211. @Autowired
  212. private EventRepository eventRepo;
  213.  
  214. // @Autowired
  215. // private WSController eventsController;
  216.  
  217. @Autowired
  218. private JavaMailSender mailSender;
  219.  
  220. @Autowired
  221. private SimpMessagingTemplate messagingTemplate;
  222.  
  223. @RequestMapping(path = "/registr", method = RequestMethod.GET)
  224. public String registrForm(Model model) {
  225. model.addAttribute("user", new User());
  226. return "authentication/registr";
  227. }
  228.  
  229. @RequestMapping(path = "/registr", method = RequestMethod.POST)
  230. public String registrNewUser(@Valid User user, BindingResult bindingResult,
  231. HttpSession session, HttpServletRequest req) {
  232. if(! user.getPassword().equals(user.getPasswordRepeat())){
  233. bindingResult.rejectValue("passwordRepeat", "notEqualPass",
  234. "Passwords are not equal! Try again!");
  235. }
  236. if(userRepo.findFirstByUsername(user.getUsername()) != null){
  237. bindingResult.rejectValue("username", "hasSuchAMail",
  238. "Sorry, but user with this Email is alredy registred in our system!");
  239. }
  240. if(user.getUserInfo().getName().length() < 2){
  241. bindingResult.rejectValue("userInfo.name", "nameIsMust", "Field \"Name\" must be filled");
  242. }
  243. if(user.getUserInfo().getSurname().length() < 2){
  244. bindingResult.rejectValue("userInfo.surname", "surnameIsMust", "Field \"Surname\" must be filled");
  245. }
  246. if (bindingResult.hasErrors()) {
  247. return "authentication/registr";
  248. }
  249. String md5 = DigestUtils.md5Hex(user.getPassword());
  250. user.setPassword(md5);
  251. userRepo.save(user);
  252. String activationPath = createActivationPath(req);
  253. String hash = createMd5Hash(user.getUsername(), session.getId());
  254. sendEmail(user.getUsername(), activationPath+hash);
  255. session.setAttribute("email", user.getUsername());
  256.  
  257. return "redirect:/regemail";
  258. }
  259.  
  260. @RequestMapping(path = "/activate", method = RequestMethod.GET)
  261. public String userActivation(HttpSession session, HttpServletRequest req, Model model){
  262. String userEmail = (String) session.getAttribute("email");
  263. String requestHash = req.getParameter("user_activ_code");
  264.  
  265. if(requestHash == null){
  266. session.setAttribute("activationMessage", "Sorry, but your link doesn't have information for activation");
  267. } else if(userEmail == null){
  268. session.setAttribute("activationMessage", "Sorry, but it seems that you didn't registrate new user during this session! Try again!");
  269. } else {
  270. String hashToCompare = createMd5Hash(userEmail, session.getId());
  271.  
  272. if(requestHash.equals(hashToCompare)){
  273. User user = userRepo.findFirstByUsername(userEmail);
  274. user.setEnabled((short)1);
  275. user.setUserRoles(userRolesRepo.findOne(5));
  276.  
  277. user = userRepo.save(user);
  278.  
  279. createNewEvent(user);
  280.  
  281. authorizeRegistredUser(user);
  282.  
  283. } else {
  284. session.setAttribute("activationMessage", "Sorry, but your request doesn't match with activation code!");
  285. }
  286. }
  287. return "redirect:/activation";
  288. }
  289.  
  290. private boolean sendEmail(String adrTo, String activationPath){
  291. MimeMessage message = mailSender.createMimeMessage();
  292. MimeMessageHelper helper;
  293. try {
  294. helper = new MimeMessageHelper(message, true);
  295. helper.setTo(adrTo);
  296. helper.setFrom("educationerp@mail.ru");
  297. helper.setSubject("Confirm your EducationERP account!");
  298. helper.setText("<html><body><h1>Thanks for your registration!</h1>" +
  299. "<p style=\"font-size: 16pt\">To Activate your account just click on the button below!</p> \n\n" +
  300. "</body></html> \n" + activationPath, true);
  301.  
  302. mailSender.send(message);
  303. } catch (MessagingException ex) {
  304. Logger.getLogger(RegistrationController.class.getName()).log(Level.SEVERE, null, ex);
  305. return false;
  306. }
  307.  
  308. return true;
  309. }
  310.  
  311. private String createMd5Hash(String adrTo, String sessionId){
  312. String concat = adrTo+sessionId;
  313. String result = DigestUtils.md5Hex(concat);
  314.  
  315. return result;
  316. }
  317.  
  318. private String createActivationPath(HttpServletRequest req){
  319. String result = req.getScheme() + "://" +
  320. req.getServerName() + ":" +
  321. req.getServerPort() +
  322. "/activate?user_activ_code=";
  323. return result;
  324. }
  325.  
  326. private void authorizeRegistredUser(User user){
  327. Set<GrantedAuthority> userAuth = new HashSet<>();
  328. userAuth.add(new SimpleGrantedAuthority(user.getUserRoles().getRole()));
  329. SecurityContextHolder.getContext().setAuthentication(
  330. new UsernamePasswordAuthenticationToken(user,user.getPassword(),userAuth));
  331. }
  332.  
  333. private void createNewEvent(User user){
  334. Event event = new Event();
  335. event.setType("info");
  336.  
  337. StringBuilder sb = new StringBuilder("New user just registred! Set his role and profile!");
  338. sb.append("\n").append(String.format("<a href='/user/%d'>%s</a>", user.getId(), user.getUsername()));
  339.  
  340. event.setMessage(sb.toString());
  341. event.setTitle("New registration!");
  342.  
  343. // eventsController.addEvent(event);
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement