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

sar*_*eeh 9 java security authentication spring spring-security

我已经为标题中的问题苦苦挣扎了几天,我感到非常沮丧。我不知道我做错了什么以及为什么我的实现不起作用。

让我告诉你我有什么:

自定义身份验证提供程序:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

网络安全配置:

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我已将记录器添加到 AuthenticationProvider 中,但并未调用任何记录器。

我试过的:

  • 添加@Autowiredconfigure其中的AuthenticationManagerBuilder
  • 添加@EnableGlobalMethodSecurity(prePostEnabled=true)到班级
  • AuthenticationProvider直接添加自定义HttpSecurity

我如何测试它:

  • 通过 IntelliJ 调试 - 没有结果,没有断点被调用。
  • 运行应用程序并发送请求 - 也没有结果,没有日志,什么也没有。

请伙计们以某种方式帮助我。我没电了 我讨厌在应该有效的事情上浪费太多时间:(

Jul*_*ane 7

您可能错过了 WebSecurityConfigurerAdapter 中的以下方法:

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}
Run Code Online (Sandbox Code Playgroud)

我也遇到了同样的情况。