HandlerInterceptorConfigurer 類中需要
@Autowired
private UserService userService;
但是InterceptorRegistration interceptorRegistration = registry.addInterceptor(new HandlerInterceptorConfigurer());
時userService為空
分析原因 :
new 過的對象不會交給Spring容器管理 所以里面的 userService注入不進來。
解決:
HandlerInterceptorConfigurer類上加上
@Component
HandlerInterceptorConfigurer 使用注入的方式
@Autowired private HandlerInterceptorConfigurer handlerInterceptorConfigurer;
registry.addInterceptor(handlerInterceptorConfigurer);
完整代碼(解決后的)

package com.could.demo.config; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.could.demo.entity.User; import com.could.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.xml.ws.Action; /** * AUTHOR :zhao * 日期:2020/3/20 18:48 * 用戶登錄校驗 * HandlerInterceptorAdapter相當於一個Filter攔截器,但是這個顆粒度更細,能使用Spring的@Autowired注入。 */ @Component public class HandlerInterceptorConfigurer extends HandlerInterceptorAdapter { /** *preHandle在業務處理器處理請求之前被調用。預處理,可以進行編碼、安全控制等處理; * postHandle在業務處理器處理請求執行完成后,生成視圖之前執行。后處理(調用了Service並返回ModelAndView,但未進行頁面渲染),有機會修改ModelAndView; * afterCompletion在DispatcherServlet完全處理完請求后被調用,可用於清理資源等。返回處理(已經渲染了頁面),可以根據ex是否為null判斷是否發生了異常,進行日志記錄; */ @Autowired private UserService userService; /** * 判斷用戶是否登錄 通過session 實現 * @param request * @param response * @param handler * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); User user = (User) session.getAttribute("user"); if (user != null){ QueryWrapper<User> wrapper = new QueryWrapper<>(); User user1 = new User(); user1.setName(user.getName()); wrapper.setEntity(user1); User one = userService.getOne(wrapper); if(one!=null&&user.getPassword().equals(one.getPassword())){ return true; } } // 跳轉登錄 String url = "/index"; response.sendRedirect(url); return false; } }

package com.could.demo.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*; @Configuration public class Config { @Autowired private HandlerInterceptorConfigurer handlerInterceptorConfigurer; /** * AUTHOR :zhao * 日期:2020/3/17 17:55 * 解決跨域 */ @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addInterceptors(InterceptorRegistry registry) { InterceptorRegistration interceptorRegistration = registry.addInterceptor(handlerInterceptorConfigurer); // 排除配置 interceptorRegistration.excludePathPatterns("/error"); interceptorRegistration.excludePathPatterns("/login/**"); interceptorRegistration.excludePathPatterns("/index"); // 攔截配置 interceptorRegistration.addPathPatterns("/**"); } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS") .allowCredentials(true) .maxAge(3600) .allowedHeaders("*"); } }; } /** * mybatis-plus SQL執行效率插件【生產環境可以關閉】 */ @Bean public PerformanceInterceptor performanceInterceptor() { return new PerformanceInterceptor(); } /** * 分頁插件 */ @Bean public PaginationInterceptor paginationInterceptor() { //官網: https://mp.baomidou.com/guide/page.html PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); // 設置請求的頁面大於最大頁后操作, true調回到首頁,false 繼續請求 默認false // paginationInterceptor.setOverflow(false); // 設置最大單頁限制數量,默認 500 條,-1 不受限制 // paginationInterceptor.setLimit(500); // 開啟 count 的 join 優化,只針對部分 left join // paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true)); return paginationInterceptor; } }