1

저는 Spring을 처음 사용하고 있으며 Spring을 사용하여 RESTful API에 대한 인증을 작성하려고합니다. 다음 오류가 나타납니다.이 문제를 어떻게 해결할 수 있습니까?SecurityConfig Java 파일 오류

스택 트레이스 :

Error creating bean with name 'webConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setAuthenticationConfiguration(org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 

의 Web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app> 
    <display-name>Archetype Created Web Application</display-name> 
    <servlet> 
     <servlet-name>springrest</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>springrest</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 


    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
</web-app> 

WebConfig.java

@Configuration 
@EnableWebMvc 
@ComponentScan("com.base") 
public class WebConfig extends WebSecurityConfigurerAdapter{ 

    @Autowired 
    private RestAuthenticationEntryPoint restAuthenticationEntryPoint; 

    @Autowired 
    private MySavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler; 


    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication(). 
     withUser("temporary").password("temporary").roles("ADMIN").and(). 
     withUser("user").password("userPass").roles("USER"); 
    } 

    @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
      .csrf().disable() 
      .exceptionHandling() 
      .authenticationEntryPoint(restAuthenticationEntryPoint) 
      .and() 
      .authorizeRequests() 
      .antMatchers("/api/foos").authenticated() 
      .and() 
      .formLogin() 
      .successHandler(authenticationSuccessHandler) 
      .failureHandler(new SimpleUrlAuthenticationFailureHandler()) 
      .and() 
      .logout(); 
     } 

} 

MySavedRequestAwareAuthenticationSuccessHandler.java

@Component 
public class MySavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { 

    private RequestCache requestCache = new HttpSessionRequestCache(); 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 
      Authentication authentication) throws ServletException, IOException { 
     SavedRequest savedRequest = requestCache.getRequest(request, response); 

     if (savedRequest == null) { 
      clearAuthenticationAttributes(request); 
      return; 
     } 
     String targetUrlParam = getTargetUrlParameter(); 
     if (isAlwaysUseDefaultTargetUrl() 
       || (targetUrlParam != null && StringUtils.hasText(request.getParameter(targetUrlParam)))) { 
      requestCache.removeRequest(request, response); 
      clearAuthenticationAttributes(request); 
      return; 
     } 

     clearAuthenticationAttributes(request); 
    } 

    public void setRequestCache(RequestCache requestCache) { 
     this.requestCache = requestCache; 
    } 
} 
,536 웹 보안 구성

분할 다른 클래스에 WebSecurityConfig에서 WebConfig에 대한

답변

1

사용 @EnableWebSecurity.