监控模块安全认证 - 图1

为什么要做二次认证

spring boot admin 默认没有开启认证,也是就是别人知道了监控模块的IP:PORT 即可访问。监控功能在生产上又是必要的功能,所以需要有二次认证

实现原理

  • 引入spring security
  1. <!--security-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-security</artifactId>
  5. </dependency>
  • 配置spring security即可
  1. @Configuration
  2. public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
  3. private final String adminContextPath;
  4. public WebSecurityConfigurer(AdminServerProperties adminServerProperties) {
  5. this.adminContextPath = adminServerProperties.getContextPath();
  6. }
  7. @Override
  8. protected void configure(HttpSecurity http) throws Exception {
  9. // @formatter:off
  10. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  11. successHandler.setTargetUrlParameter("redirectTo");
  12. successHandler.setDefaultTargetUrl(adminContextPath + "/");
  13. http
  14. .headers().frameOptions().disable()
  15. .and().authorizeRequests()
  16. .antMatchers(adminContextPath + "/assets/**"
  17. , adminContextPath + "/login"
  18. , adminContextPath + "/actuator/**"
  19. ).permitAll()
  20. .anyRequest().authenticated()
  21. .and()
  22. .formLogin().loginPage(adminContextPath + "/login")
  23. .successHandler(successHandler).and()
  24. .logout().logoutUrl(adminContextPath + "/logout")
  25. .and()
  26. .httpBasic().and()
  27. .csrf()
  28. .disable();
  29. // @formatter:on
  30. }
  31. }
  • 在对应的 pig-monitor-dev.yml 配置用户

pig 默认的登录用户 pig/pig,可以参考配置文件加解密章节

  1. spring:
  2. security:
  3. user:
  4. name: ENC(8Hk2ILNJM8UTOuW/Xi75qg==) # pig
  5. password: ENC(o6cuPFfUevmTbkmBnE67Ow====) # pig

❤ 问题咨询

手势点击蓝字求关注简约风动态引导关注__2022-09-07+23_18_38.gif