我正在尝试使用Spring Boot Admin在Spring Boot应用程序中在运行时更改日志级别,但是我在浏览器控制台中得到了401
并且日志级别也未更改。
我所做的是制作一个springadmin
应用程序和一个名为SpringBootClient
的客户端,管理员将在运行时更改客户端的日志级别。当我登录到springadmin
应用程序并输入指定的用户名和密码时,我登录到了springadmin
应用程序,在那里我可以看到我的客户端为UP,但是当我去更改它的日志时在“记录程序”选项卡上,我看到一条消息,例如“配置警告失败”,当我检查浏览器控制台时,它向POST
发出http://localhost:8080/instances/8723817f5b45/actuator/loggers/com.muktadirkhan
请求并给出401
。
这是我的application.properties
应用程序的springadmin
spring.security.user.name=admin
spring.security.user.password=admin
management.endpoints.web.exposure.include=httptrace,loggers,health,info,metrics
spring.boot.admin.routes.endpoints=env, metrics, trace, jolokia, info, configprops
springadminApplication.java
是这样的:package com.muktadirkhan.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
@EnableAdminServer
@Configuration
@SpringBootApplication
public class SpringadminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringadminApplication.class, args);
}
}
我还在
WebSecurityConfig.java
应用程序中创建了一个springadmin
,如下所示:package com.muktadirkhan.springboot;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.web.PathUtils;
import java.util.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DurationUnit;
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final AdminServerProperties adminServer;
public WebSecurityConfig(AdminServerProperties adminServer) {
this.adminServer = adminServer;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler =
new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(this.adminServer.getContextPath() + "/");
http
.authorizeRequests()
.antMatchers(this.adminServer.getContextPath() + "/assets/**").permitAll()
.antMatchers(this.adminServer.getContextPath() + "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage(this.adminServer.getContextPath() + "/login")
.successHandler(successHandler)
.and()
.logout()
.logoutUrl(this.adminServer.getContextPath() + "/logout")
.and()
.httpBasic()
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringRequestMatchers(
new AntPathRequestMatcher(this.adminServer.getContextPath() +
"/instances", HttpMethod.POST.toString()),
new AntPathRequestMatcher(this.adminServer.getContextPath() +
"/instances/*", HttpMethod.DELETE.toString()),
new AntPathRequestMatcher(this.adminServer.getContextPath() + "/actuator/**"))
.and()
.rememberMe()
.key(UUID.randomUUID().toString())
.tokenValiditySeconds(1209600);
}
}
关于客户,我的
SpringBootClientApplication.java
是:package com.muktadirkhan.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootClientApplication.class, args);
}
}
客户端的
application.properties
:server.port=8090
spring.boot.admin.client.url=http://localhost:8080
spring.boot.admin.url=http://localhost:8080
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.security.user.name=admin
spring.security.user.password=admin
spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name}
spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password}
最佳答案
您的客户端上可能具有适用于此公开的执行器端点的csrf安全设置,因此不允许spring boot管理服务器执行任何POST请求