当前位置:   article > 正文

Spring Boot Admin 安全访问 Actuator_spring.boot.admin访问

spring.boot.admin访问

一、pom依赖

  1:admin模块pom

  1. <dependencies>
  2. <dependency>
  3. <groupId>de.codecentric</groupId>
  4. <artifactId>spring-boot-admin-starter-server</artifactId>
  5. <version>${spring-boot-admin}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-security</artifactId>
  14. </dependency>
  15. <dependency>
  16. <groupId>net.logstash.logback</groupId>
  17. <artifactId>logstash-logback-encoder</artifactId>
  18. <version>${logstash-logback-encoder}</version>
  19. </dependency>
  20. <!-- <dependency>-->
  21. <!-- <groupId>org.springframework.boot</groupId>-->
  22. <!-- <artifactId>spring-boot-starter-mail</artifactId>-->
  23. <!-- </dependency> -->
  24. </dependencies>

  2:actuator模块pom

  1. <dependencies>
  2. <!--admin client-->
  3. <dependency>
  4. <groupId>de.codecentric</groupId>
  5. <artifactId>spring-boot-admin-starter-client</artifactId>
  6. <version>${de.codecentric}</version>
  7. </dependency>
  8. <!--actuator begin-->
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-actuator</artifactId>
  12. </dependency>
  13. <!-- spring security -->
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-security</artifactId>
  17. </dependency>
  18. </dependencies>

二、配置文件

  1:admin配置

  1. #spring boot admin 账号密码
  2. spring:
  3. security:
  4. user:
  5. name: admin
  6. password: ***
  1. # 要在其中注册的Spring Boot Admin Server的安全信息。
  2. spring:
  3. boot:
  4. admin:
  5. client:
  6. url: http://ip:port
  7. instance:
  8. name: ${spring.application.name}
  9. # service-url: http://10.206.65.132:25245
  10. prefer-ip: true # true 注册时 admin 中显示IP地址不显示主机名
  11. port: 8000
  12. username: admin
  13. password: ***
  14. # 与Spring Boot 2一样,默认情况下,大多数端点都不通过http公开,我们公开了所有端点。对于生产,您应该仔细选择要公开的端点。
  15. # health显示 management.endpoint.health.show-details=always
  16. management:
  17. endpoints:
  18. web:
  19. exposure:
  20. include: '*'

三、admin安全配置

  1. @Configuration
  2. @EnableGlobalMethodSecurity(prePostEnabled = true)
  3. public class AdminServiceSecurityConfig extends WebSecurityConfigurerAdapter {
  4. private final AdminServerProperties adminServer;
  5. public AdminServiceSecurityConfig(AdminServerProperties adminServer) {
  6. this.adminServer = adminServer;
  7. }
  8. @Override
  9. protected void configure(HttpSecurity http) throws Exception {
  10. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  11. successHandler.setTargetUrlParameter("redirectTo");
  12. successHandler.setDefaultTargetUrl(this.adminServer.path("/"));
  13. // http.authorizeRequests().antMatchers("/instances**").permitAll();
  14. http.authorizeRequests(
  15. //授予公众对所有静态资产和登录页面的访问权限。
  16. (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll()
  17. .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated() //其他所有请求都必须经过验证。
  18. ).formLogin(
  19. //配置登录和注销。
  20. (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()
  21. //启用HTTP基本支持。这是Spring Boot Admin Client注册所必需的。
  22. ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults())
  23. // .csrf().disable()
  24. .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600))
  25. .csrf()
  26. //开启基于cookie的csrf保护
  27. .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  28. //忽略这些路径的csrf保护以便Spring Boot Admin Client注册
  29. .ignoringAntMatchers(
  30. this.adminServer.path("/instances"),
  31. this.adminServer.path("/actuator/**")
  32. );
  33. }
  34. }

四、admin添加自定义请求头

  1. @Component
  2. public class HttpHeadersProviderConfig implements HttpHeadersProvider {
  3. @Value("${server.port}")
  4. private String port;
  5. @Override
  6. public HttpHeaders getHeaders(Instance instance) {
  7. HttpHeaders httpHeaders = new HttpHeaders();
  8. //设置请求头参数
  9. httpHeaders.add("spring-boot-admin-service", port);
  10. return httpHeaders;
  11. }
  12. }

五、actuator配置安全校验

  1. @WebFilter
  2. @ServletComponentScan
  3. @Component
  4. public class ActuatorFilter implements Filter {
  5. @Value("${spring.boot.admin.client.port}")
  6. private String adminServicePort;
  7. @Override
  8. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  9. String headerKey = "spring-boot-admin-service";
  10. String uriKey = "/actuator";
  11. HttpServletRequest request = (HttpServletRequest) servletRequest;
  12. if (request.getRequestURI().contains(uriKey) && !adminServicePort.equals(request.getHeader(headerKey))) {
  13. throw new RuntimeException("无权限访问Actuator端口");
  14. }
  15. filterChain.doFilter(servletRequest, servletResponse);
  16. }
  17. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/102014
推荐阅读
相关标签
  

闽ICP备14008679号