我确实从 Spring Boot 2.6.15 升级到 2.7.0
有关我的设置的更多信息:
html thymeleaf 页面名为 myPage.html
位于目录 myproject/src/main/resources/templates/html/myPage.html
<!DOCTYPE HTML>
<html>
<head>
<title>MyPage</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet">
<link rel="icon" type="image/png" th:href="@{/img/favicon.png}" />
</head>
<body>
<div id="app"></div>
<script type="text/javascript" th:src="@{/js/app/myApp.js}" ></script>
</body>
</html>
myApp.js
位于目录 myproject/src/main/resources/static/js/app/myApp.js
弹簧控制器
package com.myapp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ViewController {
@GetMapping("/myPage")
public String myPage() {
return "html/myPage";
}
}
在更新到 Spring Boot 2.7.0 之前,我能够在我的 Android 应用程序中加载包含 javascript 的 html 页面,并且使用此代码加载一切都工作正常
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://localhost:8080/myPage");
更新到springboot 2.7.0后
我收到错误:
I/chromium: [INFO:CONSOLE(0)] “拒绝从‘http://localhost:8080/js/app/myApp.js’执行脚本,因为它的 MIME 类型 ('') 不可执行,并且严格MIME 类型检查已启用。”,来源:http://localhost/myPage (0)
我试图在发行说明中查找 MIME、Content-Type 等的更改,但我什么也没找到 https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release -注释 我还检查了 2.5.0 的弃用列表https://docs.spring.io/spring-framework/docs/2.5.x/javadoc-api/deprecated-list.html 但又什么也没有。也许你们中的一些人知道会发生什么以及如何解决这个问题?
编辑:
我已经尝试了SpringBoot 2.7.13版本,但错误仍然存在。
2.6.15 和 2.7.13 的 Curl 但结果相同。
2.6.15:
HTTP/1.1 200
Vary: origin,access-control-request-method,access-control-request-headers,accept-encoding
Last-Modified: Sat, 08 Jul 2023 08:49:59 GMT
Cache-Control: no-store
Accept-Ranges: bytes
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
Content-Type: application/javascript
Content-Length: 2453575
Date: Sat, 08 Jul 2023 12:02:28 GMT
2.7.13:
HTTP/1.1 200
Vary: origin,access-control-request-method,access-control-request-headers,accept-encoding
Last-Modified: Sat, 08 Jul 2023 08:49:59 GMT
Cache-Control: no-store
Accept-Ranges: bytes
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
Content-Type: application/javascript
Content-Length: 2453575
Date: Sat, 08 Jul 2023 11:53:14 GMT
Spring 服务内容类型肯定有一些变化,但如何跟踪它?
如何使用 JWT 令牌执行安全的 WebView 请求。我又回来回答这个问题了
问题出在 Android WebView 配置中。 就我而言,解决方案是在扩展 WebViewClient 的类 WebViewClientConfig 中重写 shouldInterceptRequest 并向其传递 JWT 令牌。 shouldInterceptRequest 将使用 jwt 令牌单独调用每个资源。这里的资源是指:html、css、js
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
...
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
try {
OkHttpClient client = new OkHttpClient.Builder().build();
Request.Builder builder = new Request.Builder().url(url);
// Add your Authorization header
builder.header("Authorization", "Bearer " + this.accessToken);
// Add other headers if needed
Map<String, String> originalHeaders = request.getRequestHeaders();
for (Map.Entry<String, String> header : originalHeaders.entrySet()) {
if (!header.getKey().equalsIgnoreCase("Authorization")) {
builder.header(header.getKey(), header.getValue());
}
}
// Build the request and execute
Request okHttpRequest = builder.build();
Response response = client.newCall(okHttpRequest).execute();
if (response.isSuccessful()) {
String mimeType = response.header("Content-Type", "text/html");
MediaType mediaType = MediaType.parse(mimeType);
String charset = "UTF-8";
if (mediaType != null && mediaType.charset() != null) {
charset = mediaType.charset().name();
}
InputStream responseBodyStream = response.body().byteStream();
Map<String, String> responseHeaders = new HashMap<>();
for (String name : response.headers().names()) {
responseHeaders.put(name, response.header(name));
}
WebResourceResponse resourceResponse = new WebResourceResponse(
mediaType.type() + "/" + mediaType.subtype(),
charset,
responseBodyStream
);
resourceResponse.setResponseHeaders(responseHeaders);
return resourceResponse;
} else {
return super.shouldInterceptRequest(view, request);
}
} catch (Exception e) {
Log.e("WebViewInterceptor", "Network error while intercepting request: " + request.getUrl(), e);
return super.shouldInterceptRequest(view, request);
}
}
然后在 MyActivity 中,我使用 WebViewClientConfig 类,该类扩展了 WebViewClient,我设置了 WebViewClient 并向其传递 JWT 令牌
webView.setWebViewClient(new WebViewClientConfig(token));
现在这里请求我的html页面。 shouldInterceptRequest 将分别获取 html 中提到的每个 src 的 token:css、js 等。
webView.loadUrl(url + "/endpointName");