MrShi
昨天 7ee466ebc953bb5640bcf42f2b8e2a87aa471c21
server/services/src/main/java/com/doumee/config/jwt/WebMvcConfig.java
@@ -2,15 +2,18 @@
import com.alibaba.fastjson.JSONObject;
import com.doumee.core.annotation.LoginRequired;
import com.doumee.core.annotation.LoginShopRequired;
import com.doumee.core.constants.Constants;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.dao.business.model.Member;
import com.doumee.dao.business.model.ShopInfo;
import io.jsonwebtoken.JwtException;
import org.apache.commons.lang3.StringUtils;
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;
@@ -30,8 +33,9 @@
    @Autowired
    private JdbcTemplate dao;
    @Resource
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    /**
     * 添加拦截器
@@ -55,7 +59,7 @@
                    //获取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.BE_OVERDUE.getCode(),"未登录");
                    }
@@ -63,7 +67,23 @@
                    //获取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.BE_OVERDUE.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.BE_OVERDUE.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.BE_OVERDUE.getCode(),"未登录");
                    }
@@ -76,12 +96,20 @@
    public Boolean checkLogin(HttpServletRequest request, HttpServletResponse response){
    public Boolean checkMemberLogin(HttpServletRequest request, HttpServletResponse response){
        String token = request.getHeader(JwtTokenUtil.HEADER_KEY);
        try {
            Member member  = jwtTokenUtil.getUserInfoByToken(token);
            if(!token.startsWith(Constants.ZERO+"")){
                throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME);
            }
            String tokenRedis = (String) redisTemplate.opsForValue().get(token);
            if(StringUtils.isBlank(tokenRedis)){
                throw new BusinessException(ResponseStatus.BE_OVERDUE);
            }
            Member member = JSONObject.parseObject(tokenRedis, Member.class);
            if(Objects.isNull(member)){
                throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"长时间未操作,请重新登录");
                throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME);
            }
            Integer isDeleted = dao.queryForObject(" select COALESCE(DELETED,1)  from member where id  = ?", Integer.class, member.getId());
            if(isDeleted.equals(Constants.ONE)){
@@ -89,7 +117,6 @@
            }
            Integer count = dao.queryForObject("select count(1) from member where id  = ?", Integer.class, member.getId());
            if (count != null && count > 0) {
//                jwtTokenUtil.refreshToken(token,member);
                request.setAttribute(JwtTokenUtil.MEMBER_INFO, JSONObject.toJSONString(member));
                request.setAttribute(JwtTokenUtil.MEMBER_ID, member.getId());
                return true;
@@ -101,6 +128,59 @@
        }
    }
    public Boolean checkShopLogin(HttpServletRequest request, HttpServletResponse response){
        String token = request.getHeader(JwtTokenUtil.HEADER_KEY);
        try {
            if(!token.startsWith(Constants.TWO+"")){
                throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME);
            }
            String tokenRedis = (String) redisTemplate.opsForValue().get(token);
            if(StringUtils.isBlank(tokenRedis)){
                throw new BusinessException(ResponseStatus.BE_OVERDUE);
            }
            ShopInfo shop = JSONObject.parseObject(tokenRedis, ShopInfo.class);
            if(Objects.isNull(shop)){
                throw new BusinessException(ResponseStatus.BE_OVERDUE);
            }
            String openid = shop.getOpenid();
            Integer shopId = getTokenId(token);
            Integer isDeleted = dao.queryForObject(" select COALESCE(ISDELETED,0)  from shop_info 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_info where id  = ?", Integer.class, shopId);
            if(isForbidden == Constants.ONE){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"商户已禁用,请联系管理员");
            }
            String dbOpenid = dao.queryForObject(" select ifnull(openid,'')  from shop where id  = ?", String.class, shopId);
            if(StringUtils.isBlank(dbOpenid)||!openid.equals(dbOpenid)){
                throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME);
            }
            Integer count = dao.queryForObject("select count(1) from shop where id  = ?", Integer.class, shopId);
            if (count != null && count > 0) {
                request.setAttribute(JwtTokenUtil.SHOP_INFO, JSONObject.toJSONString(shop));
                request.setAttribute(JwtTokenUtil.SHOP_ID, shop.getId());
                return true;
            }else{
                throw new BusinessException(ResponseStatus.BE_OVERDUE.getCode(),"用户信息出错");
            }
        } catch (IllegalArgumentException | JwtException e) {
            throw new BusinessException(ResponseStatus.BE_OVERDUE);
        }
    }
    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.BE_OVERDUE);
        }
    }
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();