问题:
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: ***