Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spring Boot Admin

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to use Spring Boot Admin. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Introduction

Official website address

Spring Boot Admin is a project incubated by the open source community for managing and monitoring Spring Boot applications. Spring Boot Admin is divided into server (spring-boot-admin-server) and client (spring-boot-admin-client). Http communication is used between server and client to achieve data exchange. Spring-boot-admin-client needs to be integrated in a single project to allow applications to be monitored. In the SpringCloud project, spring-boot-admin-server grabs the application information directly from the registry and does not need to integrate spring-boot-admin-client for each micro-service application to realize the application management and monitoring.

2. Server build 2.1 introduce dependency

Note: the version corresponds to the Spring Boot version. For example, if my Spring Boot is 2.3.7.RELEASE, then the Spring Boot Admin version is 2.3.x.

De.codecentric spring-boot-admin-starter-server 2.3.12.2 add comments

Add an annotation to the startup class: @ EnableAdminServer

@ EnableAdminServer@SpringBootApplication (exclude = {DataSourceAutoConfiguration.class}) public class ServerApplication {public static void main (String [] args) {SpringApplication.run (ServerApplication.class, args);}} 2.3 to test

Just access the port number of the project!

For example, if the port number I configured is 9000, then you can access http://localhost:9000/ directly!

2.4 Test results

3. Client build 3.1 introduce dependency

Note: the version corresponds to the Spring Boot version. For example, if my Spring Boot is 2.3.7.RELEASE, then the Spring Boot Admin version is 2.3.x.

De.codecentric spring-boot-admin-starter-client 2.3.13.2 write configuration

Write an application.yml file:

Spring: application: name: Client boot: admin: client: # configure Admin Server (server name) url: http://localhost:9000server: port: 9001 # Open Endpoint Monitoring for SpringBoot Admin management: endpoints: web: exposure: include:'* 'logging: file: # configure the generated log file name name: admin-client.log3.3 for testing

Start the project, and then access the server-side Web management interface:

4. Security

The management background of this Spring Boot Admin can be accessed directly without an account password, which is not secure at all, so add login function to it.

Referring to the official documentation of Spring Boot Admin, we can add Spring Security-related dependencies on the Admin-Server side and access the webpage management panel only after login.

Official website document address

4.1 add dependencies

Add Spring Security dependencies on the server:

Org.springframework.boot spring-boot-starter-security4.2 Writing configuration

Write the application.yml file, write the username and password:

Server: port: 9000spring: application: name: Server security: user: name: admin password: admin4.3 write configuration class

Write the configuration class for Spring Security:

@ Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {private final String adminContextPath; public SecurityConfig (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 () / / 1. Configure all static resources and login pages for public access (anonymous access) .antMatrices (adminContextPath + "/ assets/**"). PermitAll () .antMatrices (adminContextPath + "/ login"). PermitAll () .anyRequest (). Authenticated () and () / / 2. Configure the login and logout path .formLogin () .formLogin (adminContextPath + "/ login") .formHandler (successHandler). And () .logout (). LogoutUrl (adminContextPath + "/ logout"). And () / / 3. Enable http basic support, client registration needs to use .httpBasic () .csrf () .csrf () / 4. Open Cookie-based CSRF protection. CsrfTokenRepository (CookieCsrfTokenRepository.withHttpOnlyFalse ()) / / 5. Ignore the CSRF protection of these paths so that the client can register. AdminContextPath + "/ instances", adminContextPath + "/ actuator/**");}} 4.4 modify client configuration

Modify the client's application.yml configuration file and add the username and password:

If you do not add a user name and password here, you will not be able to connect to the server:

Spring: application: name: Client boot: admin: client: # configure Admin Server (server name) url: http://localhost:9000 # configure user name username: admin # configure password password: admin4.5 for testing

Restart client and server projects

The access effect is as follows:

Thank you for reading! This is the end of the article on "how to use Spring Boot Admin". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report