必须指定 Spring Security authenticationmanager - 用于自定义过滤器

标签 spring spring-security

我正在尝试创建自定义用户名密码身份验证过滤器,因为我需要验证来自两个不同来源的密码。我正在使用 Spring Boot 1.2.1 和 Java 配置。部署时我得到的错误是

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customUsernamePasswordAuthenticationFilter' defined in file [/Users/rjmilitante/Documents/eclipse-workspace/login-service/bin/com/elsevier/eols/loginservice/CustomUsernamePasswordAuthenticationFilter.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: authenticationManager must be specified
…
Caused by: java.lang.IllegalArgumentException: authenticationManager must be specified

我不确定我错过了什么。我一直在尝试在我的 SecurityConfig 中为此过滤器设置 authenticationManager。我的代码看起来像

我的过滤器:
@Component
public class CustomUsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    public CustomUsernamePasswordAuthenticationFilter(RequestMatcher requiresAuthenticationRequestMatcher) {
        super(requiresAuthenticationRequestMatcher);
        // TODO Auto-generated constructor stub
    }

    public CustomUsernamePasswordAuthenticationFilter() {
        super(new AntPathRequestMatcher("/login","POST"));
        // TODO Auto-generated constructor stub
    }

    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException {
        // String dbValue = request.getParameter("dbParam");
        // request.getSession().setAttribute("dbValue", dbValue);
        System.out.println("attempting to authentificate");
        while (request.getAttributeNames().hasMoreElements()) {
            String e = (String) request.getAttributeNames().nextElement();
            System.out.println("param name : " + e + " and param value : " + request.getAttribute(e));
        }
        return null;
    }
}

我的安全配置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean(name="loginService")
    public LoginService loginService(){
        return new LoginServiceImpl();
    }

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

    @Bean
    CustomUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter() throws Exception {
        CustomUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter = new CustomUsernamePasswordAuthenticationFilter();
        customUsernamePasswordAuthenticationFilter.setAuthenticationManager(authenticationManagerBean());
      return customUsernamePasswordAuthenticationFilter;
    }


    @Autowired
    private myAuthenticationProvider myAuthenticationProvider;

    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
        .authorizeRequests()
        .anyRequest().authenticated()
        .and()
        /*.addFilterBefore(customUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)*/;

    }

    public void configure(AuthenticationManagerBuilder auth)  throws Exception {

        auth.authenticationProvider(myAuthenticationProvider);
        }
}

有人可以看看吗?不知道怎么回事。

最佳答案

documentationAbstractAuthenticationProcessingFilter声明您必须设置 AuthenticationManager .

我建议您尝试在您的 CustomUsernamePasswordAuthenticationFilter 中添加以下代码类(class):

@Override
@Autowired
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
    super.setAuthenticationManager(authenticationManager);
}

关于必须指定 Spring Security authenticationmanager - 用于自定义过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34233856/

相关文章:

java - Spring boot AuthenticationSuccessHandler 被忽略

java - 使用注释创建有条件运行条件运行 hibernate

java - Spring Data Repository - 没有合格的 bean 注入(inject)

performance - Tomcat Spring - 在本地工作区中创建 Spring Web 应用程序上下文需要 8 分钟

java - Spring Boot - 不允许使用 POST 方法

grails - 当在Kerberos中抛出AuthenticationException时,IE中的无限重定向循环

java - spring中@ModelAttribute、model.addAttribute有什么区别?

java - 具有单个域对象和多个数据源的 Spring Boot

Grails 3.2.4 : custom authentication filter not called

spring-security - 在 SpringSecurity 4 中覆盖 BasicAuthenticationEntryPoint 的最简单方法是什么?