【发布时间】:2018-01-04 14:35:25
【问题描述】:
我有一个基于 Spring-Boot 版本 1.5.4.RELEASE 和 spring-boot-starter-security 构建的纯 REST 微服务。该服务没有网页,只有 JSON 输入和输出。用户名和密码在 application.properties 文件中配置。归功于http://ryanjbaxter.com/2015/01/06/securing-rest-apis-with-spring-boot/,以下配置使服务器很好地实现了基本的HTTP身份验证,它接受凭据并拒绝未经授权的请求:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests() //
.anyRequest().authenticated().and().httpBasic();
}
}
我的问题是,我想从基本 HTTP 身份验证中排除一个小 ole 路径、一个小端点。从疯狂的谷歌搜索和复制粘贴中,我修改了上面的内容:
http.csrf().disable().authorizeRequests() //
.antMatchers("/healthcheck").permitAll()
.anyRequest().authenticated().and().httpBasic();
这会编译并运行而不会发出警告,但不会打开该路径以进行未经身份验证的访问。我仍然必须提供凭据来检查服务运行状况。
我必须一一匹配路径吗?我的小型运行状况检查端点位于上下文路径的基础上,一大堆其他端点也是如此 - 一个接一个地添加路径会很麻烦。
我的 application.properties 文件的相关部分是:
security.user.name = web-user
security.user.password = web-pass
management.security.roles=SUPERUSER
也许我需要以某种方式摆弄角色?
请帮忙,提前谢谢。
更新 1:
路径信息 - 我希望保护这条路径(以及更多根路径):
localhost:8081/abcd/user
我只想打开这条路径,不需要身份验证:
localhost:8081/abcd/healthcheck
更新 2:看起来我在很大程度上重复了这个 3 年前的问题,但那里没有接受我的问题的答案:
【问题讨论】:
-
您能否发布产生问题的完整 url 请求?
-
/healhtcheck 不是 /abcd/healthcheck
标签: java rest spring-boot