Spring Boot Admin排坑指南是什么,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元賈汪做網(wǎng)站,已為上家服務(wù),為賈汪各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):13518219792
常見(jiàn)的注冊(cè)失敗問(wèn)題可以分為以下兩種
Spring Boot Admin服務(wù)端與客戶(hù)端不在同一臺(tái)服務(wù)器上
提示安全校驗(yàn)不通過(guò)
第一種問(wèn)題的解決辦法:
必須在客戶(hù)端配置boot.admin.client.instance.service-url屬性,讓Spring Boot Admin服務(wù)端可以通過(guò)網(wǎng)絡(luò)獲取客戶(hù)端的數(shù)據(jù)(否則默認(rèn)會(huì)通過(guò)主機(jī)名去獲?。?/p>
boot: admin: client: url: ${your spring boot admin url} username: ${your spring boot admin username} password: ${your spring boot admin password} instance: prefer-ip: true service-url: ${your spring boot client url}
第二種問(wèn)題的解決辦法:
首先,安全檢驗(yàn)問(wèn)題,其實(shí)就是現(xiàn)在服務(wù)端配置賬號(hào)密碼,然后客戶(hù)端在注冊(cè)的時(shí)候提供賬號(hào)密碼進(jìn)行登錄來(lái)完成校驗(yàn)
這個(gè)過(guò)程的實(shí)現(xiàn),作為Spring全家桶項(xiàng)目,推薦使用Spring Security來(lái)解決,所以如果出現(xiàn)校驗(yàn)失敗,那多半是Spring Security的配置出現(xiàn)問(wèn)題
接下來(lái)介紹如何分別配置服務(wù)端與客戶(hù)端來(lái)處理這個(gè)問(wèn)題
通過(guò)maven加載Spring Security依賴(lài)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
設(shè)置服務(wù)端的用戶(hù)名和密碼(客戶(hù)端來(lái)注冊(cè)時(shí)使用此賬號(hào)密碼進(jìn)行登錄)
spring: security: user: name: liumapp password: superliumapp
編寫(xiě)Spring Security配置類(lèi)
import de.codecentric.boot.admin.server.config.AdminServerProperties; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; import org.springframework.security.web.csrf.CookieCsrfTokenRepository; /** * file SecuritySecureConfig.java * author liumapp * github https://github.com/liumapp * email liumapp.com@gmail.com * homepage http://www.liumapp.com * date 2018/11/29 */ @Configuration public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final String adminContextPath; public SecuritySecureConfig(AdminServerProperties adminServerProperties) { this.adminContextPath = adminServerProperties.getContextPath(); } @Override protected void configure(HttpSecurity http) throws Exception { // @formatter:off SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); successHandler.setDefaultTargetUrl(adminContextPath + "/"); http.authorizeRequests() .antMatchers(adminContextPath + "/assets/**").permitAll() .antMatchers(adminContextPath + "/login").permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and() .logout().logoutUrl(adminContextPath + "/logout").and() .httpBasic().and() .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringAntMatchers( adminContextPath + "/instances", adminContextPath + "/actuator/**" ); // @formatter:on } }
上面這段代碼,需要大家注意的就一個(gè)AdminServerProperties類(lèi),通過(guò)瀏覽它的部分源代碼:
@ConfigurationProperties("spring.boot.admin") public class AdminServerProperties { /** * The context-path prefixes the path where the Admin Servers statics assets and api should be * served. Relative to the Dispatcher-Servlet. */ private String contextPath = ""; /** * The metadata keys which should be sanitized when serializing to json */ private String[] metadataKeysToSanitize = new String[]{".*password$", ".*secret$", ".*key$", ".*$token$", ".*credentials.*", ".*vcap_services$"}; /** * For Spring Boot 2.x applications the endpoints should be discovered automatically using the actuator links. * For Spring Boot 1.x applications SBA probes for the specified endpoints using an OPTIONS request. * If the path differs from the id you can specify this as id:path (e.g. health:ping). */ private String[] probedEndpoints = {"health", "env", "metrics", "httptrace:trace", "httptrace", "threaddump:dump", "threaddump", "jolokia", "info", "logfile", "refresh", "flyway", "liquibase", "heapdump", "loggers", "auditevents", "mappings", "scheduledtasks", "configprops", "caches", "beans"}; //以下省略... }
可以發(fā)現(xiàn)AdminServerProperties定義了Spring Boot Admin的配置屬性,登錄自然也是其中之一,所以我們?cè)诰帉?xiě)Spring Security配置類(lèi)的時(shí)候,務(wù)必要引入AdminServerProperties
到這里,Spring Boot Admin服務(wù)端對(duì)于Spring Security的配置便結(jié)束了,接下來(lái)讓我們開(kāi)始客戶(hù)端的Security配置
首先對(duì)于客戶(hù)端,我們除了Spring Boot Admin Client依賴(lài)外,還需要額外引入 Spring Security依賴(lài):
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
在此基礎(chǔ)上通過(guò)編寫(xiě)客戶(hù)端application.yml配置文件來(lái)設(shè)置賬號(hào)密碼
spring: boot: admin: client: url: ${your sba server url} username: ${your sba username} password: ${your sba password} instance: service-base-url: ${your client url}
接下來(lái)對(duì)Client端的Spring Security做配置,允許Server端讀取actuator暴露的數(shù)據(jù)
添加一個(gè)配置類(lèi):
import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().permitAll() .and().csrf().disable(); } }
到此,因?yàn)榘踩?yàn)證而不能注冊(cè)成功的問(wèn)題便可以解決
這個(gè)問(wèn)題產(chǎn)生原因有兩種
客戶(hù)端日志沒(méi)有以文件形式存儲(chǔ)下來(lái)
客戶(hù)端容器化部署后,日志文件沒(méi)有映射到宿主機(jī)磁盤(pán)上
針對(duì)第一種情況,解決辦法比較簡(jiǎn)單,將系統(tǒng)產(chǎn)生的日志以文件形式保存即可:
logging: file: ./log/client.log pattern: file: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"
第二種情況較為復(fù)雜,首先要分清除是用什么工具來(lái)部署容器的,但一般而言直接通過(guò)文件映射即可
這里以docker為例,在docker內(nèi)通過(guò)設(shè)置volumes來(lái)映射日志文件
volumes: - ./log:/client/log/
偶爾也會(huì)遇到這種情況:Spring Boot Admin客戶(hù)端注冊(cè)服務(wù)端是成功的,但是統(tǒng)計(jì)頁(yè)面顯示的數(shù)據(jù)過(guò)少(可能只有日志這一欄)
造成這種問(wèn)題的原因在于:我們沒(méi)有開(kāi)放客戶(hù)端的actuator接口地址給服務(wù)端訪問(wèn)
那么解決辦法也很簡(jiǎn)單,允許服務(wù)端訪問(wèn)actuator即可
首先我們需要確保項(xiàng)目有actuator依賴(lài)(一般來(lái)說(shuō),spring-boot-admin-starter-client本身就包含這個(gè)依賴(lài),所以不需要額外引入):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
然后打開(kāi)actuator的端口,在client端的配置文件中增加以下內(nèi)容:
management: endpoints: web: exposure: include: "*"
同時(shí)考慮到client與server域名存在不一樣的情況,順便把跨域也解決掉,增加跨域配置類(lèi):
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @author liumapp * @file CorsConfig.java * @email liumapp.com@gmail.com * @homepage http://www.liumapp.com * @date 2018/8/11 */ @Configuration public class CorsConfig implements WebMvcConfigurer { public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowCredentials(true) .allowedHeaders("*") .allowedOrigins("*") .allowedMethods("*"); } }
關(guān)于Spring Boot Admin排坑指南是什么問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
分享題目:SpringBootAdmin排坑指南是什么
網(wǎng)址分享:http://bm7419.com/article28/jdccjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、域名注冊(cè)、品牌網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站收錄、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)