springboot集成Admin「服务端」

394 阅读1分钟

强大的springboot服务监控工具,主要还有优秀的图形界面。

一、服务端简单搭建

开发环境

  • Spring Boot:2.1.6.RELEASE
  • Spring Boot Admin Server:2.1.6

1. 引入POM依赖

<dependency>
       <groupId>de.codecentric</groupId>
       <artifactId>spring-boot-admin-starter-server</artifactId>
       <version>2.1.6</version>
</dependency>

2. 启动类Code

@EnableAdminServer //开启admin服务端
@SpringBootApplication
public class AdminServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminServerApplication.class,args);
    }
}

3. application.yml配置

server:
  port: 7001
  servlet:
    context-path: /springboot-admin-server
spring:
  application:
    name: springboot-admin-server

启动服务后,访问:http://localhost:7001/springboot-admin-server/

二、安全认证

1. 增加POM依赖

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

2. SecuritySecureConfig 拦截器Code

@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServer) {
        this.adminContextPath = adminServer.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        http.authorizeRequests()
                .antMatchers(adminContextPath + "/login",
                        adminContextPath + "/assets/**",
                        adminContextPath + "/manage/**",
                        adminContextPath + "/actuator/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler)
                .and()
                .logout().logoutUrl(adminContextPath + "/logout")
                .and()
                .csrf().disable();
    }

}

3. 增加安全认证的账号密码

spring:
  security:
    user:
      name: admin
      password: admin

启动服务后,访问:http://localhost:7001/springboot-admin-server/

到此,springboot-admin 服务端已搭建妥了...

springboot集成admin DEMO : gitee.com/renxiaoshi/…