java - SecurityConfig Java 文件错误

标签 java xml spring restful-authentication spring-java-config

我是 Spring 新手,正在尝试使用 Spring 为 RESTful API 创建身份验证。我收到以下错误,如何解决此问题?

堆栈跟踪:

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

最佳答案

使用@EnableWebSecurity进行网络安全配置

WebConfigWebSecurityConfig 拆分到不同的类。

关于java - SecurityConfig Java 文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41197522/

相关文章:

java - Spring 组件字段线程安全

java - 用特殊字符分割字符串

java - 让反射在单元测试中工作的问题

java - 如何计算、控制和更新客户信用?

java - 将字符串的内容与模式进行匹配

java - 如何将动态创建的 View 限制为 10 个?

c# - 如何在使用 XmlWriter 执行 XmlSerialization 时添加命名空间?

java - 修改 hibernate 文件以满足我的需要

c# - 避免将 “http://www.w3.org/2001/XMLSchema-instance” 命名空间与 .Net DataContractSerializer 一起使用

c# - 使用 XmlDocument.CreateElement() 创建具有命名空间的 XML 元素