Chrome 和 Opera 浏览器未加载 CSS 和 JavaScript 文件(404 - 未找到)
Chrome and Opera browsers not loading CSS and JavaScript files (404 - Not Found)
我遇到了一个奇怪的问题。
我使用的技术:
- Java 11
- 野蝇 20
- 日食 2020-03 (4.15.0)
问题是,当我打开我在 Chrome 或 Opera 浏览器上开发的应用程序的页面时,css 和 Java 脚本文件未应用,浏览器不会加载它们,就好像它们被禁止一样。如果我在任何其他浏览器(Firefox、Microsoft Edge 等)中打开,页面会正常加载,并应用 css 和 JavaScript。
在 Chrome 或 Opera 浏览器中(无 css):
在 Firefox 或 Microsoft Edge 浏览器中(使用 css):
检查Chrome:
这些是一些文件的相关部分:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<security-constraint>
<web-resource-collection>
<web-resource-name>restricted</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>USUARIO</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>allowed</web-resource-name>
<url-pattern>/templates/*</url-pattern>
<url-pattern>/resources/*</url-pattern>
</web-resource-collection>
</security-constraint>
<security-role>
<role-name>USUARIO</role-name>
</security-role>
<security-role>
<role-name>NAO_AUTORIZADO</role-name>
</security-role>
<welcome-file-list>
<welcome-file>inicio.xhtml</welcome-file>
</welcome-file-list>
</web-app>
jboss-app.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-app>
<security-domain>jaspitest</security-domain>
</jboss-app>
AutenticadorUsuario.java
@CustomFormAuthenticationMechanismDefinition(
loginToContinue = @LoginToContinue(
loginPage = "/login.xhtml",
errorPage = ""
)
)
@FacesConfig
@ApplicationScoped
public class AutenticadorUsuario implements IdentityStore
浏览器页面源代码:
<!DOCTYPE html>
<html lang="pt-br" xmlns="http://www.w3.org/1999/xhtml"><head id="j_idt2">
<title>Autenticação</title>
<meta charset="UTF-8" />
<link href="resources/css/autenticacao.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="resources/script/jquery.js"></script>
<script type="text/javascript" src="resources/script/autenticacao.js"></script>
<link href="resources/css/login.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="resources/script/login.js"></script></head><body>
<header>
</header>
<main>
<form id="form" name="form" method="post" action="/autenticacao/login.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="form" value="form" />
<div id="titulo">
Login
</div>
<div id="conteudo">
<div id="campos"><label for="form:nomeUsuario">Nome do usuário:</label><input id="form:nomeUsuario" type="text" name="form:nomeUsuario" /><label for="form:senha">Senha:</label><input id="form:senha" type="password" name="form:senha" value="" /><input type="submit" name="form:j_idt18" value="Entrar" />
</div>
</div><input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-1019286018880157796:-2226995668037289798" autocomplete="off" />
</form>
</main>
<footer>
<p> </p>
</footer></body>
</html>
项目结构
我已经进行了干净的构建、干净的部署、清除浏览器缓存、禁用防病毒和防火墙。问题依旧。
如果我在我的应用程序中禁用安全性(在 web.xml 中),问题就解决了(我已经检查过了)。所以这似乎与安全有关,
但我需要在此应用程序中启用安全性。
如果我在浏览器上查看源代码并单击 css 或 Java 脚本文件,我会得到
404 - 未找到
自从发了这个问题,我就升级到Wildfly 20 看看是不是Wildfly 19 的BUG 被修正了,但是没有。
此应用程序与我使用以前版本的 JBoss 或 Wildfly 开发的其他应用程序没有什么不同。新事物是我使用自定义 FORM 身份验证而不是 FORM 身份验证。我也在使用新的 Java EE Security。
这是怎么回事?
更新 1:
嘿,看看Chrome和Opera在做什么。正确的资源url是这样的:
http://localhost:8080/autenticacao/resources/css/autenticacao.css
但是当启用安全时,Chrome 和 Opera 将 url 更改为:
http://localhost:8080/resources/css/autenticacao.css
确保资源不在最后url。
知道他们为什么这样做吗?
更新 2:
我找到问题了。看看下面的答案。
问题已解决。
我将浏览器中的应用程序 URL 更改为:
http://localhost:8080/autenticacao
对此:
http://localhost:8080/autenticacao/
成功了,找到了资源。
我遇到了一个奇怪的问题。
我使用的技术:
- Java 11
- 野蝇 20
- 日食 2020-03 (4.15.0)
问题是,当我打开我在 Chrome 或 Opera 浏览器上开发的应用程序的页面时,css 和 Java 脚本文件未应用,浏览器不会加载它们,就好像它们被禁止一样。如果我在任何其他浏览器(Firefox、Microsoft Edge 等)中打开,页面会正常加载,并应用 css 和 JavaScript。
在 Chrome 或 Opera 浏览器中(无 css):
在 Firefox 或 Microsoft Edge 浏览器中(使用 css):
检查Chrome:
这些是一些文件的相关部分:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<security-constraint>
<web-resource-collection>
<web-resource-name>restricted</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>USUARIO</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>allowed</web-resource-name>
<url-pattern>/templates/*</url-pattern>
<url-pattern>/resources/*</url-pattern>
</web-resource-collection>
</security-constraint>
<security-role>
<role-name>USUARIO</role-name>
</security-role>
<security-role>
<role-name>NAO_AUTORIZADO</role-name>
</security-role>
<welcome-file-list>
<welcome-file>inicio.xhtml</welcome-file>
</welcome-file-list>
</web-app>
jboss-app.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-app>
<security-domain>jaspitest</security-domain>
</jboss-app>
AutenticadorUsuario.java
@CustomFormAuthenticationMechanismDefinition(
loginToContinue = @LoginToContinue(
loginPage = "/login.xhtml",
errorPage = ""
)
)
@FacesConfig
@ApplicationScoped
public class AutenticadorUsuario implements IdentityStore
浏览器页面源代码:
<!DOCTYPE html>
<html lang="pt-br" xmlns="http://www.w3.org/1999/xhtml"><head id="j_idt2">
<title>Autenticação</title>
<meta charset="UTF-8" />
<link href="resources/css/autenticacao.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="resources/script/jquery.js"></script>
<script type="text/javascript" src="resources/script/autenticacao.js"></script>
<link href="resources/css/login.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="resources/script/login.js"></script></head><body>
<header>
</header>
<main>
<form id="form" name="form" method="post" action="/autenticacao/login.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="form" value="form" />
<div id="titulo">
Login
</div>
<div id="conteudo">
<div id="campos"><label for="form:nomeUsuario">Nome do usuário:</label><input id="form:nomeUsuario" type="text" name="form:nomeUsuario" /><label for="form:senha">Senha:</label><input id="form:senha" type="password" name="form:senha" value="" /><input type="submit" name="form:j_idt18" value="Entrar" />
</div>
</div><input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-1019286018880157796:-2226995668037289798" autocomplete="off" />
</form>
</main>
<footer>
<p> </p>
</footer></body>
</html>
项目结构
我已经进行了干净的构建、干净的部署、清除浏览器缓存、禁用防病毒和防火墙。问题依旧。
如果我在我的应用程序中禁用安全性(在 web.xml 中),问题就解决了(我已经检查过了)。所以这似乎与安全有关,
但我需要在此应用程序中启用安全性。
如果我在浏览器上查看源代码并单击 css 或 Java 脚本文件,我会得到
404 - 未找到
自从发了这个问题,我就升级到Wildfly 20 看看是不是Wildfly 19 的BUG 被修正了,但是没有。
此应用程序与我使用以前版本的 JBoss 或 Wildfly 开发的其他应用程序没有什么不同。新事物是我使用自定义 FORM 身份验证而不是 FORM 身份验证。我也在使用新的 Java EE Security。
这是怎么回事?
更新 1:
嘿,看看Chrome和Opera在做什么。正确的资源url是这样的:
http://localhost:8080/autenticacao/resources/css/autenticacao.css
但是当启用安全时,Chrome 和 Opera 将 url 更改为:
http://localhost:8080/resources/css/autenticacao.css
确保资源不在最后url。
知道他们为什么这样做吗?
更新 2:
我找到问题了。看看下面的答案。
问题已解决。
我将浏览器中的应用程序 URL 更改为:
http://localhost:8080/autenticacao
对此:
http://localhost:8080/autenticacao/
成功了,找到了资源。