簡體   English   中英

Spring 安全性 - 自定義 AuthenticationProvider 不起作用 - Java Config

[英]Spring security - Custom AuthenticationProvider not working - Java Config

我已經為標題中的問題苦苦掙扎了幾天,我感到非常沮喪。 我不知道我做錯了什么以及為什么我的實現不起作用。

讓我告訴你我有什么:

自定義身份驗證提供程序:

@Component
public class AuthProvider implements AuthenticationProvider {

    private Logger logger = LoggerFactory.getLogger(AuthProvider.class);

    public AuthProvider() {
        logger.info("Building...");
    }

    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        logger.info("Authenticate...");
        return null;
    }

    public boolean supports(Class<?> authentication) {
        logger.info("Supports...");
        return true;
    }
}

網絡安全配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthProvider authProvider;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authProvider);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests().anyRequest().authenticated();
    }
}

如您所見,我已將記錄器添加到 AuthenticationProvider 中,但並未調用任何記錄器。

我試過的:

  • 添加@Autowiredconfigure AuthenticationManagerBuilder位置
  • @EnableGlobalMethodSecurity(prePostEnabled=true)到類中
  • 將自定義AuthenticationProvider直接添加到HttpSecurity

我如何測試它:

  • 通過 IntelliJ 調試 - 沒有結果,沒有斷點被調用。
  • 運行應用程序並發送請求 - 也沒有結果,沒有日志,什么也沒有。

請伙計們以某種方式幫助我。 我沒電了我討厭在應該有效的事情上浪費太多時間:(

您可能錯過了 WebSecurityConfigurerAdapter 中的以下方法:

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

我也遇到了同樣的情況。

使用isAssignableFrom()方法而不是==equals我們得到一個 true,然后流將通過authenticate()

override fun supports(authentication: Class<*>): Boolean {
    return UsernamePasswordAuthenticationToken::class.java.isAssignableFrom(authentication)
}

GL

來源

暫無
暫無

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

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