相关疑难解决方法(0)

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

我正在使用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: {}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过注射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" …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-java-config

74
推荐指数
1
解决办法
9万
查看次数

考虑在配置中定义类型为'org.springframework.security.authentication.AuthenticationManager'的bean

我遵循了这里提到的一些建议,但它对我不起作用.因此,在这里提出问题

  1. 如何在自定义筛选器中使用Java配置注入AuthenticationManager
  2. Spring需要一个'AuthenticationManager'类型的bean

谁能指导我是什么问题以及如何解决这个问题?

错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field authenticationManager in com.techprimers.security.springsecurityauthserver.config.AuthorizationServerConfig required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.
Run Code Online (Sandbox Code Playgroud)

AuthorizationServerConfig.java

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

        security.tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()");
    }


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient("ClientId")
                .secret("secret")
                .authorizedGrantTypes("authorization_code")
                .scopes("user_info")
                .autoApprove(true);
    }


    @Override
    public …
Run Code Online (Sandbox Code Playgroud)

spring spring-security spring-boot

12
推荐指数
3
解决办法
2万
查看次数