java - 如何在 java 中配置 war 应用程序以允许从 GET 方法访问资源目录?

标签 java jsp web.xml wildfly

我正在尝试使用 wildfly,但我不知道如何允许访问某些资源。

我的 index.jsp 需要获取一些 js 文件。但是在加载相应网页的时候。我观察到这个错误:

GET http://localhost:8080/resources/jquery-ui/external/jquery/jquery-2.1.4.min.js [HTTP/1.1 405 Method Not Allowed 4ms]
GET http://localhost:8080/resources/jquery-ui/jquery-ui.min.js [HTTP/1.1 405 Method Not Allowed 1ms]

索引.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>GROOLS</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script async="defer" src="<%=request.getContextPath()%>/resources/jquery-ui/external/jquery/jquery-2.1.4.min.js"> </script>
        <script async="defer" src="<%=request.getContextPath()%>/resources/jquery-ui/jquery-ui.min.js"> </script>

    </head>
    <body>

    </body>
</html>

网络.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>allowed</web-resource-name>
            <url-pattern>/resources/*</url-pattern>
            <http-method>GET</http-method>
        </web-resource-collection>
    </security-constraint>
</web-app>

jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
      http://www.jboss.com/xml/ns/javaee
      http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
    <context-root>/</context-root>
</jboss-web>

结构

$ tree -d src/
src/
└── main
    ├── java
    │   └── fr
    │       └── foo
    │           └── bar
    │               └── wildfly
    │                   └── service
    ├── resources
    │   └── META-INF
    └── webapp
        ├── resources
        │   └── jquery-ui
        │       ├── external
        │       │   └── jquery
        │       └── images
        └── WEB-INF

问候

最佳答案

据我了解:http://docs.oracle.com/cd/E19798-01/821-1841/gjjcd/index.html在 web-resource-collection 中指定一个模式拒绝访问,而不指定它允许。因此:

    <web-resource-collection>
        <web-resource-name>allowed</web-resource-name>
        <url-pattern>/resources/*</url-pattern>
        <http-method>GET</http-method>
    </web-resource-collection>

此配置主动拒绝对资源的 GET 请求。

关于java - 如何在 java 中配置 war 应用程序以允许从 GET 方法访问资源目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30919273/

相关文章:

java - 无法在 netbeans 7.0.1 中找到 web.xml

java - MessageBrokerServlet 与 XmlWebAppInitializer 方法

面向 Clojure 用户的 Java

java - 未找到类 : javax. servlet.ServletException : java. lang.ClassNotFoundException : com. mysql.jdbc.Driver

java - 使用 JRI 从 Java 中用 R 编写 csv 文件

java - 如何在java appengine中将代码与html分离?

JSP 错误 - 多次出现具有不同值的 'contentType' 是非法的

java - session 超时重定向到登录java maven项目

java构造函数行为继承和静态/动态绑定(bind)

java - Peek() 真正地看到元素流过管道中的某个点