springbootadmin-上手

springbootadmin-上手

起男 1,365 2021-11-17

springbootadmin-上手

配置服务端

  1. 引入依赖

    		<dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-server</artifactId>
                <version>2.1.0</version>
            </dependency>
    
  2. 修改配置文件

    spring:
      application:
        name: admin-service
    server:
      port: 8000
    
  3. 启动类添加注解@EnableAdminServer

  4. 访问http://localhost:8000即可访问服务端页面

安全

  1. 引入依赖

    		<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
    
  2. 增加配置类

    @Configuration
    public class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
    
        private String adminContextPath;
        public AdminSecurityConfig(AdminServerProperties adminServerProperties){
            this.adminContextPath = adminServerProperties.getContextPath();//上下文处理路径
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            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();
        }
    }
    
    
  3. 修改配置文件

    spring:
      security:
        user:
          name: dqn
          password: dqn
    
  4. 此时访问服务端页面,或客户端向服务端注册就需要输入密码

配置客户端

  1. 导入依赖

    		<dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
                <version>2.1.0</version>
            </dependency>
    		<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
  2. 修改配置文件

    server:
      port: 8081
    
    spring:
      application:
        name: test-server
    #spirngbootadmin配置
      boot:
        admin:
          client:
            username: dqn #账号
            password: dqn #密码
            url: http://127.0.0.1:8000 #地址
    # actuator配置
    management:
      server:
        port: 9090 #使用端口
      endpoints:
        web:
          exposure:
            include: "*" #开启全部的监控终端
          base-path: /actuator #访问子路径
    
  3. 这样项目就可以被服务端健康