Java tutorial
/** * Copyright (C) 2011 Flamingo Project (http://www.opencloudengine.org). * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package io.uengine.web.registe; import io.uengine.common.rest.Response; import io.uengine.util.DateUtils; import io.uengine.util.EscapeUtils; import io.uengine.util.ExceptionUtils; import io.uengine.util.NetworkUtils; import io.uengine.web.configuration.DefaultController; import io.uengine.web.security.AESPasswordEncoder; import io.uengine.web.system.UserService; import org.apache.commons.codec.binary.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Properties; /** * Security Auth Controller * * @author Seungpil PARK * @since 2.0 */ @Controller @RequestMapping("/registe") public class RegisteController extends DefaultController { @Autowired @Qualifier("config") private Properties config; @Autowired private UserService userService; @Autowired private RegisteService registeService; @Autowired @Qualifier("passwordEncoder") private AESPasswordEncoder passwordEncoder; /** * SLF4J Logging */ private Logger logger = LoggerFactory.getLogger(RegisteController.class); @RequestMapping(value = "/register", method = RequestMethod.GET) public ModelAndView register(HttpSession session) { return new ModelAndView("/registe/register"); } /** * ? ? ? ? ? ? ??? . */ @RequestMapping(value = "/request", method = RequestMethod.POST) public ModelAndView registerRequest(HttpServletRequest request, @RequestParam String email, @RequestParam String password, @RequestParam String confirmPassword) { String unescapedPassword = EscapeUtils.unescape(password); //? ??? if (userService.waitingConfirmation(email)) { ModelAndView mav = new ModelAndView(); mav.setViewName("/registe/reaffirm"); mav.addObject("responseEmail", email); return mav; } //? ? ?? ? if (userService.completeAccount(email)) { ModelAndView mav = new ModelAndView(); mav.setViewName("/registe/exist"); mav.addObject("responseEmail", email); return mav; } // ? ?? ??. String ipAddr = NetworkUtils.getIpAddr(request); Map userMap = new HashMap(); userMap.put("email", email); userMap.put("password", passwordEncoder.encode(unescapedPassword)); userMap.put("country", NetworkUtils.getCountryCode(ipAddr)); userService.createUser(userMap); Registe registe = new Registe(); registeService.sendRegisteMail(email); ModelAndView mav = new ModelAndView(); mav.addObject("responseEmail", email); mav.setViewName("/registe/confirmation"); return mav; } @RequestMapping(value = "/remail", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) @ResponseBody public Response remail(@RequestBody Map params) { Response response = new Response(); try { String email = params.get("email").toString(); registeService.sendRegisteMail(email); response.setSuccess(true); } catch (Exception ex) { response.setSuccess(false); response.getError().setMessage(ex.getMessage()); if (ex.getCause() != null) response.getError().setCause(ex.getCause().getMessage()); response.getError().setException(ExceptionUtils.getFullStackTrace(ex)); } return response; } /** * ?? ??? ? ? ??. * ??? . */ @RequestMapping(value = "/confirm", method = RequestMethod.GET) public ModelAndView confirm(HttpServletResponse res, @RequestParam String userid, @RequestParam String token) throws IOException { long tokenTimestamp = Long.parseLong(new String(Base64.decodeBase64(token))); if (DateUtils.getDiffDays(new Date(), new Date(tokenTimestamp)) > 1) { ModelAndView mav = new ModelAndView(); mav.setViewName("/auth/error-401"); return mav; } try { registeService.completeRegiste(userid, token); ModelAndView mav = new ModelAndView(); mav.setViewName("/registe/complete"); return mav; } catch (Exception ex) { ModelAndView mav = new ModelAndView(); mav.setViewName("/auth/error-401"); return mav; } } }