我的问题是关于“将bean放入配置类中”

时间:2018-11-23 16:25:26

标签: java spring

说明:

  

security.demo.config.SecurityConfig中的字段entryPoint需要找不到类型为'security.demo.config.jwt.JwtAuthenticationEntryPoint'的bean。

操作:

  

考虑在您的配置中定义类型为“ security.demo.config.jwt.JwtAuthenticationEntryPoint”的bean。

如果我有这些配置类,如何解决此错误?


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;
    @Autowired
    UserService userService;
    @Autowired
    private JwtAuthProvider autheticationProvider;
    @Autowired
    private JwtAuthenticationEntryPoint entryPoint;

    @Bean
    public AuthenticationManager authenticationManager() {
        return new ProviderManager(Collections.singletonList(autheticationProvider));
    }

    //create a custom filter
    @Bean
    public JwtAuthFilter authTokenFilter() {
        JwtAuthFilter filter =new JwtAuthFilter();
        filter.setAuthenticationManager(authenticationManager());
        filter.setAuthenticationSuccessHandler(new JwtSuccessHandler());
        return filter;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery("select email as principal, password as credentials, true from users where email=?")
                .authoritiesByUsernameQuery("select user_email as principal, role_name as role from user_roles where user_email=?")
                .passwordEncoder(passwordEncoder()).rolePrefix("ROLE_");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/register**", "/forgot-password**", "/reset-password**").permitAll()
                .antMatchers("/resources/**", "/register**").permitAll()
                .antMatchers("/users", "/addTask")
                .hasRole("ADMIN")
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .defaultSuccessUrl("/profile")
                .and()
                .logout()
                .invalidateHttpSession(true)
                .clearAuthentication(true)
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/")
                .and().rememberMe().key("unique-and-secret").rememberMeCookieName("remember-me-cookie-name").tokenValiditySeconds(24 * 60 * 60);
        http.exceptionHandling().authenticationEntryPoint(entryPoint);
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http.addFilterBefore(authTokenFilter(), UsernamePasswordAuthenticationFilter.class);

    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

JwtAuthenticationEntryPoint类是:

    public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest arg0, HttpServletResponse arg1, AuthenticationException arg2)
            throws IOException, ServletException {
        arg1.sendError(HttpServletResponse.SC_UNAUTHORIZED,"UNAUTHARIZED");

    }
}

1 个答案:

答案 0 :(得分:0)

您需要在@Component类上添加JwtAuthenticationEntryPoint