package cn.autoform.web.controller.login; import cn.autoform.bean.system.UserInfo; import cn.autoform.bean.system.UserInfo.LogInInfo; import cn.autoform.util.tool.JSONTool; import cn.autoform.web.service.login.LoginService; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.util.WebUtils; import javax.annotation.Resource; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.net.URLEncoder; @RestController public class LoginRest { @Resource private LoginService loginService; @Value("${loginSendRedirect}") private String loginSendRedirect; @RequestMapping(value="/login", method = RequestMethod.GET) public void login(HttpServletRequest request, HttpServletResponse response) throws Exception{ //response.setCharacterEncoding("utf-8"); System.out.println("进入登录方法"); if(WebUtils.getCookie(request, "token") == null){ System.err.println("LoginRest 缺少token "); return; } /* * 获得登录信息 */ UserInfo.LogInInfo loginInfo=new LogInInfo(); loginInfo.setToken(WebUtils.getCookie(request, "token").getValue()); loginInfo.setOpenId(WebUtils.getCookie(request, "openId").getValue()); System.out.println("进入:"+JSONTool.toJson(loginInfo)); UserInfo user = loginService.getUserInfo(loginInfo); Cookie cookieUserName = new Cookie("userName", URLEncoder.encode(user.getUserName(), "UTF-8")); cookieUserName.setPath("/"); cookieUserName.setMaxAge(-1); /* * 添加登录信息(用户名) */ response.addCookie(cookieUserName); /* * 跳转默认页 */ System.out.println("登录后重定向路径:"+loginSendRedirect); response.sendRedirect(loginSendRedirect); } }