pom 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
组件的主要依赖是 spring-boot-admin-starter-server, admin主要是提供对各个连接到eureka的节点进行健康检查(cpu,内存,线程,日志等),组件本身提供了一个web ui, 引入security模块也是为了简单对登录用户做个限制。spring-boot-starter-actuator这个包可以提供健康数据,对于所有的client都需要加入配置,详细可以看github上面的项目。
启动函数
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class ExampleAdminApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleAdminApplication.class, args);
}
@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");
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();
}
}
}
@EnableAdminServer 开启spring-admin, 同时下面有一堆的security配置,都是官方的配置,详细可以参考spring.io中的Security那节,大致意思就是对一些访问进行重定向,针对某些接口进行身份验证,对一些资源接口和登录接口则放行。
配置文件
server:
port: 8781
eureka:
instance:
hostname: localhost
metadata-map:
user.name: admin
user.password: admin
health-check-url-path: /actuator/health
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8761/eureka
spring:
security:
user:
name: admin
password: admin
profiles:
active: development
management:
endpoints:
web:
exposure:
exclude:
include: "*"
endpoint:
health:
show-details: always
- spring.security.user 这个里面的内容就是配置了登录的用户名和密码,不需要多解释。
- eureka.instance.metadata-map 这个配置汉含义是这样的,因为admin也会检查自身的安全信息,但是由于我们做了安全验证,所以需要配置security的账号密码,这样访问的时候才能正确访问。
- management.endpoints.web.exposure.exclude.include 这个配置指的是要显示的安全检查内容(菜单栏的内容),这里配置全部显示了。
部分效果图