为了账号安全,请及时绑定邮箱和手机立即绑定

如何在自定义过滤器中使用Java配置注入AuthenticationManager

/ 猿问

如何在自定义过滤器中使用Java配置注入AuthenticationManager

慕后森 2019-10-16 14:48:49

我正在使用Spring Security 3.2和Spring 4.0.1


我正在将xml配置转换为Java配置。当我在“过滤器”中添加注释AuthenticationManager时@Autowired,出现异常


Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.authentication.AuthenticationManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

我已经尝试了注入,AuthenticationManagerFactoryBean但是由于类似的异常也失败了。


这是我正在使用的XML配置


<?xml version="1.0" encoding="UTF-8"?> <beans ...>

    <security:authentication-manager id="authenticationManager">

        <security:authentication-provider user-service-ref="userDao">

            <security:password-encoder ref="passwordEncoder"/>

        </security:authentication-provider>

    </security:authentication-manager>


    <security:http

            realm="Protected API"

            use-expressions="true"

            auto-config="false"

            create-session="stateless"

            entry-point-ref="unauthorizedEntryPoint"

            authentication-manager-ref="authenticationManager">

        <security:access-denied-handler ref="accessDeniedHandler"/>

        <security:custom-filter ref="tokenAuthenticationProcessingFilter" position="FORM_LOGIN_FILTER"/>

        <security:custom-filter ref="tokenFilter" position="REMEMBER_ME_FILTER"/>

        <security:intercept-url method="GET" pattern="/rest/news/**" access="hasRole('user')"/>

        <security:intercept-url method="PUT" pattern="/rest/news/**" access="hasRole('admin')"/>

        <security:intercept-url method="POST" pattern="/rest/news/**" access="hasRole('admin')"/>

        <security:intercept-url method="DELETE" pattern="/rest/news/**" access="hasRole('admin')"/>

    </security:http>

查看完整描述

2 回答

?
烙印99

覆盖方法authenticationManagerBean中WebSecurityConfigurerAdapter暴露出的AuthenticationManager使用内建configure(AuthenticationManagerBuilder)作为一个Spring bean:


例如:


   @Bean(name = BeanIds.AUTHENTICATION_MANAGER)

   @Override

   public AuthenticationManager authenticationManagerBean() throws Exception {

       return super.authenticationManagerBean();

   }


查看完整回答
反对 回复 2019-10-16
?
holdtom

真正帮助我解决这个问题的是“名称= BeanIds.AUTHENTICATION_MANAGER”。没有它,它至少在我的环境中不起作用。

查看完整回答
反对 回复 2019-10-16

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信