基于spring boot 1.5.22.RELEASE
1 Hypermedia for actuator MVC endpoints
1.1、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1.2、yml配置
- 启用Hypermedia
- 设置management.context-path(与spring boot 2.X 默认值相同)
server:
port: 8080
endpoints:
hypermedia:
enabled: true
actuator:
enabled: true
management:
context-path: /actuator
1.3、获取全量接口
http://localhost:8080/actuator
`
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator"
},
"dump": {
"href": "http://localhost:8080/actuator/dump"
},
"env": {
"href": "http://localhost:8080/actuator/env"
},
"trace": {
"href": "http://localhost:8080/actuator/trace"
},
"configprops": {
"href": "http://localhost:8080/actuator/configprops"
},
"auditevents": {
"href": "http://localhost:8080/actuator/auditevents"
},
"health": {
"href": "http://localhost:8080/actuator/health"
},
"loggers": {
"href": "http://localhost:8080/actuator/loggers"
},
"heapdump": {
"href": "http://localhost:8080/actuator/heapdump"
},
"beans": {
"href": "http://localhost:8080/actuator/beans"
},
"autoconfig": {
"href": "http://localhost:8080/actuator/autoconfig"
},
"mappings": {
"href": "http://localhost:8080/actuator/mappings"
},
"info": {
"href": "http://localhost:8080/actuator/info"
},
"metrics": {
"href": "http://localhost:8080/actuator/metrics"
}
}
}
`
1.4、_links中只有如下可以访问,其他会返回401
- http://localhost:8080/actuator/health
- http://localhost:8080/actuator/info
因为health、info 的Sensitive Default为false。具体信息见(https://docs.spring.io/spring-boot/docs/1.5.22.RELEASE/reference/htmlsingle/#production-ready-endpoints)
2、 引入spring security 访问其他接口
2.1 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.2 yml配置
- 指定management.port,独立于业务端口
- security.user.name、security.user.password配置账号密码(密码要使用强密码)
server:
port: 8080
endpoints:
hypermedia:
enabled: true
actuator:
enabled: true
management:
context-path: /actuator
port: 8087
security:
user:
name: test
password: BAKFX$3l
basic:
enabled: false
2.3 获取全量接口
http://localhost:8087/actuator/
{
"_links": {
"self": {
"href": "http://localhost:8087/actuator"
},
"health": {
"href": "http://localhost:8087/actuator/health"
},
"metrics": {
"href": "http://localhost:8087/actuator/metrics"
},
"loggers": {
"href": "http://localhost:8087/actuator/loggers"
},
"configprops": {
"href": "http://localhost:8087/actuator/configprops"
},
"autoconfig": {
"href": "http://localhost:8087/actuator/autoconfig"
},
"beans": {
"href": "http://localhost:8087/actuator/beans"
},
"dump": {
"href": "http://localhost:8087/actuator/dump"
},
"trace": {
"href": "http://localhost:8087/actuator/trace"
},
"env": {
"href": "http://localhost:8087/actuator/env"
},
"info": {
"href": "http://localhost:8087/actuator/info"
},
"auditevents": {
"href": "http://localhost:8087/actuator/auditevents"
},
"mappings": {
"href": "http://localhost:8087/actuator/mappings"
},
"heapdump": {
"href": "http://localhost:8087/actuator/heapdump"
}
}
}
2.4 验证
访问_links中接口,第一次需要输入账号、密码。访问正常。但是业务端口访问会弹出输入框,输入账号密码后,返回404
建议使用自定义filter认证替换spring security basic(basic 安全性低)