springcloud2.0 -- sprinadmin

pom 依赖

    <dependencies>
        <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.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

组件的主要依赖是 spring-boot-admin-starter-server, admin主要是提供对各个连接到eureka的节点进行健康检查(cpu,内存,线程,日志等),组件本身提供了一个web ui, 引入security模块也是为了简单对登录用户做个限制。spring-boot-starter-actuator这个包可以提供健康数据,对于所有的client都需要加入配置,详细可以看github上面的项目。

启动函数

@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class ExampleAdminApplication {

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

    @Configuration
    public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
        private final String adminContextPath;

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

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // @formatter:off
            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();
        }
    }
}

@EnableAdminServer 开启spring-admin, 同时下面有一堆的security配置,都是官方的配置,详细可以参考spring.io中的Security那节,大致意思就是对一些访问进行重定向,针对某些接口进行身份验证,对一些资源接口和登录接口则放行。

配置文件

server:
  port: 8781
eureka:
  instance:
    hostname: localhost
    metadata-map:
      user.name: admin
      user.password: admin
    health-check-url-path: /actuator/health
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka
spring:
  security:
    user:
      name: admin
      password: admin
  profiles:
    active: development
management:
  endpoints:
    web:
      exposure:
        exclude:
          include: "*"
  endpoint:
    health:
      show-details: always
  1. spring.security.user 这个里面的内容就是配置了登录的用户名和密码,不需要多解释。
  2. eureka.instance.metadata-map 这个配置汉含义是这样的,因为admin也会检查自身的安全信息,但是由于我们做了安全验证,所以需要配置security的账号密码,这样访问的时候才能正确访问。
  3. management.endpoints.web.exposure.exclude.include 这个配置指的是要显示的安全检查内容(菜单栏的内容),这里配置全部显示了。

部分效果图

登录
首页
实例详情
运行日志

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值