三、使用Spring Security拦截所有的Actuator(监控)请求,同时放行其他请求

问题:

  Springboot通过与Actuator(监控)集成,我们可以全面的监控生产服务器的状况。本来我的本意是分别设置项目的端口和Actuator(监控)的端口,但是报错,不让分别设置端口,那只能用项目的端口。这样就带来一个问题:如何确保Actuator(监控)只能被我访问,而不会被别人恶意访问?

解决办法:

  这里我直接使用了Spring Security组件帮助我们拦截所有对Actuator(监控)的请求,只有通过验证的用户才能访问。具体解决办法如下:

1.添加依赖

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

2.创建安全配置类,设置所有对监控的访问都需要验证,而其他访问不需要:SecurityConfig

package com.yuedu.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;


/**
 * @author 咸鱼
 * @date 2019-03-27 21:57
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //对actuator监控所用的访问全部需要认证
        http.formLogin().and().authorizeRequests().antMatchers("/actuator/*").authenticated();
    }
}

3.配置验证的用户名、密码

spring:
  security:
    user:
      name: ***
      password: ***
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值