当前位置: 首页 > 知识库问答 >
问题:

必须为自定义筛选器指定Spring Security authenticationmanager

郜昊苍
2023-03-14

我正在尝试创建自定义用户名密码身份验证过滤器,因为我需要验证来自两个不同来源的密码。我使用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

我不确定我错过了什么。我一直在尝试在我的安全配置中为此过滤器设置 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);
        }
}

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

共有2个答案

彭炳
2023-03-14

我实际上克服了错误。我只需要从自定义过滤器中删除@Component注释即可。

太叔岳
2023-03-14

AbstractAuthenticationProcessingFilter的文档指出,必须设置AuthenticationManager

我建议您尝试在< code > customusernamepasswordtauthenticationfilter 类中添加以下代码:

@Override
@Autowired
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
    super.setAuthenticationManager(authenticationManager);
}
 类似资料:
  • 存储在Django模型中的元素如下 示例数据如下: . 结果:找到对象- 结果:找到对象- 结果:找到对象- 结果:未找到对象 如何使用过滤器和正则表达式进行这些查询?

  • 我是阿帕奇骆驼的新手。我正试图将头和请求体一起发送到Apache Camel中的路由。 我得到以下错误:

  • 我创建了一个自定义筛选器,用于获取令牌,然后用与令牌相关的角色填充身份验证对象 然后,我将该过滤器添加到springsecuritycontext中,如下所示: 应用程序已经存在,我只是尝试添加Spring Security层。Spring Security版本为4.2.3。在尝试实现此功能的几天后,不会加载,因此不会筛选任何请求。请帮帮忙。

  • 下面是过滤器的外观。 对于WebSecurityConfigurerAdapter#configure(webSecurityWeb)web.忽略()中包含的路径,我希望该筛选器不会像Spring Security筛选器链的其余部分一样激发。 下面是它的样子 我希望在调用Spring Security链的其余部分时调用这个过滤器,并对web.igneration()中的路径忽略这个过滤器,就像Sp

  • 尽管标题出现了,但这并不是一个哲学问题。 从未初始化的数组读取 使用错误数据 使用不可移植构造。(即内存分配的细节1) 导致具有的行为 标准没有要求产生可预测的效果 我会称之为“未定义的行为”。但也许我错过了什么(?) null null

  • 我正在使用高级自定义字段插件,我试图通过分类字段过滤一些自定义帖子,修改WP_Query: 如果我尝试通过文本字段过滤一切正常,WP_Query被修改。但是当字段是一个分类法字段时,我不知道应该传递什么参数,因为它是一个对象。我尝试了分类法名称和分类法ID,但不起作用。 是否可以通过分类字段进行筛选?我应该传递的什么参数?谢谢! 更新-结构: 自定义帖子:“递归操作系统” 自定义分类Slug:'r

  • 我正在尝试将基于Spring MVC xml的项目配置重构为基于Spring Boot java的配置。同时按如下方式设置shiro配置: