【问题标题】:Spring Cloud Contracts and Spring Security issuesSpring Cloud Contracts 和 Spring Security 问题
【发布时间】:2018-08-14 13:34:57
【问题描述】:

我在项目中使用 Spring Cloud Contracts 来测试微服务,一切正常。但是当我在生产者端添加 Spring Security 时,GET 返回 401 状态码而不是 200。

@Autowired
WebApplicationContext context;

@Before
public void setup() {
    RestAssuredMockMvc.webAppContextSetup(this.context);
}

我的问题是:

  1. 我必须避免合约测试中的安全设置?
  2. 如果我想考虑安全配置,如何使其工作。

【问题讨论】:

  • 如果启用了安全性,为什么要获得 200?您是否通过了合同中的认证细节?
  • 使用 SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(testUser.getLogin(), testUser.getPassword())) 添加身份验证详细信息;
  • @MarcinGrzejszczak 是的,可能需要一种简单的方法来模拟合同中的 OAuth2 身份验证令牌(以及一种生成和检查令牌的简单方法)。
  • 是的,这是模拟令牌接收所必需的

标签: spring spring-boot spring-boot-test spring-cloud-contract


【解决方案1】:

我成功地在基类上使用了自定义注释,如此处所述test-method-withsecuritycontext

@Retention(RetentionPolicy.RUNTIME)
@Inherited
@WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory.class)
public @interface WithMockCustomUserDetails {
    String username() default "email@example.com";
    String role() default "DEFAULT_ROLE";
    String password() default "123456";
}

然后

@WithMockCustomUserDetails
class AccountBase {
   ...
}

【讨论】:

    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2013-05-25
    • 2021-11-06
    • 2019-06-13
    • 1970-01-01
    • 2018-10-09
    相关资源
    最近更新 更多