| | |
| | | package com.doumee.config.Jwt; |
| | | |
| | | import com.doumee.config.annotation.LoginRequired; |
| | | import com.doumee.config.annotation.LoginShopRequired; |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.exception.BusinessException; |
| | | import com.doumee.core.utils.Constants; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.jdbc.core.JdbcTemplate; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import org.springframework.web.method.HandlerMethod; |
| | |
| | | |
| | | @Autowired |
| | | private JdbcTemplate dao; |
| | | |
| | | @Autowired |
| | | private RedisTemplate<String,Object> redisTemplate; |
| | | |
| | | /** |
| | | * 添加拦截器 |
| | |
| | | //获取token |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 从 http 请求头中取出 token |
| | | if (StringUtils.isNotBlank(token)) { |
| | | checkLogin(request,response); |
| | | checkMemberLogin(request,response); |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | |
| | | //获取token |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 从 http 请求头中取出 token |
| | | if (StringUtils.isNotBlank(token)) { |
| | | checkLogin(request,response); |
| | | checkMemberLogin(request,response); |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | }else if (beanType.isAnnotationPresent(LoginShopRequired.class)) { |
| | | //获取token |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 从 http 请求头中取出 token |
| | | if (StringUtils.isNotBlank(token)) { |
| | | checkShopLogin(request,response); |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | }else if (handlerMethod.hasMethodAnnotation(LoginShopRequired.class)){ |
| | | //获取token |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 从 http 请求头中取出 token |
| | | if (StringUtils.isNotBlank(token)) { |
| | | checkShopLogin(request,response); |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | public Boolean checkLogin(HttpServletRequest request, HttpServletResponse response){ |
| | | public Boolean checkMemberLogin(HttpServletRequest request, HttpServletResponse response){ |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); |
| | | try { |
| | | //判断Token是否超时 |
| | | boolean expiration = JwtTokenUtil.isTokenExpired(token); |
| | | if (expiration) { |
| | | if(!token.startsWith(Constants.ZERO+"")){ |
| | | throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"长时间未操作,请重新登录"); |
| | | } |
| | | //获取账号ID |
| | | Integer memberId = JwtTokenUtil.getJwtPayLoad(token).getMemberId(); |
| | | String tokenRedis = (String) redisTemplate.opsForValue().get(token); |
| | | if(StringUtils.isBlank(tokenRedis)){ |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | Integer memberId = getTokenId(token); |
| | | Integer isDeleted = dao.queryForObject(" select COALESCE(ISDELETED,0) from Member where id = ?", Integer.class, memberId); |
| | | if(isDeleted== Constants.ONE){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已删除,请联系管理员"); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | public Boolean checkShopLogin(HttpServletRequest request, HttpServletResponse response){ |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); |
| | | try { |
| | | if(!token.startsWith(Constants.ONE+"")){ |
| | | throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"长时间未操作,请重新登录"); |
| | | } |
| | | String tokenRedis = (String) redisTemplate.opsForValue().get(token); |
| | | if(StringUtils.isBlank(tokenRedis)){ |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | Integer shopId = getTokenId(token); |
| | | Integer isDeleted = dao.queryForObject(" select COALESCE(ISDELETED,0) from shop where id = ?", Integer.class, shopId); |
| | | if(isDeleted== Constants.ONE){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已删除,请联系管理员"); |
| | | } |
| | | Integer isForbidden = dao.queryForObject(" select COALESCE(STATUS,0) from shop where id = ?", Integer.class, shopId); |
| | | if(isForbidden== Constants.ONE){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已禁用,请联系管理员"); |
| | | } |
| | | Integer count = dao.queryForObject("select count(1) from shop where id = ?", Integer.class, shopId); |
| | | if (count != null && count > 0) { |
| | | request.setAttribute(JwtTokenUtil.ShopId_Name, shopId); |
| | | return true; |
| | | }else{ |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"用户信息出错"); |
| | | } |
| | | } catch (IllegalArgumentException | JwtException e) { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | } |
| | | |
| | | |
| | | public Integer getTokenId(String token){ |
| | | try { |
| | | Integer lastIndex = token.lastIndexOf("_")+1; |
| | | Integer tokenId = Integer.valueOf(token.substring(lastIndex)); |
| | | return tokenId; |
| | | }catch (Exception e){ |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Bean |
| | | public RestTemplate getRestTemplate(){ |
| | | return new RestTemplate(); |