03登錄

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;
}


}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容