繁体   English   中英

AuthenticationSuccessHandler警告没有合格的Bean

[英]AuthenticationSuccessHandler alerts no qualifying bean

当尝试将Spring Security 4.2.3与AuthenticationSuccessHandler一起使用时出现警告时,我遇到了一些问题:

创建名称为'customWebSecurityConfigurerAdapter'的bean时出错:通过字段'customAuthenticationSuccesshandler'表示的不满意依赖性; 嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为“ com.test.smsportal.common.filter.CustomAuthenticationSuccessHandler”的合格Bean。

奇怪的是,我已经为成功处理程序声明了@Component。

以下是我的CustomAuthenticationSuccessHandler:

package com.test.smsportal.common.filter;

@Component
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException {
        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains("ADMIN")) {
            response.sendRedirect("admin/home.html");
        } else if (roles.contains("USER")) {
            response.sendRedirect("static/user.html");
        }
    }
}

以下是WebSecurityConfigurerAdapter:

package com.test.smsportal.configuration;

import com.test.smsportal.common.filter.CustomAuthenticationSuccessHandler;

@EnableWebSecurity
@Configuration
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Autowired
    CustomAuthenticationSuccessHandler customAuthenticationSuccesshandler;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user") // #1
                .password("password").roles("USER").and().withUser("admin") // #2
                .password("password").roles("ADMIN", "USER");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/public/**"); // #3
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().antMatchers("/signup", "/about").permitAll() // #4
                .antMatchers("/admin/**").hasRole("ADMIN")// #6
                .anyRequest().authenticated() // 7
                .and().formLogin() // #8
                .loginPage("/login") // #9
                .successHandler(customAuthenticationSuccesshandler); // #5
                //.permitAll().defaultSuccessUrl("/static/user.html");
    }
}

以下是我的WebMvcConfigurerAdapter:

package com.sgx.smsportal.configuration;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.test.smsportal.controller", 
"com.test.smsportal.service", "com.test.smsportal.dao",
    "com.test.smsportal.common.filter" })
public class MvcConfiguration extends WebMvcConfigurerAdapter {
 //something
}

问题是MvcConfiguration中的@ComponentScan不包含com.test.smsportal.configuration包,而CustomWebSecurityConfigurerAdapter也没有包含com.test.smsportal.common.filter@ComponentScan

你可以做

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.test.smsportal.controller", 
"com.test.smsportal.service", "com.test.smsportal.dao",
    "com.test.smsportal.common.filter", "com.test.smsportal.configuration"})
public class MvcConfiguration extends WebMvcConfigurerAdapter {
 //something
}

要么

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.test.smsportal.controller", 
"com.test.smsportal.service", "com.test.smsportal.dao",
    "com.test.smsportal.common.filter"})
@Import({CustomWebSecurityConfigurerAdapter.class})
public class MvcConfiguration extends WebMvcConfigurerAdapter {
 //something
}

要么

@EnableWebSecurity
@Configuration
@ComponentScan(basePackages = { "com.test.smsportal.common.filter" })
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM