Java tutorial
/* * Copyright 2015-2020 Fengduo.com All right reserved. This software is the confidential and proprietary information of * Fengduo.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only * in accordance with the terms of the license agreement you entered into with Fengduo.com. */ package com.fengduo.bee.web.controller.account; import java.io.IOException; import java.io.PrintWriter; import javax.security.auth.login.AccountException; import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import org.apache.commons.lang.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.IncorrectCredentialsException; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.web.filter.authc.FormAuthenticationFilter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; 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 com.fengduo.bee.commons.cons.ResultCode; import com.fengduo.bee.commons.core.lang.Argument; import com.fengduo.bee.commons.persistence.Parameter; import com.fengduo.bee.commons.result.JsonResultUtils; import com.fengduo.bee.commons.servlet.ValidateCodeServlet; import com.fengduo.bee.commons.shiro.Permission; import com.fengduo.bee.commons.velocity.CustomVelocityLayoutView; import com.fengduo.bee.model.entity.User; import com.fengduo.bee.service.interfaces.SmsService; import com.fengduo.bee.service.interfaces.UserService; import com.fengduo.bee.web.controller.BaseController; import com.fengduo.bee.web.shiro.ShiroDbRealm.ShiroUser; import com.fengduo.bee.web.shiro.exception.CaptchaException; import com.fengduo.bee.web.shiro.exception.CaptchaInvalidException; import com.fengduo.bee.web.utils.InvokeTypeTools; /** * LoginController?(GET)?(POST), POSTFilter?, Controller. * * @author jie.xu */ @Controller public class LoginController extends BaseController { @Autowired private UserService userService; @Autowired private Permission permission; @Autowired private SmsService smsService; @InitBinder("user") public void initBinder(WebDataBinder binder) { binder.setFieldDefaultPrefix("user."); } /** * */ @RequestMapping(value = { "/index", "/" }) public ModelAndView index() { ModelAndView mav = new ModelAndView("index"); return mav; } /** * v1 */ @RequestMapping(value = { "/v1" }) public ModelAndView v1() { ModelAndView mav = new ModelAndView("indexV1"); mav.getModel().put(CustomVelocityLayoutView.USE_LAYOUT, "false"); return mav; } /** * 404 */ @RequestMapping(value = { "/404" }) public ModelAndView notFound() { ModelAndView mav = new ModelAndView("error/404"); return mav; } /** * ? */ @RequestMapping(value = "/login", method = RequestMethod.GET) public String login(Model model, String errMsg) { // SecurityUtils.getSubject().logout(); // ShiroUser currentUser = getCurrentUser(); // if (currentUser != null || (currentUser != null && // Argument.isNotPositive(currentUser.getId()))) { // return "redirect:/index"; // } model.addAttribute("errMsg", errMsg); return "account/login"; } /** * ajax? */ @RequestMapping(value = "/ajaxLogin") public String ajaxLogin(Model model) { model.addAttribute(CustomVelocityLayoutView.USE_LAYOUT, "false"); return "account/ajaxLogin"; } /** * ? * * @param userName * @param model * @return */ @RequestMapping(value = "/login", method = RequestMethod.POST) public String fail(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String userName, HttpServletRequest req, Model model) { String exceptionClassName = (String) req.getAttribute("shiroLoginFailure"); String error = null; if (UnknownAccountException.class.getName().equals(exceptionClassName)) { error = ",!"; } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) { error = "??/??!"; } else if (CaptchaInvalidException.class.getName().equals(exceptionClassName)) { error = "???,??!"; } else if (CaptchaException.class.getName().equals(exceptionClassName)) { error = "??!"; } else if (AccountException.class.getName().equals(exceptionClassName)) { error = "??/?!"; } else if (exceptionClassName != null) { error = "?!"; } if (StringUtils.isEmpty(exceptionClassName)) { String code = request.getParameter("captcha"); String phone = request.getParameter("username"); String pwd = request.getParameter("password"); if (StringUtils.equalsIgnoreCase(code, (String) session.getAttribute(ValidateCodeServlet.VALIDATE_CODE))) { updateShiroUser(phone, pwd); return "redirect:/user/setting"; } else { error = "??!"; } } model.addAttribute("errMsg", error); // ajax if (InvokeTypeTools.isAjax(req)) { response.setContentType("application/json"); PrintWriter writer = null; try { writer = response.getWriter(); } catch (IOException e1) { e1.printStackTrace(); } writer.print(JsonResultUtils.createJsonResult(ResultCode.ERROR, "", error)); writer.flush(); writer.close(); } return "account/login"; } /** * ? */ @RequestMapping(value = "/register", method = RequestMethod.GET) public String registerForm() { return "account/register"; } /** * ? * * @param user * @param result * @param checkCode ?? * @param confirmPassword * @param isRead 1:?? 0:?? * @return * @description: * @author jie.xu * @date 201569 ?8:48:23 */ @RequestMapping(value = "/register", method = RequestMethod.POST) public String register(@Valid User user, BindingResult result, String checkCode, String confirmPassword, String isRead, Model model) { if (result.hasErrors()) { model.addAttribute("errMsg", showFirstErrors(result)); return "account/register"; } model.addAttribute("phone", user.getPhone()); if (isRead == null || StringUtils.equals(isRead, "0")) { model.addAttribute("errMsg", "?????!"); return "account/register"; } confirmPassword = StringUtils.trim(confirmPassword); if (StringUtils.isEmpty(confirmPassword)) { model.addAttribute("errMsg", "?,?!"); return "account/register"; } String passwd = StringUtils.trim(user.getPassword()); if (!StringUtils.equals(confirmPassword, passwd)) { model.addAttribute("errMsg", "??!"); return "account/register"; } // ?? String phone = StringUtils.trim(user.getPhone()); String cacheCode = smsService.getCheckCodeCache(phone); if (!StringUtils.equals(checkCode, cacheCode)) { model.addAttribute("errMsg", "??,?????!"); return "account/register"; } Parameter query = Parameter.newParameter()// .pu("phone", phone); User existUser = userService.queryUser(query); if (existUser != null) { model.addAttribute("errMsg", "?,!"); return "account/register"; } user.setPhone(phone); user.setPassword(passwd); user = userService.insertUser(user); // updateShiroUser(phone, passwd); return "redirect:/user/setting"; } /** * ??? */ @RequestMapping(value = "/getSmsCode", method = RequestMethod.GET) public ModelAndView getSmsCode(String phone) { if (StringUtils.isEmpty(phone)) { return createJsonMav("?...", ResultCode.ERROR); } phone = com.fengduo.bee.commons.util.StringUtils.trim(phone); // ?? Parameter query = Parameter.newParameter()// .pu("phone", phone); User existUser = userService.queryUser(query); if (existUser != null) { return createJsonMav("?...", ResultCode.ERROR); } boolean flag = smsService.sendCheckCode(phone.trim()); if (flag) { return createJsonMav("???...", ResultCode.SUCCESS); } else { return createJsonMav("???", ResultCode.ERROR); } } /** * ? */ @RequestMapping(value = "/findpwd", method = RequestMethod.GET) public String findpwd() { ShiroUser currentUser = getCurrentUser(); if (currentUser != null || (currentUser != null && Argument.isNotPositive(currentUser.getId()))) { return "redirect:/index"; } return "account/findpwd"; } /** * ??? */ @RequestMapping(value = "/findPwdSms", method = RequestMethod.GET) public ModelAndView findPwdSms(String phone) { if (StringUtils.isEmpty(phone)) { return createJsonMav("?...", ResultCode.ERROR); } phone = com.fengduo.bee.commons.util.StringUtils.trim(phone); // ?? Parameter query = Parameter.newParameter()// .pu("phone", phone); User existUser = userService.queryUser(query); if (existUser == null) { return createJsonMav("?,?...", ResultCode.ERROR); } boolean flag = smsService.sendCheckCode(phone.trim()); if (flag) { return createJsonMav("???...", ResultCode.SUCCESS); } else { return createJsonMav("???", ResultCode.ERROR); } } /** * ????? */ @RequestMapping(value = "/findpwd", method = RequestMethod.POST) public String findpwd(Model model, String mobile, String code, String verify) { // ??? mobile = com.fengduo.bee.commons.util.StringUtils.trim(mobile); code = com.fengduo.bee.commons.util.StringUtils.trim(code); if (StringUtils.isEmpty(mobile)) { model.addAttribute("errMsg", "?,?!"); return "account/findpwd"; } if (StringUtils.isEmpty(code)) { model.addAttribute("errMsg", "??,?!"); return "account/findpwd"; } // ?? Parameter query = Parameter.newParameter()// .pu("phone", mobile); User existUser = userService.queryUser(query); if (existUser == null) { model.addAttribute("errMsg", "?,?!"); return "redirect:/register"; } String checkCodeCache = smsService.getCheckCodeCache(mobile); if (StringUtils.isEmpty(checkCodeCache)) { model.addAttribute("errMsg", "???,?????!"); return "account/findpwd"; } if (!StringUtils.equals(checkCodeCache, code)) { model.addAttribute("errMsg", "???,???!"); return "account/findpwd"; } session.setAttribute("MOBILE", mobile); return "redirect:/resetpwd"; } /** * ?? */ @RequestMapping(value = "/resetpwd", method = RequestMethod.GET) public ModelAndView resetpwd(ModelAndView mav) { mav.setViewName("account/resetpwd"); if (session == null) { mav.setViewName("account/findpwd"); mav.addObject("errMsg", "???"); return mav; } String phone = (String) session.getAttribute("MOBILE"); if (StringUtils.isEmpty(phone)) { mav.setViewName("account/findpwd"); mav.addObject("errMsg", "???"); return mav; } String checkCodeCache = smsService.getCheckCodeCache(phone); if (StringUtils.isEmpty(checkCodeCache)) { mav.setViewName("account/findpwd"); mav.addObject("errMsg", "???"); return mav; } return mav; } /** * ?????? */ @RequestMapping(value = "/resetpwd", method = RequestMethod.POST) public String resetpwd(Model model, String password, String confirmPwd) { if (StringUtils.isEmpty(password) || StringUtils.isEmpty(confirmPwd)) { model.addAttribute("errMsg", "?!"); return "account/resetpwd"; } if (!StringUtils.equals(password, confirmPwd)) { model.addAttribute("errMsg", "??,?!"); return "account/resetpwd"; } if (session == null) { model.addAttribute("errMsg", "???"); return "account/findpwd"; } String phone = (String) session.getAttribute("MOBILE"); if (StringUtils.isEmpty(phone)) { model.addAttribute("errMsg", "???"); return "account/findpwd"; } String checkCodeCache = smsService.getCheckCodeCache(phone); if (StringUtils.isEmpty(checkCodeCache)) { model.addAttribute("errMsg", "???"); return "account/findpwd"; } Parameter query = Parameter.newParameter()// .pu("phone", phone); User existUser = userService.queryUser(query); if (existUser == null) { model.addAttribute("errMsg", "?,?!"); return "redirect:/register"; } userService.updateUserPwd(existUser.getId(), password); model.addAttribute("errMsg", "??,?!"); SecurityUtils.getSubject().logout(); return "redirect:/login"; } }