分享

解决Spring Boot Admin使用HTTPS协议访问页面无法正常显示的问题-CSDN博客

 vnxy001 2024-03-25 发布于浙江

应用场景:

采用Spring Boot Admin监控微服务运行情况,部署两个Admin服务,然后通过Nginx进行负载均衡,从而实现Admin服务高可用。在Nginx中配置SSL证书,通过HTTPS协议进行访问。

问题描述:

在浏览器中访问https://admin./#/applications,页面无法显示,查看页面请求信息后发现浏览器在加载当前页面的资源文件时使用的是http://admin.的地址,因为请求协议http和地址栏中的请求协议https不一样,所以浏览器认为是跨域访问,阻止了请求,导致页面无法显示。
在这里插入图片描述

原因分析:

根据浏览器中的错误提示,可以断定是Admin的页面中在某个地方设置了请求地址,从而导致异步请求的地址和地址栏中的地址不一致。查看页面源码,可以发现

<base href="http://admin./">

这个地址中使用的是http协议,很有可能是异步请求的时候就使用了这里的值。接下来我们看看能否从官方源码中找到答案,下面是Admin前端代码中index.html的部分代码。

<head>
    <base th:href="${baseUrl}" href="/">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="format-detection" content="telephone=no,email=no">
    <meta name="theme-color" content="#42d3a5">

    <!-- include extensions -->
    <th:block th:each="cssExtension : ${cssExtensions}">
        <link rel="preload" th:href="'extensions/' + ${cssExtension.resourcePath}" as="style">
    </th:block>
    <th:block th:each="jsExtension : ${jsExtensions}">
        <link rel="preload" th:href="'extensions/' + ${jsExtension.resourcePath}" as="script">
    </th:block>
    <th:block th:each="cssExtension : ${cssExtensions}">
        <link th:href="'extensions/' + ${cssExtension.resourcePath}" rel="stylesheet">
    </th:block>

    <link rel="shortcut icon" th:href="${uiSettings.favicon}" type="image/png">
    <title th:text="${uiSettings.title}">Spring Boot Admin</title>
</head>

从上面的代码中可以发现${baseUrl} 是一个变量,在页面访问的时候转换成了访问的网址,接下来我们再看这个值是如何设置的,通过开发工具在项目中搜索baseUrl,我们找到了一个baseUrl接口,方法路径de.codecentric.boot.admin.server.ui.web.UiController#getBaseUrl

@ModelAttribute(value = "baseUrl", binding = false)
	public String getBaseUrl(UriComponentsBuilder uriBuilder) {
		UriComponents publicComponents = UriComponentsBuilder.fromUriString(this.publicUrl).build();
		if (publicComponents.getScheme() != null) {
			uriBuilder.scheme(publicComponents.getScheme());
		}
		if (publicComponents.getHost() != null) {
			uriBuilder.host(publicComponents.getHost());
		}
		if (publicComponents.getPort() != -1) {
			uriBuilder.port(publicComponents.getPort());
		}
		if (publicComponents.getPath() != null) {
			uriBuilder.path(publicComponents.getPath());
		}
		return uriBuilder.path("/").toUriString();
	}

在方法中打上断点,发现每次打开Admin页面的时候,都会请求这个方法,而且这个方法返回的正是页面中地址:http://admin./。既然这个地址是从后台接口中获取的,那么是否可以通过某个配置项来设置呢,在方法中我们可以看到publicUrl这个参数会影响生成的地址,然后尝试在application.yml配置文件中输入这个配置项,根据提示发现public-url这个配置项,并对其进行赋值

spring:
  boot:
    admin:
      ui:
        public-url: https://admin./

配置完成后重启服务,再次访问Admin页面,页面可以正常显示,再看页面源码中的地址已经变成了

<base href="https://admin./">

查找资料发现public-url这个值就是用于构建页面中href的值,它的作用是在反向代理的情况下设置请求地址。如果不进行配置,那么主机和端口都可以从浏览器地址栏中的访问地址中获取,但是请求协议使用的是默认的http,这时如果和地址栏中访问地址的协议不一致,就会导致页面中的异步请求跨域,被浏览器拦截。

解决方案:

在Admin服务端通过spring.boot.admin.ui.public-url配置请求地址,避免跨域访问。

spring:
  boot:
    admin:
      ui:
        public-url: https://admin./

另外还有几个关于Admin服务端页面的配置
spring.boot.admin.ui.brand 用于设置页面中的LOGO
spring.boot.admin.ui.title 用于设置浏览器页面标签
spring.boot.admin.ui.favicon 用于设置浏览器页面标签中显示的图标

软件版本:

JDK:1.8
Spring Boot:2.1.9.RELEASE
Spring Boot Admin:2.1.6
Nginx:1.20.1

参考资料:

[1]: https://blog.csdn.net/u014217825/article/details/103061261
[2]: https:///mirrors/spring-boot-admin

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多