找不到具有自定义表单登录名的authenticationprovider

jhiyze9q  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(264)

我正在编写一个带有自定义表单登录的spring boot应用程序。我试图这样做,当我试图登录应用程序时,我得到了这个错误。
org.springframework.security.authentication.providernotfoundexception:在org.springframework.security.authentication.providermanager.authentication(providermanager.java:251)上找不到.configuration.security.web.customauthenticationtoken的authenticationprovider提供程序我尝试了很多方法,但都不起作用。错误在哪里?我找不到它。

@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
    @EnableWebSecurity
    @Configuration
    @RequiredArgsConstructor
    @Slf4j

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.addFilterBefore(authenticationFilter(), CustomAuthenticationFilter.class)
                .authorizeRequests()                    .mvcMatchers(PublicUrls.URLS).permitAll()                    .anyRequest().fullyAuthenticated().and() .formLogin().loginPage("/login").defaultSuccessUrl("/").permitAll()
                .and()
                .cors()
                .and()
                .logout()
                .invalidateHttpSession(true)
                .clearAuthentication(true)
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login?logout")
                .permitAll();
    }

    public CustomAuthenticationFilter authenticationFilter() throws Exception {
        CustomAuthenticationFilter filter = new CustomAuthenticationFilter();
        filter.setAuthenticationManager(authenticationManagerBean());
        filter.setAuthenticationFailureHandler(failureHandler());
        return filter;
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authProvider());
    }

    public AuthenticationProvider authProvider() {
        CustomAuthenticationProvider provider
                = new CustomAuthenticationProvider();
        return provider;
    }

    public SimpleUrlAuthenticationFailureHandler failureHandler() {
        return new SimpleUrlAuthenticationFailureHandler("/login?error=true");
    }

customauthenticationtoken是

public class CustomAuthenticationToken extends UsernamePasswordAuthenticationToken {

    @Getter
    private String surname;
    private String password;
    private String birthDate;

    public CustomAuthenticationToken(Object principal, Object credentials, String surname, String password, String birthDate) {
        super(principal, credentials);
        this.surname = surname;
        this.password = password;
        this.birthDate = birthDate;
        super.setAuthenticated(false);
    }

    public CustomAuthenticationToken( String surname, String password, String birthDate) {
        super(null, null);
        this.surname = surname;
        this.password = password;
        this.birthDate = birthDate;
        super.setAuthenticated(false);
    }

    public CustomAuthenticationToken(Object principal, Object credentials, String surname, String password, String birthDate,
        Collection<? extends GrantedAuthority> authorities) {
        super(principal, credentials, authorities);
        this.surname = surname;
        this.password = password;
        this.birthDate = birthDate;
        super.setAuthenticated(true); // must use super, as we override
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题