Autowired members must be defined in valid Spring bean (@Component|@Service|...)

自定义过滤器BUG

package com.imooc.mall.filter;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.service.UserService;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;

import com.imooc.mall.service.impl.UserServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 描述:     管理员校验过滤器
 */

public class AdminFilter implements Filter {

    @Autowired
    UserService userService;


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain filterChain) throws IOException, ServletException {
        System.out.println("执行了一次自定义的AdminFilter过滤器");

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpSession session = request.getSession();
        User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
        if (currentUser == null) {
            PrintWriter out = new HttpServletResponseWrapper(
                    (HttpServletResponse) servletResponse).getWriter();
            out.write("{\n"
                    + "    \"status\": 10007,\n"
                    + "    \"msg\": \"NEED_LOGIN\",\n"
                    + "    \"data\": null\n"
                    + "}");
            out.flush();
            out.close();
            return;
        }
        //校验是否是管理员
        boolean adminRole = userService.checkAdminRole(currentUser);
        if (adminRole) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            PrintWriter out = new HttpServletResponseWrapper(
                    (HttpServletResponse) servletResponse).getWriter();
            out.write("{\n"
                    + "    \"status\": 10009,\n"
                    + "    \"msg\": \"NEED_ADMIN\",\n"
                    + "    \"data\": null\n"
                    + "}");
            out.flush();
            out.close();
        }
    }

    @Override
    public void destroy() {

    }
}

可以过滤未登录的用户请求,但是

Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第1张图片
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第2张图片
但是无法进一步验证管理员身份(这里我用管理员号登录了)
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第3张图片
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第4张图片
定位到AdminFilter中的问题
在AdminFilter中使用了Bean自动注入
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第5张图片
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第6张图片
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第7张图片
自动注入对象必须定义在有效的spring bean内,即需要将AdminFilter定义为bean,才能在该类中注入其他bean。
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第8张图片
但是将AdminFilter定义为bean后,他会拦截所有的请求。。。
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第9张图片
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第10张图片
正确修改方式:
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第11张图片

未登录前
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第12张图片
登录了普通用户
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第13张图片

登录了管理员用户
Autowired members must be defined in valid Spring bean (@Component|@Service|...)_第14张图片

你可能感兴趣的:(生鲜电商,spring,servlet,java)