代码之家  ›  专栏  ›  技术社区  ›  xyzwlxp

AuthenticationSuccessHandler警报没有符合条件的bean

  •  0
  • xyzwlxp  · 技术社区  · 8 年前

    我在尝试将spring security 4.2.3与AuthenticationSuccessHandler一起使用时遇到了一些问题,因为它会发出警报:

    创建名为“CustomWebSecurityConfigureAdapter”的bean时出错: 通过字段表示的未满足的依赖关系 'customAuthenticationSuccesshandler';嵌套异常为 组织。springframework。豆。工厂NoSuchBeanDefinitionException:否 类型的限定bean 'com。测验smsportal。常见的滤器CustomAuthenticationSuccessHandler'。

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

    下面是我的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");
            }
        }
    }
    

    以下是WebSecurity配置适配器:

    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");
        }
    }
    

    以下是我的WebMVCConfigureAdapter:

    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
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   shazin    8 年前

    问题是 @ComponentScan 在里面 MvcConfiguration 不包括包装 com.test.smsportal.configuration CustomWebSecurity配置适配器也不包括 @组件扫描 覆盖 com.test.smsportal.common.filter .

    你可以

    @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 {
    
    }
    
    推荐文章