SpringBoot集成SpringBootAdmin实现监控

简介: SpringBootAdmin监控

效果展示

客户端

maven引用

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
        </parent>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.0.0</version>
        </dependency>

配置文件

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

安全保护

public class ActuatorAuthFilter implements Filter, Ordered {
   
   
    private AuthService authService = SpringBootBeanUtil.getBean(AuthService.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
   
   

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
   
   
        boolean authPass = false;
        HttpServletRequest req = (HttpServletRequest) request;
        String system = req.getHeader("system");
        String token = req.getHeader("token");
        if ( !StringUtil.isEmpty(system) && !StringUtil.isEmpty(token)) {
   
   
            if(system.equals("haopanwatch") && token.equals("7e447e5d38d323b847edf2b4895eb242")){
   
   
                authPass = true;
            }
        }
        if (authPass) {
   
   
            chain.doFilter(request, response);
        } else {
   
   
            Result result = Result.errorResult().setMsg("NoAuthAccess").setCode(SystemErrorCodeEnum.ErrorCode.TokenAuthError.get_value());
            response.getWriter().println(JSON.toJSON(result));
        }

    }

    @Override
    public void destroy() {
   
   

    }

    @Override
    public int getOrder() {
   
   
        return 11;
    }
}

管理端

maven引用

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <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.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
    </dependencies>

配置文件

server.port=9550
spring.application.name=springboot-admin-server
#配置一个账号和密码
spring.security.user.name=admin
spring.security.user.password=abcd@1234

启动注解

@SpringBootApplication
@EnableAdminServer
public class HaopanWatchApplication {
   
   

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

    @Bean
    public ApplicationRunner applicationRunner() {
   
   
        return applicationArguments -> {
   
   
            System.out.println("haopanwatch启动成功!");
        };
    }
}

安全保护

@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 {
   
   
        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()

                //开启http basic支持,admin-client注册时需要使用
                .httpBasic().and()
                .csrf()

                //开启基于cookie的csrf保护
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                //忽略这些路径的csrf保护以便admin-client注册
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
    }
}

客户端认证

@Component
public class HttpHeadersProviderConfig implements HttpHeadersProvider {
   
   

    @Override
    public HttpHeaders getHeaders(Instance instance) {
   
   
        HttpHeaders httpHeaders = new HttpHeaders();
        //设置约定好的请求头参数
        httpHeaders.add("token", "7e447e5d38d323b847edf2b4895eb242");
        httpHeaders.add("system", "haopanwatch");
        return httpHeaders;
    }
}
目录
相关文章
|
3天前
|
Java 数据库连接 数据安全/隐私保护
springBoot集成token认证,最全Java面试知识点梳理
springBoot集成token认证,最全Java面试知识点梳理
|
5天前
|
开发框架 监控 Java
深入探索Spring Boot的监控、管理和测试功能及实战应用
【5月更文挑战第14天】Spring Boot是一个快速开发框架,提供了一系列的功能模块,包括监控、管理和测试等。本文将深入探讨Spring Boot中监控、管理和测试功能的原理与应用,并提供实际应用场景的示例。
17 2
|
5天前
|
NoSQL Java MongoDB
【MongoDB 专栏】MongoDB 与 Spring Boot 的集成实践
【5月更文挑战第11天】本文介绍了如何将非关系型数据库MongoDB与Spring Boot框架集成,以实现高效灵活的数据管理。Spring Boot简化了Spring应用的构建和部署,MongoDB则以其对灵活数据结构的处理能力受到青睐。集成步骤包括:添加MongoDB依赖、配置连接信息、创建数据访问对象(DAO)以及进行数据操作。通过这种方式,开发者可以充分利用两者优势,应对各种数据需求。在实际应用中,结合微服务架构等技术,可以构建高性能、可扩展的系统。掌握MongoDB与Spring Boot集成对于提升开发效率和项目质量至关重要,未来有望在更多领域得到广泛应用。
【MongoDB 专栏】MongoDB 与 Spring Boot 的集成实践
|
5天前
|
消息中间件 JSON Java
RabbitMQ的springboot项目集成使用-01
RabbitMQ的springboot项目集成使用-01
|
5天前
|
搜索推荐 Java 数据库
springboot集成ElasticSearch的具体操作(系统全文检索)
springboot集成ElasticSearch的具体操作(系统全文检索)
|
5天前
|
消息中间件 Java Spring
Springboot 集成Rabbitmq之延时队列
Springboot 集成Rabbitmq之延时队列
7 0
|
5天前
|
网络协议 Java Spring
Springboot 集成websocket
Springboot 集成websocket
12 0
|
5天前
|
XML Java 数据库
springboot集成flowable
springboot集成flowable
|
5天前
|
安全 Java 数据库连接
在IntelliJ IDEA中通过Spring Boot集成达梦数据库:从入门到精通
在IntelliJ IDEA中通过Spring Boot集成达梦数据库:从入门到精通
|
5天前
|
缓存 NoSQL Java
springboot业务开发--springboot集成redis解决缓存雪崩穿透问题
该文介绍了缓存使用中可能出现的三个问题及解决方案:缓存穿透、缓存击穿和缓存雪崩。为防止缓存穿透,可校验请求数据并缓存空值;缓存击穿可采用限流、热点数据预加载或加锁策略;缓存雪崩则需避免同一时间大量缓存失效,可设置随机过期时间。文章还提及了Spring Boot中Redis缓存的配置,包括缓存null值、使用前缀和自定义过期时间,并提供了改造代码以实现缓存到期时间的个性化设置。

热门文章

最新文章