一、将获取的openId(详细步骤见卖家扫码登陆获取openId)作为参数传入到SellerUserController中的login登陆方法。
注:此处设置token,是为了取出时先从cookie中拿出token,再回redis校验
二、将传入的openId去和数据库中的数据进行匹配
三、先在application.yml中设置redis配置,然后再将token设置进redis(token作为value设置进cookie写进response,又作为key设置进redis,相应的value为openId)
四、将Token写进cooike
1、CookieUtil的写法
package com.imooc.utils; import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.HashMap;import java.util.Map; public class CookieUtil { /** * 设置 * @param response * @param name * @param value * @param maxAge */ public static void set(HttpServletResponse response, String name, String value, int maxAge) { Cookie cookie = new Cookie(name, value); cookie.setPath("/"); cookie.setMaxAge(maxAge); response.addCookie(cookie); } /** * 获取cookie * @param request * @param name * @return */ public static Cookie get(HttpServletRequest request, String name) { Map<String, Cookie> cookieMap = readCookieMap(request); if (cookieMap.containsKey(name)) { return cookieMap.get(name); }else { return null; } } /** * 将cookie封装成Map * @param request * @return */ private static Map<String, Cookie> readCookieMap(HttpServletRequest request) { Map<String, Cookie> cookieMap = new HashMap<>(); Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie: cookies) { cookieMap.put(cookie.getName(), cookie); } } return cookieMap; }}
2、
原文地址:https://www.cnblogs.com/bozzzhdz/p/9664627.html
时间: 2024-11-05 22:36:14