【发布时间】:2020-03-15 08:10:10
【问题描述】:
我有一个 spring-boot REST API 应用程序。 REST 端点受 spring-security 保护。
这是spring-security的配置:
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.exceptionHandling().authenticationEntryPoint(new CustomForbiddenErrorHandler())
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.antMatchers("/api/**").authenticated() // HTTP 403 if the request is not authenticated
.antMatchers("/**").permitAll();
}
它工作正常。如果我在 HTTP 标头上没有授权令牌的情况下进行休息调用,我会返回正确的 HTTP 错误代码。
如果没有按顺序显示,我可以强制 spring 以某种方式添加一个自我身份验证令牌,这样我就可以在没有自己的访问管理系统的情况下进行 REST 调用吗?稍后安装。
我不是在问如何编写 JUnit 测试。我要问的是如何动态生成模拟身份验证令牌,如果不存在则将其添加到请求中。
【问题讨论】:
标签: java spring spring-boot spring-security spring-security-oauth2