Open minggen opened 5 years ago
package com.wmg.test.controller.admin; import com.wmg.test.anno.RequireLogin; import com.wmg.test.common.Consts; import com.wmg.test.common.JsonResult; import com.wmg.test.controller.BaseController; import com.wmg.test.entity.po.User; import com.wmg.test.service.UserService; import com.wmg.test.utils.CookieUtil; import com.wmg.test.utils.FastJsonUtil; import com.wmg.test.utils.RedisUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.concurrent.TimeUnit; /** * @author wangminggen * @date 2019-02-03 11:30 */ @RestController @RequestMapping(value = "user") public class UserController extends BaseController { @Autowired public UserService userService; @Autowired public RedisUtil redisUtil; @RequestMapping(value = "login", method = RequestMethod.POST) @ResponseBody public JsonResult<Object> login(String username, String password, HttpServletResponse httpServletResponse, HttpSession session) { JsonResult<String> validResult = validUserForLogin(username, password); if (validResult.isSuccess()) { User u = new User(); u.setUsername(username); u.setPassword(password); JsonResult<User> response = userService.login(u); if (response.isSuccess()) { CookieUtil.writeLoginToken(httpServletResponse, session.getId()); redisUtil.setEx(session.getId(), FastJsonUtil.objToString(response.getData()), Consts.RedisCacheExtime.REDIS_SESSION_EXTIME, TimeUnit.SECONDS); // session.setAttribute(Const.CURRENT_USER, response.getData()); } } HashMap<String, String> hm = new HashMap<>(); // hm.put("token","admin-token"); hm.put("login_token",session.getId()); return JsonResult.createSuccessWithData(hm); } @RequestMapping(value = "info", method = RequestMethod.GET) @ResponseBody @RequireLogin() public JsonResult<User> getinfo(HttpServletRequest request) { User user = (User) request.getAttribute(Consts.CURRENT_USER); return JsonResult.createSuccessWithData(user); } }
test
aa