springboot 2.0 spring boot admin 配置2,登录配置,连接eureka,监控spring cloud client

springboot 2.0 spring boot admin 配置1 简单配置
https://blog.csdn.net/haveqing/article/details/86545201

一、spring boot admin server 端

1.pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.8.RELEASE
         
    
    com.urthink.upfs
    spring-boot-admin-server
    0.0.1-SNAPSHOT
    spring-boot-admin-server
    spring-boot-admin-server project for Spring Boot

    
        1.8
        2.0.4
        Finchley.SR2
    

    

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            de.codecentric
            spring-boot-admin-starter-server
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-security
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                de.codecentric
                spring-boot-admin-dependencies
                ${spring-boot-admin.version}
                pom
                import
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.application.yml

server:
  port: 8080

spring:
  application:
    name: sba-server
  profiles:
    active: secure
  security:
    user:
      name: username
      password: password

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10 #服务刷新时间配置,每隔这个时间会主动心跳一次
    metadata-map:
      user.name: ${spring.security.user.name}         #These two are needed so that the server
      user.password: ${spring.security.user.password} #can access the protected client endpoints
    health-check-url-path: /actuator/health
  client:
    registryFetchIntervalSeconds: 5 #eureka client刷新本地缓存时间
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

# 开启监控接口
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

3.SpringBootAdminServerApplication.java

package com.urthink.upfs.springbootadminserver;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
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;

/**
 * spring boot admin server端
 * @author zhao
 * @date 2019.1.18
 */
@SpringBootApplication
@EnableAdminServer
public class SpringBootAdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminServerApplication.class, args);
    }

    //不用登录时用这段配置
    @Profile("insecure")
    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().permitAll()
                    .and().csrf().disable();
        }
    }

    //用登录时用这段配置
    @Profile("secure")
    @Configuration
    public static 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");
            successHandler.setDefaultTargetUrl(adminContextPath + "/");

            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()
                    .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                    .ignoringAntMatchers(
                            adminContextPath + "/instances",
                            adminContextPath + "/actuator/**"
                    );
            // @formatter:on
        }
    }

}

 二、spring boot admin client 端

1.pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.8.RELEASE
         
    
    com.urthink.upfs
    upfs-provider
    0.0.1-SNAPSHOT
    upfs-provider
    Upfs provider project for Spring Boot

    
        1.8
        Finchley.SR2
        2.0.4
    

    

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.application.yml

server:
  port: 8771

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10 #服务刷新时间配置,每隔这个时间会主动心跳一次
    metadata-map:
      user.name: username         #These two are needed so that the server
      user.password: password     #can access the protected client endpoints
    health-check-url-path: /actuator/health
  client:
    registryFetchIntervalSeconds: 5 #eureka client刷新本地缓存时间
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: upfs-provider

# 开启监控接口
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

3.SpringBootAdminClientApplication.java

package com.urthink.upfs.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication//(exclude= {DataSourceAutoConfiguration.class})
public class UpfsProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(UpfsProviderApplication.class, args);
        System.out.println("upfs-provider startup completed.");
    }

}

三、访问http://localhost:8080

springboot 2.0 spring boot admin 配置2,登录配置,连接eureka,监控spring cloud client_第1张图片

 

springboot 2.0 spring boot admin 配置2,登录配置,连接eureka,监控spring cloud client_第2张图片

spring boot admin 还可以配置邮件提醒,

不连接eureka也可监控spring cloud client端,按照简单配置就行,

参考:

官方文档
http://codecentric.github.io/spring-boot-admin/current/

Spring Boot Admin 详解(Spring Boot 2.0,基于 Eureka 的实现)
https://blog.csdn.net/hubo_88/article/details/80671192
SpringBoot2.x搭建SpringBootAdmin2.x
https://www.cnblogs.com/anxminise/p/9795787.html
SpringCloud2.0入门4-springboot-admin监控
https://www.cnblogs.com/woshimrf/p/springcloud2-admin.html

你可能感兴趣的:(springboot)