
image.png

image.png
將Token寫入Cookie服務(wù)層做不到,只能由表現(xiàn)層來做.
服務(wù)層
package cn.e3mall.sso.service.impl;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import cn.e3mall.common.jedis.JedisClient;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.common.utils.JsonUtils;
import cn.e3mall.mapper.TbUserMapper;
import cn.e3mall.pojo.TbUser;
import cn.e3mall.pojo.TbUserExample;
import cn.e3mall.pojo.TbUserExample.Criteria;
import cn.e3mall.sso.service.LoginService;
@Service
public class LoginServiceImpl implements LoginService {
@Autowired
private TbUserMapper userMapper;
@Autowired
private JedisClient jedisClient;
@Value("${SESSION_EXPIRE}")
private Integer SESSION_EXPIRE;
@Override
public E3Result userLogin(String username, String password) {
// 1、判斷用戶和密碼是否正確
//根據(jù)用戶名查詢用戶信息
TbUserExample example = new TbUserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo(username);
//執(zhí)行查詢
List<TbUser> list = userMapper.selectByExample(example);
if (list == null || list.size() == 0) {
//返回登錄失敗
return E3Result.build(400, "用戶名或密碼錯(cuò)誤");
}
//取用戶信息
TbUser user = list.get(0);
//判斷密碼是否正確
if (!DigestUtils.md5DigestAsHex(password.getBytes()).equals(user.getPassword())) {
// 2、如果不正確,返回登錄失敗
return E3Result.build(400, "用戶名或密碼錯(cuò)誤");
}
// 3、如果正確生成token。
String token = UUID.randomUUID().toString();
// 4、把用戶信息寫入redis,key:token value:用戶信息
user.setPassword(null);
jedisClient.set("SESSION:" + token, JsonUtils.objectToJson(user));
// 5、設(shè)置Session的過期時(shí)間
jedisClient.expire("SESSION:" + token, SESSION_EXPIRE);
// 6、把token返回
return E3Result.ok(token);
}
}
表現(xiàn)層
package cn.e3mall.sso.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
/**
* 展示登錄頁面
* @author Administrator
*
*/
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.e3mall.common.utils.CookieUtils;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.sso.service.LoginService;
@Controller
public class LoginController {
@Autowired
private LoginService loginService;
@Value("${TOKEN_KEY}")
private String TOKEN_KEY;
@RequestMapping("/page/login")
public String showLogin(){
return "login";
}
@RequestMapping(value="/user/login",method=RequestMethod.POST)
@ResponseBody
public E3Result userLogin(String username,String password,
HttpServletRequest request,HttpServletResponse response){
E3Result result = loginService.userLogin(username, password);
//判斷是否登錄成功
if(result.getStatus() == 200){
String token = result.getData().toString();
//登錄成功把token寫入cookie
CookieUtils.setCookie(request, response, TOKEN_KEY, token);
}
return result;
}
}