gpt4 book ai didi

java - 试图让 Spring SecurityConfig 与 Spring MVC 和 JavaConfig 一起工作

转载 作者:搜寻专家 更新时间:2023-11-01 02:12:33 25 4
gpt4 key购买 nike

我试图让 Spring SecuirtyConfig 与 Spring MVC 和 JavaConfig 一起工作,但我收到以下错误:

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:251)

这是我的 SampleSimpleWebSecurityConfig:

@Configuration
@EnableWebSecurity
public class SampleSimpleWebSecurityConfig extends SimpleWebSecurityConfig {

private static final Logger logger = LoggerFactory.getLogger(SampleSimpleWebSecurityConfig.class);

protected void authorizeUrls(ExpressionUrlAuthorizationRegistry interceptUrls) {

logger.warn("* * Loadinging Role(s) * *");
interceptUrls
.antMatchers("/*").permitAll()
.antMatchers("/ask-union").hasRole("ROLE_VERIFIED_MEMBER");
}

protected void configure(
SecurityFilterChainSecurityBuilder springSecurityFilterChain) throws Exception {
springSecurityFilterChain
.formLogin()
.permitAll();
}

protected void registerAuthentication(AuthenticationRegistry registry) throws Exception {
registry
.inMemoryAuthentication()
.withUser("xxxx@aol.com.dev").password("testing").roles("ROLE_VERIFIED_MEMBER").and();

}
}

这是我的 web.xml:

 <servlet>
<servlet-name>Spring MVC Dispatcher Servlet</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>org.xxxx.inquiryconfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
<filter-name>securityFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>org.xxxx.inquiryconfig.SampleSimpleWebSecurityConfig</param-name>
<param-value>springSecurityFilterChain</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>securityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

我的旧 Spring Secuiryt XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">


<global-method-security pre-post-annotations="enabled" />

<http use-expressions="true">
<intercept-url access="hasRole('ROLE_VERIFIED_MEMBER')" pattern="/ask**" />
<intercept-url pattern='/*' access='permitAll' />
<form-login default-target-url="/visit" />

<logout logout-success-url="/" />
</http>

<authentication-manager>
<authentication-provider>
<user-service>
<user name="XXXX@aol.com.dev" password="testing" authorities="ROLE_VERIFIED_MEMBER" />
</user-service>

</authentication-provider>
</authentication-manager>
</beans:beans>

最佳答案

您的错误状态:

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:251)

所以只需在您的 web.xml 中注册一个 ContextLoaderListener,在过滤器和 servlet 之前。

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

对于 applicationContext,在您的 web.xml 中添加一个上下文参数

<context-param>
<param-name>contextClass</param-name>
<param-value>com.yourcompany.yourclass</param-value>
</context-param>

其中 com.yourcompany.yourclass 是您的应用程序上下文配置类。

典型的 Spring MVC 应用程序有 2 个上下文,应用程序上下文和 servlet 上下文,其中应用程序是根或父项,servlet 上下文是其子项。 ContextLoaderListener 监听应用程序上下文,默认情况下可以在 /WEB-INF/applicationContext.xml 中找到它,这就是 Spring 在那里寻找它但找不到它的原因。我假设您使用的是 java @Configuration 类,因此请将其指向该类。

关于java - 试图让 Spring SecurityConfig 与 Spring MVC 和 JavaConfig 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15530670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com