How to config a war app inside java to allow access to resources directory from GET method?

124 Views Asked by At

I am trying wildfly and I do not know how to allow to access to some resources.

My index.jsp need to get some js files. But when loading the corresponding web page. I observe this error:

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]

index.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>

web.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>

structure

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

Regards

2

There are 2 best solutions below

0
On

resource is not inside of web-inf so your problem must because your path href of link

change code to this

<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/jquery-ui/external/jquery/jquery-2.1.4.min.js" />
0
On

From what I understand here: http://docs.oracle.com/cd/E19798-01/821-1841/gjjcd/index.html specifying a pattern in web-resource-collection denies access, while not specifying it allows. Therefore:

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

this configuration is actively denying GET request to resources.