[英]spring security custom inject AuthenticationProvider inside AuthenticationManager
[英]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 中,但並未調用任何記錄器。
我試過的:
@Autowired
以configure
AuthenticationManagerBuilder
位置@EnableGlobalMethodSecurity(prePostEnabled=true)
到類中AuthenticationProvider
直接添加到HttpSecurity
我如何測試它:
請伙計們以某種方式幫助我。 我沒電了我討厭在應該有效的事情上浪費太多時間:(
您可能錯過了 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.