使用Spring Boot Admin管理Spring Boot应用

Spring BootSpring Boot Admin大约 2674 字

client使用了主机名注册

client actuator使用了hostname进行注册时无法绑定到Spring Boot Admin,可设置prefer-ip属性为true即可解决。

spring:
  boot:
    admin:
      client:
        url: http://localhost:12345
        instance:
          prefer-ip: true

设置邮件提醒

Spring Boot Admin Server中设置client下线通知

  • 添加spring-boot-starter-mail依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
  • 设置admin发/收件人
spring:
  boot:
    admin:
      notify:
        mail:
          from: ${spring.mail.username}
          to: receiver@163.com
  mail:
    host: smtp.163.com
    username: username@163.com
    password: # your own password

535 Error: authentication failed

  • 网易邮箱有没有开启授权码,若开启则password为授权码
  • username需是完整的登录名,如:15317832520@163.com

553 Mail from must equal authorized user

必须设置spring.boot.admin.notify.mail.from属性,默认是Spring Boot Admin <noreply@localhost>

spring:
  boot:
    admin:
      notify:
        mail:
          from: ${spring.mail.username}
  mail:
    username: 15317832520@163.com

Spring Boot Admin添加登录页面

  • 添加spring-boot-starter-security依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
  • 设置用户名密码
spring:
  security:
    user:
      name: admin
      password: admin123
  • 配置Spring Boot Admin登录页面
@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" );

        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().disable();
        // @formatter:on
    }
}

参考

https://codecentric.github.io/spring-boot-admin/current/

代码

https://github.com/fendoudebb/learning/tree/master/java/learn-spring-boot-admin

阅读 395 · 发布于 2019-08-01

————        END        ————

扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看换一批