Springboot关于拦截器或者过滤器无法注入bean的问题

参考别人: https://segmentfault.com/a/1190000012107467

WebMvcConfig代码:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
IUserPropertyService userPropertyService;

@Autowired
FeedBackMapper feedBackMapper;

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new CheckFilter(userPropertyService,feedBackMapper)).addPathPatterns("/static/index.html").
excludePathPatterns("/error");
System.err.println("加载1");
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
System.err.println("加载2");
}

@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/static/",".html");
System.err.println("加载3");
}
}
过滤器: CheckFilter
public class CheckFilter implements HandlerInterceptor {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
private IUserPropertyService userPropertyService;
private static final String ERROR_PAGE = "/static/error.html";

private static final String APPID = "xxx";

public static final String SECRET = "xxx";
public static final String WECHART_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";

private FeedBackMapper feedBackMapper;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
String code = request.getParameter("code");
String propertyId = request.getParameter("propertyId");
String pushDate = request.getParameter("pushDate");
if (StringUtils.isEmpty(code)) {
String encode = URLEncoder.encode("http://di.qdingnet.com/diapi/qd-pmonthly/static/index.html?propertyId=" + propertyId, "UTF-8");
response.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxx&redirect_uri=" + encode + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
return false;
}
String respJson = HttpRequest.get(String.format(WECHART_URL, APPID, SECRET, code)).send().bodyText();
logger.info("获取微信openid返回:{}", respJson);
JSONObject jsonObject = JSON.parseObject(respJson);
String openId = jsonObject.getString("openid");
logger.info("获取微信openidc参数:{}", openId);
if (StringUtils.isEmpty(openId)) {
response.sendRedirect(ERROR_PAGE);
return false;
}
QueryWrapper<UserProperty> wrapper = new QueryWrapper<>();
wrapper.eq("open_id", openId);
Map<String, Object> map = userPropertyService.getMap(wrapper);
String status = feedBackMapper.getEntity(openId, pushDate);
if (StringUtils.isNotEmpty(status) && "0".equals(status)) {
feedBackMapper.updateEntity(openId, pushDate);
}

logger.info("userPropertyServiceGetMap", JSON.toJSONString(map));
if (map != null) {
logger.info("pass openId:", openId);
logger.info("queryResp:", JSON.toJSONString(map));
response.setHeader("propertyId", propertyId);
return true;
}
logger.info("invalid openId:", openId);
response.sendRedirect(ERROR_PAGE);
return false;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
}

public CheckFilter(IUserPropertyService userPropertyService, FeedBackMapper feedBackMapper) {
this.userPropertyService = userPropertyService;
this.feedBackMapper = feedBackMapper;
}

}
posted @ 2020-01-13 11:19  快到碗来  阅读(510)  评论(0)    收藏  举报