Spring Boot Admin基于security认证注册中心版
非注册中心版:https://blog.csdn.net/niugang0920/article/details/82528434
缺点就是每一个client都需要写admin-server的地。如果应用都注册在Eureka中就不需要再对每个应用进行配置,Spring Boot Admin会自动从注册中心抓取应用的相关信息。
示例:
新建maven项目如下结构:
parent pom.xml配置
主要配置spring boot 和spring cloud 版本,具体配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.niugang</groupId>
<artifactId>928.spring-boot-admin-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR4</spring-cloud.version>
<spring-boot.version>1.5.10.RELEASE</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<!--引入springboot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--引入springcloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>928.spring-boot-admin-eureka-server</module>
<module>928.spring-boot-admin-server-ui</module>
<module>928.spring-boot-admin-client</module>
</modules>
</project>
928.spring-boot-admin-eureka-server
注册中心
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.niugang</groupId>
<artifactId>928.spring-boot-admin-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>928.spring-boot-admin-eureka-server</artifactId>
<description>注册中心</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- 这样变成可执行的jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
package org.niugang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
*
* Copyright: Copyright (c) 2018 863263957@qq.com
*
* @ClassName: SpringCloudEurekaApplication.java
* @Description:注册中心
* @author: niugang
* @date: 2018年9月9日 下午5:11:18
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------*
* 2018年9月9日 niugang v1.0.0 修改原因
*/
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaApplication.class, args);
}
}
application.properties
spring.application.name=eureka-server
server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
928.spring-boot-admin-server-ui
spring-boot-admin-server相关配置
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.niugang</groupId>
<artifactId>928.spring-boot-admin-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>928.spring-boot-admin-server-ui</artifactId>
<properties>
<spring-boot-admin.version>1.5.7</spring-boot-admin.version>
</properties>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<!-- tag::dependency-eureka[] -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--要在管理UI中与JMX-beans交互,您必须在应用程序中包含Jolokia。 如果您使用的是spring-boot-admin-starter-client,它将会为您提供,如果不将Jolokia添加到您的依赖项中: -->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<!--安全 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--spring admin login -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-login</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--要让版本显示在应用程序列表中,您可以使用spring-boot-maven-plugin中的build-info目标 -->
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
NotifierConfig.java
package org.niugang.config;
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.Scheduled;
import de.codecentric.boot.admin.notify.LoggingNotifier;
import de.codecentric.boot.admin.notify.Notifier;
import de.codecentric.boot.admin.notify.RemindingNotifier;
import de.codecentric.boot.admin.notify.filter.FilteringNotifier;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
*
* 重新配置消息通知
* @author niugang
*
*/
@Configuration
@EnableScheduling
public class NotifierConfig {
/**
*
提醒将每5分钟发送一次。
* @return
*/
@Bean
@Primary
public RemindingNotifier remindingNotifier() {
RemindingNotifier notifier = new RemindingNotifier(filteringNotifier(loggerNotifier()));
notifier.setReminderPeriod(TimeUnit.SECONDS.toMillis(5));
return notifier;
}
/**
* 每60秒发送一次应有的提醒。
*/
@Scheduled(fixedRate = 60_000L)
public void remind() {
remindingNotifier().sendReminders();
}
@Bean
public FilteringNotifier filteringNotifier(Notifier delegate) {
return new FilteringNotifier(delegate);
}
@Bean
public LoggingNotifier loggerNotifier() {
return new LoggingNotifier();
}
}
SecurityConfig.java
package org.niugang.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
* 基于安全认证的spring boot admin
*
* @author niugang
*
*/
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// Page with login form is served as /login.html and does a POST on /login
http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
// The UI does a POST on /logout on logout
http.logout().logoutUrl("/logout");
// The ui currently doesn't support csrf
http.csrf().disable();
// Requests for the login page and the static assets are allowed
//允许登录页面和静态资源的请求
http.authorizeRequests()
.antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
.permitAll();
// ... and any other request needs to be authorized
//这点重要:所有请求都需要认证
http.authorizeRequests().antMatchers("/**").authenticated();
// Enable so that the clients can authenticate via HTTP basic for registering
http.httpBasic();
}
}
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import de.codecentric.boot.admin.config.EnableAdminServer;
/**
*
* Copyright: Copyright (c) 2018 863263957@qq.com
*
* @ClassName: SpringBootAdminApplication.java
* @Description: 启动
* @author: niugang
* @date: 2018年9月9日 下午1:43:02
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------*
* 2018年9月9日 niugang v1.0.0 修改原因
*/
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
application.properties
server.port=8080
spring.application.name=spring-boot-admin-server
#关闭原始的spring security 认证,不关闭的话,浏览器打开就会跳出弹出框
security.basic.enabled=false
#spring boot actuator某些端点的访问时需要权限的
management.security.enabled=false
#spring boot default user.name='user'
security.user.name=root
#spring boot dafault user.password 在项目启动时打印在控制台中
security.user.password=root
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
eureka.instance.leaseRenewalIntervalInSeconds=10
eureka.client.registryFetchIntervalSeconds=5
928.spring-boot-admin-client
模拟客户端
pom.xml
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.niugang</groupId> <artifactId>928.spring-boot-admin-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>928.spring-boot-admin-client</artifactId> <dependencies> <!-- tag::dependency-eureka[] --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!--每个要注册的应用程序都必须包含Spring Boot Admin Client。 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>1.5.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--要让版本显示在应用程序列表中,您可以使用spring-boot-maven-plugin中的build-info目标 --> <executions> <execution> <goals> <goal>build-info</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
controller
package org.niugang.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/") public String hello() { return "hello spring boot admin"; } }
启动类
package org.niugang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * * Copyright: Copyright (c) 2018 863263957@qq.com * * @ClassName: SpringBootAdminClientApplication.java * @Description: 客户端启动类 * @author: niugang * @date: 2018年9月9日 下午1:54:36 * * Modification History: * Date Author Version Description *---------------------------------------------------------* * 2018年9月9日 niugang v1.0.0 修改原因 */ @SpringBootApplication @EnableDiscoveryClient public class SpringBootAdminClientApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminClientApplication.class, args); } }
application.properties
server.port=8081 spring.application.name=spring-boot-admin-client #从Spring Boot 1.5.x开始,默认情况下所有端点都是安全的。 为简洁起见,我们暂时禁用了安全性。 查看有关如何处理安全端点的安全性部分。 management.security.enabled=false eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
启动注册中心,admin-server,模拟的client.
源码地址:https://gitee.com/niugangxy/springcloud 在 spring-cloud-learn-code文件下中
微信公众号