gpt4 book ai didi

java - 使用集成的 Spring 安全访问 Spring 3 MVC 应用程序中的静态内容

转载 作者:行者123 更新时间:2023-11-29 05:46:56 24 4
gpt4 key购买 nike

我已经通过 Spring Tool Suite 模板创建了 Spring 3 MVC 项目,并且在那里集成了 Spring 安全性。除了访问静态内容之外,一切正常。

当我只创建 MVC 应用程序并将我的静态内容放在 /src/webapp/WEB-INF/resources/ 中时然后放<resources mapping="/resources/**" location="src/main/webapp/WEB-INF/resources" />到我的applicationContext.xml ,它运行良好......但我无法将此代码添加到我的 applicationContext.xml具有安全性...代码甚至无法编译...知道要将什么写入我的 web.xml 才能使其正常工作吗?

我的 applicationContext.xml文件看起来像这样:

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

<context:annotation-config />

<context:component-scan base-package="cz.cvut.fit.genepi.controllers" />

<import resource="classpath:applicationContext-security.xml" />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

奇怪的是,当使用上面的代码时,映射 View Controller 工作正常,但是当我使用它时,我得到这个错误 The prefix mvc:resources is not bound

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

<context:annotation-config />
<context:component-scan base-package="cz.cvut.fit.genepi.controllers" />
<import resource="classpath:applicationContext-security.xml" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

<mvc:resources mapping="/resources/**" location="/resources/" />

</beans>

解决方案的结构:

enter image description here

最佳答案

代码应该在你的应用程序上下文定义(applicationContext.xml)中,并且位置是相对于部署根目录:

 <mvc:resources location="/resources/" mapping="/resources/**"/>

你会在配置文件的顶部需要这个

 xmlns:mvc="http://www.springframework.org/schema/mvc"

然后

 xsi:schemaLocation = http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

我认为可能有些困惑,正常的 wdirectory 结构是这样的:

src/main/java 
src/main/resources
src/main/webapp
src/main/webapp/WEB-INF
src/main/webapp/WEB-INF/jsps
src/main/webapp/css

css 目录只是一个例子,我实际上有 javascipt 和图像目录,有些人更喜欢只称为“static-assets”的目录。但是称它为资源是相当困惑的。 src/main/resource/目录实际上包含整个项目的配置文件(我把我的 appContext.xml 和 log.properties 文件放在那里),它在部署时被复制到 WEB-INF,不应该用于映射静态资源。例如,在我的示例中,dactualyl 将像这样映射:

<mvc:resources location="/css/" mapping="/css/**"/>

关于java - 使用集成的 Spring 安全访问 Spring 3 MVC 应用程序中的静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15526975/

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