几天来,我一直在尝试使用基本的安全配置来设置客户端的Spring Boot管理服务器。不幸的是,我无法让客户端向服务器进行身份验证。我尝试过各种示例,现在我想知道这是否只是Spring Boot Admin当前版本的一个bug。
服务器application.properties
spring.security.user.name=admin
spring.security.user.password=admin
服务器build.gradle
plugins {
id 'groovy'
}
group 'edu.wsu.it.esg-apps'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:latest.release'
compile 'de.codecentric:spring-boot-admin-starter-server:2.1.6'
compile 'org.springframework.boot:spring-boot-starter-web:2.1.7.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-security:2.1.7.RELEASE'
compile 'de.codecentric:spring-boot-admin-server-ui-login:2.1.6'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
SecuritySecureConfig.groovy
package admin.security
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
@Configuration
class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath
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
}
}
SpringBootAdminApplication.groovy
package admin
import de.codecentric.boot.admin.server.config.EnableAdminServer
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.Configuration
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
class SpringBootAdminApplication {
static void main(String[] args){
SpringApplication.run(SpringBootAdminApplication.class,args)
}
}
客户端application.properties
server.port=8081
logging.level.root=DEBUG
spring.boot.admin.client.url=http://localhost:8080
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
spring.boot.admin.client.api-path=instances
management.endpoints.web.exposure.include=*
management.endpoints.jmx.exposure.include=*
management.endpoint.health.show-details=always
spring.security.user.name=client
spring.security.user.password=client
spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name}
spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password}
客户端build.gradle
plugins {
id 'groovy'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
}
group 'edu.wsu.it.esgapps'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compile "org.codehaus.groovy:groovy-all:latest.release"
compile "de.codecentric:spring-boot-admin-starter-client:2.1.6"
compile "org.springframework.boot:spring-boot-starter-security:2.1.7.RELEASE"
compile "org.springframework.boot:spring-boot-starter-web:2.1.7.RELEASE"
compile 'org.springframework.boot:spring-boot-starter-actuator:2.1.7.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf:2.1.7.RELEASE'
// compile 'org.springframework.cloud:spring-cloud-config-client:latest.release'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
客户端application.groovy
package edu.wsu.it.esgapps
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class Application {
static void main(String[] args){
SpringApplication.run(Application.class,args)
}
}
在这一点上,我不知所措。90%的配置和类是从报告的工作示例中复制粘贴的。启动客户端后,我在日志中收到以下消息。
2019-11-24 21:06:36.021 DEBUG 20980 --- [gistrationTask1] o.s.web.client.RestTemplate : HTTP POST http://localhost:8080/instances
2019-11-24 21:06:36.021 DEBUG 20980 --- [gistrationTask1] o.s.web.client.RestTemplate : Accept=[application/json, application/*+json]
2019-11-24 21:06:36.021 DEBUG 20980 --- [gistrationTask1] o.s.web.client.RestTemplate : Writing [Application(name=spring-boot-application, managementUrl=http://ITS-ESG-T2-3025.ad.wsu.edu:8081/actuator, healthUrl=http://ITS-ESG-T2-3025.ad.wsu.edu:8081/actuator/health, serviceUrl=http://ITS-ESG-T2-3025.ad.wsu.edu:8081/)] as "application/json"
2019-11-24 21:06:36.024 DEBUG 20980 --- [gistrationTask1] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@1715607e8 pairs: {POST /instances HTTP/1.1: null}{Accept: application/json}{Content-Type: application/json}{Authorization: Basic YWRtaW46YWRtaW4=}{User-Agent: Java/11.0.1}{Host: localhost:8080}{Connection: keep-alive}{Content-Length: 329}
2019-11-24 21:06:36.030 DEBUG 20980 --- [gistrationTask1] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@17d95de411 pairs: {null: HTTP/1.1 401}{Set-Cookie: JSESSIONID=3D29D6099D55422892981E516391F158; Path=/; HttpOnly}{X-Content-Type-Options: nosniff}{X-XSS-Protection: 1; mode=block}{Cache-Control: no-cache, no-store, max-age=0, must-revalidate}{Pragma: no-cache}{Expires: 0}{X-Frame-Options: DENY}{WWW-Authenticate: Basic realm="Realm"}{Content-Length: 0}{Date: Mon, 25 Nov 2019 05:06:36 GMT}
2019-11-24 21:06:36.030 DEBUG 20980 --- [gistrationTask1] o.s.web.client.RestTemplate : Response 401 UNAUTHORIZED
2019-11-24 21:06:36.033 DEBUG 20980 --- [gistrationTask1] d.c.b.a.c.r.ApplicationRegistrator : Failed to register application as Application(name=spring-boot-application, managementUrl=http://ITS-ESG-T2-3025.ad.wsu.edu:8081/actuator, healthUrl=http://ITS-ESG-T2-3025.ad.wsu.edu:8081/actuator/health, serviceUrl=http://ITS-ESG-T2-3025.ad.wsu.edu:8081/) at spring-boot-admin ([http://localhost:8080/instances]): 401 null
如果有人能帮我提供一些见解,我将不胜感激。
发布于 2020-04-08 23:15:50
这是由gradle无法成功解决的依赖冲突造成的。事实上,我使用的是spring boot 2.1.7,而boot admin是在2.1.5上构建的,有一些库要么在2.1.7中被弃用,要么移到了不同的包中。全面回滚到2.1.5解决了这个问题。
https://stackoverflow.com/questions/59025591
复制相似问题