在安全过滤器中注入一个bean(Spring)

时间:2014-05-23 14:29:28

标签: java spring dependency-injection spring-security spring-bean

在登录之前,我添加了一个过滤器,用于检查计算机名称是否已在数据库中注册。为此,我在过滤器中注入了一个存储库。问题是它将bean标记为null,并且我读取了过滤器并且bean在不同的上下文中。

如何在过滤器中注入bean?

这是豆子注射

@Qualifier("simpleRepo")
    @Autowired
    private SimpleRepo simpleRepo;

这就是我想使用存储库方法的方法

@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        response.setContentType("text/html");


        String computerName = null;
        String remoteAddress = request.getRemoteAddr();
        try {
            InetAddress inetAddress = InetAddress.getByName(remoteAddress);
            computerName = inetAddress.getHostName();

            if (computerName.equalsIgnoreCase("localhost")) {
                computerName = java.net.InetAddress.getLocalHost().getCanonicalHostName();
            }
        } catch (UnknownHostException e) {

        }

        Response response_query = new Response();

        System.out.println("computerName: " + computerName);

        response_query = simpleRepo.isARegisteredMachine(computerName);

        System.out.println(response_query.getStatus());

        //httpServletResponse.sendRedirect("/signin");
        chain.doFilter(request, response);
    }

0 个答案:

没有答案