试图让Spring SecurityConfig与Spring MVC和JavaConfig一起工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了试图让Spring SecurityConfig与Spring MVC和JavaConfig一起工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4062字,纯文字阅读大概需要6分钟。
内容图文
我试图让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)
因此,只需在过滤器和servlet之前在web.xml中注册ContextLoaderListener.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
对于applicationContext,在web.xml中添加一个context参数
<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类,因此请将其指向该类.
内容总结
以上是互联网集市为您收集整理的试图让Spring SecurityConfig与Spring MVC和JavaConfig一起工作全部内容,希望文章能够帮你解决试图让Spring SecurityConfig与Spring MVC和JavaConfig一起工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。