Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 小程序 Arthas JVM AQS juc Kubernetes Docker 诊断工具


使用 Spring Boot Admin 管理 Spring Boot 应用

Spring Boot Spring Boot Admin 大约 2676 字

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

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

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

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

扫描二维码关注我
昵称:
随便看看 换一批