SpringCloud学习(二)----- SpringBoot Admin搭建(与Eureka整合)

SpringCloud版本:2021.0.1     SpringBoot版本:2.6.3

系列文章

SpringCloud学习(一)----- Eureka搭建

SpringCloud学习(二)----- SpringBoot Admin搭建(与Eureka整合)

SpringCloud学习(三)----- Gatewayw网关搭建

SpringCloud学习(四)----- Gatewayw网关完善(限流)

SpringCloud学习(五)----- Gatewayw网关完善(Resilience4j断路器)

SpringCloud学习(六)----- Gatewayw网关完善(防止SQL注入)

SpringCloud学习(七)----- 使用Feign调用别的微服务的方法

SpringCloud学习(八)----- Gateway网关及其他微服务接入Swagger接口文档

一、创建项目

1、打开IDEA,新建项目,因为之前已经建好Eureka项目了,所以这次打开Eureka项目再新建一个Module项目,这样两个项目就会在同一个界面了。

2、前面的和之前新建Eureka项目一样,不过这次选的依赖不一样了,这次选了SpringBoot admin 的server服务和Eureka的client服务,这样以后注册到Eureka的服务就会自动同步到SpringBoot admin,不用再对SpringBoot admin进行注册。

 3、在项目的启动类上面加上@EnableDiscoveryClient@EnableAdminServer这个注解,一个是说明像Eureka注册的,一个是说明当前服务为SpringBoot admin服务端的

4、修改application.yml的配置,记得如果要注册的Eureka有设置账号密码,那么这里的配置文件就得加上去。

server:
  port: 8769
spring:
  application:
    name: test-admin
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    hostname: 127.0.0.1
  client:
    register-with-eureka: true
    fetch-registry: true
    registryFetchIntervalSeconds: 5
    ##Eureka账号密码
    service-url:
      defaultZone: http://test:123456@${eureka.instance.hostname}:8001/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

 5、启动Eureka和SpringBoot admin 服务后进入http://127.0.0.1:8769/,就可看到admin服务已经注册到Eureka并可以看到监控页面了。

二、给SpringBoot Admin加上密码登陆的验证

1、修改application.yml的配置文件,加上账号密码,

server:
  port: 8769
spring:
  application:
    name: test-admin
  security:
    user:
      name: test
      password: 123456
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    hostname: 127.0.0.1
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}
  client:
    register-with-eureka: true
    fetch-registry: true
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:8001/eureka/

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

2、pom.xml配置文件也得加上新依赖

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

 3、新建config文件夹,新建配置文件MonitorWebSecurityConfigure

@EnableWebSecurity
public class MonitorWebSecurityConfigure extends WebSecurityConfigurerAdapter {
    private final AdminServerProperties adminServer;

    public MonitorWebSecurityConfigure(AdminServerProperties adminServer) {
        this.adminServer = adminServer;
    }

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

        http
                .authorizeRequests()
                .antMatchers(this.adminServer.getContextPath() + "/assets/**").permitAll()
                .antMatchers(this.adminServer.getContextPath() + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage(this.adminServer.getContextPath() + "/login")
                .successHandler(successHandler)
                .and()
                .logout()
                .logoutUrl(this.adminServer.getContextPath() + "/logout")
                .and()
                .httpBasic()
                .and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringRequestMatchers(
                        new AntPathRequestMatcher(this.adminServer.getContextPath() +
                                "/instances", HttpMethod.POST.toString()),
                        new AntPathRequestMatcher(this.adminServer.getContextPath() +
                                "/instances/*", HttpMethod.DELETE.toString()),
                        new AntPathRequestMatcher(this.adminServer.getContextPath() + "/actuator/**"))
                .and()
                .rememberMe()
                .key(UUID.randomUUID().toString())
                .tokenValiditySeconds(1209600);
    }
}

4、再重启,就需要输入账号和密码了  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值