在登录的时候,不只是客户端能登录,服务器端,也是一个完整的登录项目。
通过前面两篇可以看出,我的架构用的是spring 的MVC和spring注解,其实我根本就不明白,只是会用那么一点。
下面是登录方法的代码,包括,本地登录,和客户端的登录两种。
package com.tujia.tuuser.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.util.WebUtils; import com.tujia.common.config.ResultCode; import com.tujia.common.exception.UnAuthedException; import com.tujia.core.security.SecurityUtil; import com.tujia.core.util.Md5Util; import com.tujia.core.util.ParamChecker; import com.tujia.tuuser.entity.User; import com.tujia.tuuser.service.UserService; @Controller public class UserLoginController extends BaseController { @Resource private UserService UserService; @Resource private SecurityUtil securityUtil; @RequestMapping(value = "/tulogin", produces = "text/html") public String listByUser(Model model,HttpServletRequest request, HttpServletResponse response) { securityUtil.requrl(request,response); return "tulogin"; } /** * 登录方法/本地登录 * @param map * @param request * @param response */ @RequestMapping(value = "/tulogin") public void loginServer(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws UnAuthedException { response.setCharacterEncoding("UTF-8"); String phone = (String) map.get("phone"); String password = Md5Util.md5((String) map.get("password")); ResultCode resultCode = ResultCode.SUCCEED; Object result = null; try { ParamChecker.notEmpty("phone", phone); ParamChecker.notEmpty("password", password); User user = UserService.login(request, response, phone, password); String requrlCookie = securityUtil.getReqURL(request,response); result = requrlCookie; } catch (UnAuthedException e) { logger.error(e.getMessage()); resultCode = e.getResultCode(); result = e.getMessage(); } catch (Exception e) { logger.error(e.getMessage(), e); resultCode = ResultCode.INTERNAL_ERROR; result = e.getMessage(); } setResponse(response, resultCode, result); } /** * 退出接口 */ @RequestMapping(value = "/tulogout",method =RequestMethod.POST) public void logout( HttpServletRequest request, HttpServletResponse response) { ResultCode resultCode = ResultCode.SUCCEED; UserService.logout(request, response); Object result = null; setResponse(response, resultCode, result); } //登录接口,客户端通过此方法进行登录。 @RequestMapping(value = "/tuloginMethod") public void login(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws UnAuthedException { response.setCharacterEncoding("UTF-8"); String phone = (String) map.get("phone"); String password = Md5Util.md5((String) map.get("password")); ResultCode resultCode = ResultCode.SUCCEED; Object result = null; User user = null; String token = null; try { ParamChecker.notEmpty("phone", phone); ParamChecker.notEmpty("password", password); user = UserService.login(request, response, phone, password); token = (String) request.getAttribute("token"); } catch (UnAuthedException e) { logger.error(e.getMessage()); resultCode = e.getResultCode(); result = e.getMessage(); } catch (Exception e) { logger.error(e.getMessage(), e); resultCode = ResultCode.INTERNAL_ERROR; result = e.getMessage(); } setResponseMethod(response, resultCode, result, user , token); } //tokenMethod,通过token,客户端验证用户是否登录。 @RequestMapping(value = "/tokenMethod") public void tuloginMethod(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws UnAuthedException { response.setCharacterEncoding("UTF-8"); String token = (String) map.get("token"); boolean tokenServer = securityUtil.getAuthTuUser(token); String ret = ""; try { PrintWriter out = response.getWriter(); if(tokenServer){ ret = "success"; out.println(ret); out.close(); }else{ ret = "noLogin"; out.println(ret); out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
其中,在方法后面的
setResponseMethod
这个方法,就是传值的,通过json来传值。给前台,或者给客户端。
下面是代码。
protected void setResponse(HttpServletResponse resp, ResultCode resultCode, Object result) { setResponse(resp, resultCode, result, null); } protected void setResponse(HttpServletResponse resp, ResultCode resultCode, Object result, String callback) { try { resp.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); Map<String, Object> ret = new LinkedHashMap<String, Object>(); ret.put("code", resultCode.getCode()); ret.put("data", result); String responseStr = GSON.toJson(ret); out.println(responseStr); out.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } }
这个是往前台页面传递值的。
另一个,是给客户端传递值的。代码如下。
protected void setResponseMethod(HttpServletResponse resp, ResultCode resultCode, Object result,User user,String token) { setResponseMethod(resp, resultCode, result,user,token , null); } protected void setResponseMethod(HttpServletResponse resp, ResultCode resultCode, Object result,User user ,String token , String callback) { try { resp.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); Map<String, Object> ret = new LinkedHashMap<String, Object>(); ret.put("code", resultCode.getCode()); ret.put("data", result); ret.put("user", user); ret.put("token", token); String responseStr = GSON.toJson(ret); out.println(responseStr); out.close(); resp.setCharacterEncoding("utf-8"); } catch (Exception e) { logger.error(e.getMessage(), e); } }
这两个传递的json,引发出来两个问题,
一个是,在JS中,怎样解析JSON,获取其中的数据。
一个是,json中的json怎样转化成User对象。
这两个问题,都看另外两篇文章吧。
时间: 2024-09-12 22:57:50