| | |
| | | package com.highdatas.mdm.util; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.highdatas.mdm.entity.TUser; |
| | | import com.highdatas.mdm.pojo.CodeMsg; |
| | | import com.highdatas.mdm.pojo.Result; |
| | | import com.highdatas.mdm.service.ITUserService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.LinkedMultiValueMap; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.servlet.HandlerInterceptor; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | |
| | | */ |
| | | |
| | | @Slf4j |
| | | @Service(value = "testInterceptor") |
| | | @Service(value = "requestInterceptor") |
| | | public class CommonInterceptor implements HandlerInterceptor{ |
| | | @Autowired |
| | | ITUserService userService; |
| | | |
| | | UserRoleClient userRoleClient; |
| | | @Autowired |
| | | RedisClient redisClient; |
| | | @Override |
| | | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException { |
| | | String url = request.getRequestURI(); |
| | | log.info(url); |
| | | |
| | | String token = request.getHeader("token"); |
| | | String userId = request.getHeader("userId"); |
| | | if ("/".equalsIgnoreCase(url)) { |
| | | return true; |
| | | } |
| | | if (StringUtils.isEmpty(userId)) { |
| | | log.error("无登陆信息"); |
| | | return false; |
| | | } |
| | | |
| | | token= "U001"; |
| | | if (StringUtils.isEmpty(token)) { |
| | | log.error("token is null"); |
| | | token= "U001"; |
| | | /* response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); |
| | | // String url = "/industryiot/Login.html"; |
| | | // response.sendRedirect(url); |
| | | |
| | | response.setCharacterEncoding("UTF-8"); |
| | | response.setContentType("application/json; charset=utf-8"); |
| | | Result<Object> error = Result.error(CodeMsg.ERROR_TOKEN); |
| | | PrintWriter out = null ; |
| | | out = response.getWriter(); |
| | | out.write(JSON.toJSONString(error)); |
| | | out.flush(); |
| | | out.close(); |
| | | return false;*/ |
| | | } |
| | | HttpSession session = request.getSession(); |
| | | try { |
| | | Object userObj = redisClient.getRedisValObj(userId); |
| | | if (userObj != null) { |
| | | JSONObject userJsonObject = (JSONObject) userObj; |
| | | TUser user = JSONObject.parseObject(userJsonObject.toJSONString(), TUser.class); |
| | | session.setAttribute("user", user); |
| | | return true; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | String baseUrl = ""; |
| | | LinkedMultiValueMap<String, String> body=new LinkedMultiValueMap(); |
| | | |
| | | body.add("token", token); |
| | | //String userId = token; |
| | | // ObjectMapper objectMapper = new ObjectMapper(); |
| | | // objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| | | // TUser user = objectMapper.readValue(login, TUser.class); |
| | | // String userId = HttpUtils.HttpRestClient(baseUrl, HttpMethod.GET, body); |
| | | TUser user = userService.selectOne(new EntityWrapper<TUser>().eq("user_id", userId)); |
| | | TUser user = userRoleClient.getTUserById(userId); |
| | | if (user == null) { |
| | | log.error("user not found by : " + userId); |
| | | response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); |
| | | // String url = "/industryiot/Login.html"; |
| | | // response.sendRedirect(url); |
| | | |
| | | response.setCharacterEncoding("UTF-8"); |
| | | response.setContentType("application/json; charset=utf-8"); |
| | | Result<Object> error = Result.error(CodeMsg.ERROR_TOKEN); |
| | | PrintWriter out = null ; |
| | | out = response.getWriter(); |
| | | Result<Object> error = Result.error(CodeMsg.USER_NOT_MATHED); |
| | | PrintWriter out = response.getWriter(); |
| | | out.write(JSON.toJSONString(error)); |
| | | out.flush(); |
| | | out.close(); |
| | | |
| | | return false; |
| | | } |
| | | |
| | | session.setAttribute("user", user); |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { |
| | | //请求处理后 |
| | | |
| | | } |
| | | } |