Java tutorial
/** * ######################## SHENBAISE'S WORK ########################## * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.siyuyan.module.web.controller; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.siyuyan.core.BaseController; import org.siyuyan.module.web.model.User; import org.siyuyan.module.web.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author whiteme * @date 2013727 * @desc ?? */ @Controller(value = "userController") public class UserController extends BaseController { @Autowired UserService userService; /** * * @return * @throws Exception */ @RequestMapping(value = "login") public String login() throws Exception { return "login"; } /** * * @return * @throws Exception */ @RequestMapping(value = "register") public String register() throws Exception { return "register"; } /** * ?? * @return * @throws Exception */ @RequestMapping(value = "forgetpwd") public String forgetpwd() throws Exception { return "forgetpwd"; } /** * ? * @param request * @param response * @return * @throws Exception */ @RequestMapping(value = "dologin") public String doLogin(User user, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO ??ip? // ?cookies if (null == user) { Cookie[] cookies = request.getCookies(); String email = null, password = null; for (Cookie cookie : cookies) { if ("email".equals(cookie.getName())) { email = cookie.getValue(); } else if ("password".equals(cookie.getName())) { password = cookie.getValue(); } } // user = new User(); if (StringUtils.isNotBlank(password)) user.setPassword(password); if (StringUtils.isNotBlank(email)) user.setEmail(email); } if (null != (user = userService.login(user))) { if (user.isRememberMe()) { // ? Cookie email = new Cookie("email", user.getEmail()); email.setMaxAge(60 * 60 * 24 * 14); Cookie password = new Cookie("password", user.getPassword()); password.setMaxAge(60 * 60 * 24 * 14); response.addCookie(email); response.addCookie(password); } request.getSession().setAttribute("userSession", user); } else { request.setAttribute("msg", "???"); return "login"; } return "redirect:/"; } @RequestMapping(value = "logout") public String logout(HttpServletRequest request, HttpServletResponse response) throws Exception { // session request.getSession().setAttribute("userSession", null); // cookies Cookie[] cookies = request.getCookies(); for (Cookie cookie : cookies) { cookie.setValue(null); response.addCookie(cookie); } return "redirect:/"; } /** * ?????? * @param request * @param response * @return * @throws Exception */ @RequestMapping(value = "doresetpasswd") public String dorestPasswd(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO ?? ?? return "redirect:/"; } @RequestMapping(value = "setpasswd") public String setpasswd(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO ?? return "redirect:/"; } /** * * @param request * @param response * @return * @throws Exception */ @RequestMapping(value = "doregister") public String doregister(User user, HttpServletRequest request, HttpServletResponse response) throws Exception { if (!userService.register(user)) { request.setAttribute("msg", ""); return "login"; } return "redirect:/"; } /** * * @param request * @param response * @return * @throws Exception */ @RequestMapping(value = "profile") public String dopersonalprofile(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO return "redirect:/"; } }