New file |
| | |
| | | package com.panzhihua.shop_backstage.filter; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.panzhihua.common.constants.Constants; |
| | | import com.panzhihua.common.constants.TokenConstant; |
| | | import com.panzhihua.common.model.vos.LoginUserInfoVO; |
| | | import com.panzhihua.common.model.vos.R; |
| | | import com.panzhihua.common.model.vos.shop.ShopStoreVO; |
| | | import com.panzhihua.common.service.community.CommunityService; |
| | | import com.panzhihua.common.utlis.AES; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.*; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @ClasssName StoreValidFilter |
| | | * @Description 商铺有效性过滤 |
| | | * @Author cedoo |
| | | * @Date 2021/5/7 |
| | | * @Version 1.0 |
| | | **/ |
| | | @Slf4j |
| | | @Component |
| | | public class StoreValidFilter implements Filter { |
| | | |
| | | @Resource |
| | | private CommunityService communityService; |
| | | |
| | | @Override |
| | | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { |
| | | HttpServletRequest request = (HttpServletRequest) servletRequest; |
| | | HttpServletResponse response = (HttpServletResponse) servletResponse; |
| | | |
| | | String uri = request.getRequestURI(); |
| | | |
| | | if(!"/login".equals(uri)) { |
| | | String userInfo = request.getHeader(TokenConstant.TOKEN_USERINFO); |
| | | boolean empty = ObjectUtils.isEmpty(userInfo); |
| | | if (empty) { |
| | | log.error("获取登录用户信息失败【{}】", request); |
| | | return; |
| | | } |
| | | byte[] bytes = AES.parseHexStr2Byte(userInfo); |
| | | byte[] decrypt = AES.decrypt(bytes, Constants.AES_KEY); |
| | | userInfo = new String(decrypt); |
| | | LoginUserInfoVO loginUserInfoVO = JSONObject.parseObject(userInfo, LoginUserInfoVO.class); |
| | | |
| | | R r = communityService.getUserStoreInfo(loginUserInfoVO.getUserId()); |
| | | boolean shopStoreValid = false; |
| | | if (R.isOk(r) && r.getData() != null) { |
| | | ShopStoreVO shopStoreVO = JSONObject.parseObject(JSONObject.toJSONString(r.getData()), ShopStoreVO.class); |
| | | shopStoreValid = shopStoreVO.getStatus() == 1; |
| | | } |
| | | if (!shopStoreValid) { |
| | | //返回请求被拒绝响应 |
| | | response.setContentType("application/json"); |
| | | response.setCharacterEncoding("UTF-8"); |
| | | response.getWriter().write(JSONObject.toJSONString(R.fail(403))); |
| | | response.flushBuffer(); |
| | | return; |
| | | } |
| | | } |
| | | filterChain.doFilter(servletRequest, servletResponse); |
| | | } |
| | | } |