Next, ajax carries Cookie information across domains. After receiving Cookie information, the server in the project needs to query database user information for login verification.
According to the usual habit of using the annotation @ Autowired directly, it will report the service null pointer exception. There are many descriptions on the Internet for the specific reasons, which are not detailed here.
To solve this problem, the answer on the Internet is basically to use the spring filter agent or the ApplicationContext object in the Filter to get the bean.
But our project is built with dubbo, and we can't get bean objects with these methods. Finally, we use the annotation @ Component to solve the problem.
The package path of the scan interceptor must be configured in the spring xml configuration file.
1. web.xml configuration Interceptor:
<filter> <filter-name>headFilter</filter-name> <filter-class>com.jdy.filter.HeaderFilter</filter-class> </filter> <filter-mapping> <filter-name>headFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2. spring.xml configuration scanning path:
<!-- Normal scan configuration --> <context:component-scan base-package="com.jdy.**.controller"></context:component-scan> <!-- Interceptor scan configuration --> <context:component-scan base-package="com.jdy.filter"></context:component-scan>
3. Class code of interceptor is as follows:
package com.jdy.filter; import ........; @Component public class HeaderFilter implements Filter { private static final Logger logger = LoggerFactory.getLogger(HeaderFilter.class); @Autowired private AdServiceApi adServiceApi; @Autowired private UserServiceApi userServiceApi; @Autowired private SysTokenServiceApi sysTokenServiceApi; private static HeaderFilter headerFilter; public void setAdServiceApi(AdServiceApi adServiceApi) { this.adServiceApi = adServiceApi; } public void setSysTokenServiceApi(SysTokenServiceApi sysTokenServiceApi) { this.sysTokenServiceApi = sysTokenServiceApi; } public void setUserServiceApi(UserServiceApi userServiceApi) { this.userServiceApi = userServiceApi; } @PostConstruct public void init() { headerFilter = this; headerFilter.adServiceApi = this.adServiceApi; headerFilter.sysTokenServiceApi = this.sysTokenServiceApi; headerFilter.userServiceApi = this.userServiceApi; } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; try { logger.info("----------------Access interceptor---------------"); //Get login information String token = CookieUtils.getCookieValue(request, "token"); //Query user information String sysToken = headerFilter.sysTokenServiceApi.getByReqSign(token); //TODO: business processing ............ }catch (Exception ex){ logger.info("System exception!"); ex.printStackTrace(); } logger.info("------------End of interception----------"); filterChain.doFilter(request, response); } @Override public void destroy() { } }
Note: when calling service, it must be called by the current class name variable. Service. Interface name.