Java tutorial
/* * The MIT License * * Copyright 2015 Tijme Gommers <t.gommers@jetcat.nl>. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.klm.workshop.controller; import com.klm.workshop.dao.UserDAO; import com.klm.workshop.helper.PasswordHasher; import com.klm.workshop.model.User; import com.klm.workshop.validator.SignUpValidator; import java.util.Locale; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.support.RedirectAttributes; /** * Authentication controller * * @author Tijme Gommers <t.gommers@jetcat.nl> */ @Controller("authController") @RequestMapping(value = "/auth") public class AuthController { /** * User data access object */ @Autowired UserDAO userDAO; /** * Translations */ @Autowired private MessageSource messageSource; /** * Lets anonymous clients sign in * * @param model Autowired model and view * @param error Login error cause * @return Sign in view with the sign in error, if set */ @RequestMapping(value = "/sign-in", method = RequestMethod.GET) public ModelAndView getSignIn(ModelAndView model, @RequestParam(value = "error", required = false, defaultValue = "") String error) { model.setViewName("auth/sign_in"); model.getModelMap().addAttribute("error", error); model.getModelMap().addAttribute("user", new User()); return model; } /** * Lets anonymous clients sign up * * @param model Autowired model and view * @return Sign up view */ @RequestMapping(value = "/sign-up", method = RequestMethod.GET) public ModelAndView getSignUp(ModelAndView model) { model.setViewName("auth/sign_up"); model.getModelMap().addAttribute("account", new SignUpValidator()); return model; } /** * Sign up client, and show the sign up form. On error, show errors. On * success, redirect to the sign in page and show success message. * * @param model Objects and view * @param account The posted account * @param result Binded validation * @param redirect The redirect attributes * @param locale The current client locale * @return Form to sign up, or a redirect (if client was sign up successfully) */ @RequestMapping(value = "/sign-up", method = RequestMethod.POST) public ModelAndView postCreate(ModelAndView model, @ModelAttribute("account") @Valid SignUpValidator account, BindingResult result, RedirectAttributes redirect, Locale locale) { if (result.hasErrors()) { model.setViewName("auth/sign_up"); } else { String password = PasswordHasher.hash(account.getPassword()); User user = new User(); user.setFirstName(account.getFirstName()); user.setLastName(account.getLastName()); user.setEmail(account.getEmail()); user.setPassword(password); user.setRole(User.Role.ROLE_PARTICIPANT); user.setEnabled(true); userDAO.create(user); String success = messageSource.getMessage("auth.message_sign_up_success", null, locale); redirect.addFlashAttribute("alertSuccess", success); model.setViewName("redirect:/auth/sign-in"); } return model; } }