gpt4 book ai didi

java - Spring Security 需要 ContextLoaderListener 但我正在使用带注释的配置,该怎么办?

转载 作者:搜寻专家 更新时间:2023-10-31 20:31:04 24 4
gpt4 key购买 nike

我为我的 Spring Web 应用程序使用带注释的配置,然后不得不将 XML 与它混合,以便我可以使用 Spring Security。我用 @ImportResource("security-config.xml") 注释了我的一个配置类以加载安全配置。配置 bean 正在创建得很好。我的 web.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="com-timbuk2-webapp-compositor"
version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >

<!-- Spring Security Chain -->
<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>

<!-- Character Encoding -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

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

<!-- URL Rewrite -->
<filter>
<filter-name>urlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>commons</param-value>
</init-param>
<init-param>
<param-name>confPath</param-name>
<param-value>/WEB-INF/conf/urlrewrite-config.xml</param-value>
</init-param>
</filter>

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

<!-- Listeners -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<!-- Context Parameters -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/conf/log4j-config.xml</param-value>
</context-param>

<!-- Servlets -->
<servlet>
<servlet-name>app-dispatcher</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.website.config</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>

<!-- Servlet mappings -->
<servlet-mapping>
<servlet-name>app-dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>


</web-app>

据我了解,springSecurityFilterChain 需要 ContextLoaderListener。但是,由于我的应用程序的配置方式,如果我添加:

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

我的 web.xml 我的应用程序没有初始化。有没有办法在我的注释配置中手动创建 ContextLoaderListener?

最佳答案

只需使用 ContextLoaderListener 创建一个根应用上下文。

<context-param>
<description>The Spring configuration files.</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/application.xml</param-value>
</context-param>

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

在 application.xml 中,您还可以定义基于注解的配置。它将由您的 DispatcherServlet 定义的 WebApplicationContext 继承。

然后您将安全配置导入到 application.xml 中。因此安全性将适用于您配置中的所有 ApplicationContext。

关于java - Spring Security 需要 ContextLoaderListener 但我正在使用带注释的配置,该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3587444/

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