SpringBoot集成SpringBootAdmin實(shí)現(xiàn)監(jiān)控

效果展示

BootMonitor1.png
BootMonitor3.png
BootMonitor4.png

客戶端

maven引用

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
        </parent>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.0.0</version>
        </dependency>   

配置文件

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

安全保護(hù)

public class ActuatorAuthFilter implements Filter, Ordered {
    private AuthService authService = SpringBootBeanUtil.getBean(AuthService.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        boolean authPass = false;
        HttpServletRequest req = (HttpServletRequest) request;
        String system = req.getHeader("system");
        String token = req.getHeader("token");
        if ( !StringUtil.isEmpty(system) && !StringUtil.isEmpty(token)) {
            if(system.equals("haopanwatch") && token.equals("7e447e5d38d323b847edf2b4895eb242")){
                authPass = true;
            }
        }
        if (authPass) {
            chain.doFilter(request, response);
        } else {
            Result result = Result.errorResult().setMsg("NoAuthAccess").setCode(SystemErrorCodeEnum.ErrorCode.TokenAuthError.get_value());
            response.getWriter().println(JSON.toJSON(result));
        }

    }

    @Override
    public void destroy() {

    }

    @Override
    public int getOrder() {
        return 11;
    }
}

管理端

maven引用

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

        <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.2.0</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
    </dependencies>

配置文件

server.port=9550
spring.application.name=springboot-admin-server
#配置一個(gè)賬號(hào)和密碼
spring.security.user.name=admin
spring.security.user.password=abcd@1234

啟動(dòng)注解

@SpringBootApplication
@EnableAdminServer
public class HaopanWatchApplication {

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

    @Bean
    public ApplicationRunner applicationRunner() {
        return applicationArguments -> {
            System.out.println("haopanwatch啟動(dòng)成功!");
        };
    }
}

安全保護(hù)

@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    //項(xiàng)目應(yīng)用路徑
    private final String adminContextPath;

    public SecuritySecureConfig(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()
                //無(wú)需登錄即可訪問(wèn)
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated()
                .and()

                //登錄和登出路徑
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()

                //開(kāi)啟http basic支持,admin-client注冊(cè)時(shí)需要使用
                .httpBasic().and()
                .csrf()

                //開(kāi)啟基于cookie的csrf保護(hù)
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                //忽略這些路徑的csrf保護(hù)以便admin-client注冊(cè)
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
    }
}

客戶端認(rèn)證

@Component
public class HttpHeadersProviderConfig implements HttpHeadersProvider {

    @Override
    public HttpHeaders getHeaders(Instance instance) {
        HttpHeaders httpHeaders = new HttpHeaders();
        //設(shè)置約定好的請(qǐng)求頭參數(shù)
        httpHeaders.add("token", "7e447e5d38d323b847edf2b4895eb242");
        httpHeaders.add("system", "haopanwatch");
        return httpHeaders;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容