Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java WebSecurity配置适配器添加到非Servlet 3.0容器中的根上下文_Java_Spring Security_Annotations - Fatal编程技术网

Java WebSecurity配置适配器添加到非Servlet 3.0容器中的根上下文

Java WebSecurity配置适配器添加到非Servlet 3.0容器中的根上下文,java,spring-security,annotations,Java,Spring Security,Annotations,我使用了一个示例web应用程序作为示例,将现有的Servlet2.4应用程序从基于XML的安全配置转换为新的基于Java配置的样式,如下所述 我的安全配置如下: @Configuration @EnableWebSecurity @Slf4j public class WebSecurityConfig extends WebSecurityConfigurerAdapter { public WebSecurityConfig() { log.info("init"); } @Ov

我使用了一个示例web应用程序作为示例,将现有的Servlet2.4应用程序从基于XML的安全配置转换为新的基于Java配置的样式,如下所述

我的安全配置如下:

@Configuration
@EnableWebSecurity
@Slf4j
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

public WebSecurityConfig() {
    log.info("init");
}

@Override
protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
}

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

@Override
protected void configure(HttpSecurity http) throws Exception {
  http
    .authorizeUrls()
      .antMatchers("/","/home").permitAll() 
      .antMatchers("/secure/**").hasRole("USER")
      .anyRequest().authenticated()
      .and()
  .formLogin() 
      .loginUrl("/login") 
      .permitAll();
}
}
问题在于文档/示例基于Servlet3.0规范,而Servlet3.0规范不使用web.xml

该文档使用AbstractAnnotationConfigDispatchersServletInitializer,但我不能在Servlet2.4容器中使用它

我使用web.xml引导我的MVC应用程序,如下所示:

<servlet>
    <servlet-name>my-spike</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
           org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.example.config.WebMvcConfig</param-value>
    </init-param>
</servlet>

我的钉子
org.springframework.web.servlet.DispatcherServlet
上下文类
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
上下文配置位置
com.example.config.WebMvcConfig

因此,问题是如何将我的WebSecurity配置添加到web.xml或Java代码中的根应用程序上下文中?

您应该能够通过
ContextLoaderListener
以以下方式配置它:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value> package.WebSecurityConfig</param-value>
</context-param>
<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

org.springframework.web.context.ContextLoaderListener
上下文配置位置
package.WebSecurityConfig
上下文类
org.springframework.web.context.support.AnnotationConfigWebApplicationContext