簡體   English   中英

Bean無法在類中自動裝配

[英]Bean cannot be autowired in a class

我正在使用JWT (Json Web Token)來保護我的Spring啟動應用程序。

有一個類不接受任何bean。

首先,我想也許我想要注入的bean沒有定義。 所以我決定使用spring ApplicationContext打印bean名稱列表。 但我發現即使ApplicationContext也不能注入此類:

知道為什么會這樣嗎?

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Arrays;

public class JWTLoginFilter extends AbstractAuthenticationProcessingFilter {

    @Autowired
    private TokenAuthenticationService tokenAuthenticationService;

    @Autowired
    private ApplicationContext applicationContext;

    public JWTLoginFilter(String url, AuthenticationManager authManager) {
        super(new AntPathRequestMatcher(url));
        setAuthenticationManager(authManager);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest req,
            HttpServletResponse res) throws AuthenticationException,
            IOException, ServletException {

        CustomUserDetails creds = new ObjectMapper().readValue(
                req.getInputStream(), CustomUserDetails.class);

        return getAuthenticationManager().authenticate(
                new UsernamePasswordAuthenticationToken(creds.getUsername(),
                        creds.getPassword()));
    }

    @Override
    protected void successfulAuthentication(HttpServletRequest req,
            HttpServletResponse res, FilterChain chain, Authentication auth) {
        java.util.List s = Arrays.asList(applicationContext.getBeanDefinitionNames());
        System.out.println(s);
        tokenAuthenticationService.addAuthentication(res, auth.getName());
    }
}

JWTLoginFilter也必須是一個bean,以允許Spring注入其他bean。 目前,Spring對此沒有任何控制權。 使用@Component / @Service / @Repository注釋標記類(取決於過濾器扮演的角色,我認為@Component是一個不錯的選擇)將解決問題。

編輯1:

JWTLoginFilter需要一個無法找到的java.lang.String類型的bean。 組件可以有構造函數嗎?

問題:Spring試圖使用雙參數構造函數來創建一個bean,並且期望這兩個參數是它的bean。 但事實並非如此,因為沒有帶有String類的bean。

解決方案:您應該定義一個非參數構造函數,以允許Spring生成一個沒有問題的未調整實例。 然后創建setter以提供Spring將用於注入所需依賴項的方法。

編輯2:

解決方法是定義一個String bean(在@Configuration類中),它將被注入到JWTLoginFilter構造函數中,但我不確定你的過濾器是否需要一些外部依賴項。

@Bean
public String getStringPatternBean() {
    return "pattern";
}

好吧,我最終決定改變這些類的設計方式。 我在TokenAuthenticationService靜態方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM