项目的登入登出
1、登陆登出的逻辑:
在用户登陆时随机生成一个TOKEN的字符串,将该字符串存放到redis中,键为随机生成的字符串,本项目中为一个固定前缀加一个UUID随机生成的字符串,
值为用于唯一标识,本项目中为用户的openid,并设置过期时间;然后在后台生成一个Cookie,将key作为Cookie的值,“token”为键存在Cookie中,设置过期时间,并将Cookie通过HttpServletResponse
返回给前台用户浏览器。
在用户登出时,从HttpServletRequest中Cookie中取出key为“token”的Cookie,从该Cookie中取出value即存在redis中用户唯一标识的键,并从Redis从删除该值,并将该Cookie也删除,从而完成用户登出。
2、实现
(1)、首先是redis的使用:
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
(2)、Conrtroller:
@Controller
@RequestMapping("/seller")
public class SellerInfoController {
@Autowired
private SellerInfoService sellerInfoService;
//注入stringRedisTemplate操作Redis
@Autowired
private StringRedisTemplate stringRedisTemplate;
//注入项目配置常量
@Autowired
private ProjectConfig projectConfig;
@GetMapping("/login")
public ModelAndView login(@RequestParam("openid")String openid,
HttpServletResponse response,
Map<String,Object> map) {
SellerInfo sellerInfo = sellerInfoService.findSellerByOpenid(openid);
if (sellerInfo==null) {
map.put("msg", ResultEums.LOGIN_FAIL.getMessage());
map.put("url","/sell/seller/order/list");
return new ModelAndView("/common/error",map);
}
//设置token至redis
Integer expire = RedisConstant.EXPIRE;
String token = UUID.randomUUID().toString();
stringRedisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),openid,expire,TimeUnit.SECONDS);
//设置token至cookie
CookieUtil.set(response,"token",CookieConstrant.TOKEN,expire);
return new ModelAndView("redirect:"+projectConfig.getSell()+"/sell/seller/order/list");
}
public ModelAndView logout(HttpServletRequest request,
HttpServletResponse response,
Map<String,Object> map) {
//从cookie中得到token
Cookie cookie = CookieUtil.get(request,CookieConstrant.TOKEN);
if (cookie!=null) {
stringRedisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));
}
//清楚cookie
CookieUtil.set(response,CookieConstrant.TOKEN,null,0);
map.put("msg",ResultEums.LOGOUT_OUT_SUCCESS.getMessage());
map.put("url","/sell/seller/order/list");
return new ModelAndView("/common/",map);
}
其中CookieUtil:
public class CookieUtil {
public static void set(HttpServletResponse response,String key,String value,int maxAge) {
Cookie cookie = new Cookie("token", CookieConstrant.TOKEN);
cookie.setMaxAge(maxAge);
cookie.setPath("/");
response.addCookie(cookie);
}
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;
}
}
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;
}
}
SellerInfoService:
@Slf4j
@Service
public class SellerInfoServiceImpl implements SellerInfoService {
@Autowired
private SellerInfoRepository sellerInfoService;
@Override
public SellerInfo findSellerByUsername(String username) {
SellerInfo sellerInfo = sellerInfoService.findByUsername(username);
return sellerInfo;
}
@Override
public SellerInfo findSellerByOpenid(String openid) {
SellerInfo sellerInfo = sellerInfoService.findByOpenid(openid);
return sellerInfo;
}
}
其中几个配置常量:
public interface CookieConstrant {
String TOKEN="token";
Integer EXPIRE=7200;
}
public interface RedisConstant {
String TOKEN_PREFIX="token_%s";
Integer EXPIRE=7200;
}
3、关于redis的使用:
stringRedisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),openid,expire,TimeUnit.SECONDS);
stringRedisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));