Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%--
- Document : index
- Created on : Feb 29, 2016, 7:42:47 PM
- Author : human
- --%>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Activation</title>
- <%@include file="/WEB-INF/views/fragments/bootstrap.jspf" %>
- </head>
- <body>
- <%@include file="/WEB-INF/views/fragments/navbar.jspf" %>
- <div class="container">
- <sec:authorize access="isAnonymous()">
- <p style="font-size: 16pt">${activationMessage}</p>
- </sec:authorize>
- <sec:authorize access="isAuthenticated()">
- <p style="font-size: 16pt">
- <strong>Congratulation!</strong> Your account has been activate! Please wait until admin determine your real role!</p>
- </sec:authorize>
- </div>
- </body>
- </html>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Registration</title>
- <style>
- body {
- background-color: #eeeeee !important;
- }
- .form-signin {
- max-width: 350px;
- margin-left: 25px;
- }
- .error {
- color: red;
- }
- </style>
- <%@include file="/WEB-INF/views/fragments/bootstrap.jspf" %>
- </head>
- <body>
- <%@include file="/WEB-INF/views/fragments/navbar.jspf" %>
- <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
- <div class="container-fluid">
- <div class="row">
- <div class="col-md-4"></div>
- <div class="col-md-4">
- <form:form modelAttribute="user" class="form-signin" method="POST" action="${pageContext.servletContext.contextPath}/registr">
- <h2 class="form-signin-heading text-center">Registration</h2>
- <form:hidden path="id" />
- <div class="form-group">
- <label for="username">Email: </label>
- <form:input path="username" class="form-control" id="username" size="20" />
- <form:errors path="username" cssClass="error" />
- </div>
- <div class="form-group">
- <label for="pass">Password: </label>
- <form:password path="password" class="form-control" id="pass" size="20" />
- <form:errors path="password" cssClass="error" />
- </div>
- <div class="form-group">
- <label for="passRep">Repeat yout Password: </label>
- <form:password path="passwordRepeat" class="form-control" id="passRep" size="20" />
- <form:errors path="passwordRepeat" cssClass="error" />
- </div>
- <i>Please, enter your personal info </i>
- <div class="form-group">
- <label for="name">Name: </label>
- <form:input path="userInfo.name" class="form-control" id="name" size="20" />
- <form:errors path="userInfo.name" cssClass="error" />
- </div>
- <div class="form-group">
- <label for="surname">Surname: </label>
- <form:input path="userInfo.surname" class="form-control" id="surname" size="20" />
- <form:errors path="userInfo.surname" cssClass="error" />
- </div>
- <form:hidden path="enabled" />
- <div id="loginBut">
- <button class="btn btn-default" name="actionButt" value="sign_up">
- <span class="glyphicon glyphicon-ok" style="color: #04db66" aria-hidden="true"></span>
- Sign up
- </button>
- <a href="${pageContext.servletContext.contextPath}/index" class="btn btn-default">
- <span class="glyphicon glyphicon-remove" style="color: red" aria-hidden="true"></span>Cancel
- </a>
- </div>
- </form:form>
- </div>
- <div class="col-md-4"></div>
- </div>
- </div>
- </body>
- </html>
- <%--
- Document : index
- Created on : Feb 29, 2016, 7:42:47 PM
- Author : human
- --%>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Login form</title>
- <%@include file="/WEB-INF/views/fragments/bootstrap.jspf" %>
- </head>
- <body>
- <%@include file="/WEB-INF/views/fragments/navbar.jspf" %>
- <div class="container-fluid">
- <div class="col-md-4"></div>
- <form method="post" class="col-md-4 form-horizontal" action="${pageContext.servletContext.contextPath}/auth">
- <div class="form-group" >
- <label for="inputEmail" class="col-sm-3 control-label">Username: </label>
- <div class="col-sm-9">
- <input type="text" name="user" class="form-control" id="inputEmail" placeholder="Email">
- </div>
- </div>
- <div class="form-group">
- <label for="inputPassword" class="col-sm-3 control-label">Password: </label>
- <div class="col-sm-9">
- <input type="password" name="pass" class="form-control" id="inputPassword" placeholder="Password">
- </div>
- </div>
- <div class="form-group">
- <div class="col-sm-offset-2 col-sm-10">
- <input id = "remember_me" type="checkbox" name="_spring_security_remember_me" />
- <label for="remember_me" class="inline">Remember me</label>
- </div>
- </div>
- <div class="form-group">
- <div class="col-sm-offset-2 col-sm-10">
- <button type="submit" class="btn btn-default">Sign in</button>
- </div>
- <br><br>
- <div class="col-sm-offset-2 col-sm-10" style="color: red">
- <strong><c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" /></strong>
- </div>
- </div>
- </form>
- <div class="col-md-4"></div>
- </div>
- </body>
- </html>
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ua.com.codefire.educationerp.web;
- import java.util.HashSet;
- import java.util.Set;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.mail.MessagingException;
- import javax.mail.internet.MimeMessage;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import javax.validation.Valid;
- import org.apache.commons.codec.digest.DigestUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.mail.javamail.JavaMailSender;
- import org.springframework.mail.javamail.MimeMessageHelper;
- import org.springframework.messaging.simp.SimpMessagingTemplate;
- import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.authority.SimpleGrantedAuthority;
- import org.springframework.security.core.context.SecurityContextHolder;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.validation.BindingResult;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import ua.com.codefire.educationerp.db.entity.Event;
- import ua.com.codefire.educationerp.db.entity.User;
- import ua.com.codefire.educationerp.db.repo.*;
- /**
- *
- * @author Виталий
- */
- @Controller
- public class RegistrationController {
- @Autowired
- private UserRepository userRepo;
- @Autowired
- private UserRolesRepository userRolesRepo;
- @Autowired
- private EventRepository eventRepo;
- // @Autowired
- // private WSController eventsController;
- @Autowired
- private JavaMailSender mailSender;
- @Autowired
- private SimpMessagingTemplate messagingTemplate;
- @RequestMapping(path = "/registr", method = RequestMethod.GET)
- public String registrForm(Model model) {
- model.addAttribute("user", new User());
- return "authentication/registr";
- }
- @RequestMapping(path = "/registr", method = RequestMethod.POST)
- public String registrNewUser(@Valid User user, BindingResult bindingResult,
- HttpSession session, HttpServletRequest req) {
- if(! user.getPassword().equals(user.getPasswordRepeat())){
- bindingResult.rejectValue("passwordRepeat", "notEqualPass",
- "Passwords are not equal! Try again!");
- }
- if(userRepo.findFirstByUsername(user.getUsername()) != null){
- bindingResult.rejectValue("username", "hasSuchAMail",
- "Sorry, but user with this Email is alredy registred in our system!");
- }
- if(user.getUserInfo().getName().length() < 2){
- bindingResult.rejectValue("userInfo.name", "nameIsMust", "Field \"Name\" must be filled");
- }
- if(user.getUserInfo().getSurname().length() < 2){
- bindingResult.rejectValue("userInfo.surname", "surnameIsMust", "Field \"Surname\" must be filled");
- }
- if (bindingResult.hasErrors()) {
- return "authentication/registr";
- }
- String md5 = DigestUtils.md5Hex(user.getPassword());
- user.setPassword(md5);
- userRepo.save(user);
- String activationPath = createActivationPath(req);
- String hash = createMd5Hash(user.getUsername(), session.getId());
- sendEmail(user.getUsername(), activationPath+hash);
- session.setAttribute("email", user.getUsername());
- return "redirect:/regemail";
- }
- @RequestMapping(path = "/activate", method = RequestMethod.GET)
- public String userActivation(HttpSession session, HttpServletRequest req, Model model){
- String userEmail = (String) session.getAttribute("email");
- String requestHash = req.getParameter("user_activ_code");
- if(requestHash == null){
- session.setAttribute("activationMessage", "Sorry, but your link doesn't have information for activation");
- } else if(userEmail == null){
- session.setAttribute("activationMessage", "Sorry, but it seems that you didn't registrate new user during this session! Try again!");
- } else {
- String hashToCompare = createMd5Hash(userEmail, session.getId());
- if(requestHash.equals(hashToCompare)){
- User user = userRepo.findFirstByUsername(userEmail);
- user.setEnabled((short)1);
- user.setUserRoles(userRolesRepo.findOne(5));
- user = userRepo.save(user);
- createNewEvent(user);
- authorizeRegistredUser(user);
- } else {
- session.setAttribute("activationMessage", "Sorry, but your request doesn't match with activation code!");
- }
- }
- return "redirect:/activation";
- }
- private boolean sendEmail(String adrTo, String activationPath){
- MimeMessage message = mailSender.createMimeMessage();
- MimeMessageHelper helper;
- try {
- helper = new MimeMessageHelper(message, true);
- helper.setTo(adrTo);
- helper.setFrom("educationerp@mail.ru");
- helper.setSubject("Confirm your EducationERP account!");
- helper.setText("<html><body><h1>Thanks for your registration!</h1>" +
- "<p style=\"font-size: 16pt\">To Activate your account just click on the button below!</p> \n\n" +
- "</body></html> \n" + activationPath, true);
- mailSender.send(message);
- } catch (MessagingException ex) {
- Logger.getLogger(RegistrationController.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- return true;
- }
- private String createMd5Hash(String adrTo, String sessionId){
- String concat = adrTo+sessionId;
- String result = DigestUtils.md5Hex(concat);
- return result;
- }
- private String createActivationPath(HttpServletRequest req){
- String result = req.getScheme() + "://" +
- req.getServerName() + ":" +
- req.getServerPort() +
- "/activate?user_activ_code=";
- return result;
- }
- private void authorizeRegistredUser(User user){
- Set<GrantedAuthority> userAuth = new HashSet<>();
- userAuth.add(new SimpleGrantedAuthority(user.getUserRoles().getRole()));
- SecurityContextHolder.getContext().setAuthentication(
- new UsernamePasswordAuthenticationToken(user,user.getPassword(),userAuth));
- }
- private void createNewEvent(User user){
- Event event = new Event();
- event.setType("info");
- StringBuilder sb = new StringBuilder("New user just registred! Set his role and profile!");
- sb.append("\n").append(String.format("<a href='/user/%d'>%s</a>", user.getId(), user.getUsername()));
- event.setMessage(sb.toString());
- event.setTitle("New registration!");
- // eventsController.addEvent(event);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement