kotlin 在dev-profile上模拟身份验证

57hvy0tb  于 2023-11-21  发布在  Kotlin
关注(0)|答案(2)|浏览(340)

我正在试用Sping Boot 3.1.x,想知道是否有一个简单的解决方案来模拟一个用户的开发配置文件的oauth2 ResourceServer。使用quarkus这是非常容易实现的,但我与Sping Boot 斗争。
基本上,设置是一个不在开发机器上运行的oidc代理背后的资源服务器。因此,需要通过application.properties配置(角色/声明)的某种fakeuser来充当请求的JWT。这样,主体的每个消费者都会收到一个具有指定角色的经过身份验证的主体。
我该如何实施这样的解决方案?

aamkag61

aamkag611#

我目前的解决方案看起来像这样:
我通过将SecurityContextHolder.getContext().authentication设置为AbstractAuthenticationToken的实现来添加OncePerRequestFilter伪造身份验证。

  1. override fun doFilterInternal(request: HttpServletRequest,
  2. response: HttpServletResponse,
  3. chain: FilterChain) {
  4. val principal = JWTClaimsSet.Builder()
  5. .claim("sub", sub)
  6. .claim("email", email)
  7. .build();
  8. val authentication = UsernamePasswordAuthenticationToken(principal, null, roles.map { SimpleGrantedAuthority(it) })
  9. SecurityContextHolder.getContext().authentication = authentication
  10. chain.doFilter(request, response)
  11. }

字符串
角色,子,电子邮件是通过配置注入模拟用户的需要。我没有麻烦使用正确的AuthToken实现,因为我不需要适合。
这段代码应该(就像我在问题中提到的)只在本地开发上运行,因为这显然是一个安全问题

展开查看全部
wkftcu5l

wkftcu5l2#

我在这个Baeldung article中详细介绍了如何在单元和集成(@SpringBootTest)测试中使用模拟的OAuth2身份验证。
你必须选择:

  • spring-security-test中的内容用于OAuth2:MockMvc请求后处理器(如jwt())或WebTestClient mutator(如mockJwt()
  • 使用我编写的测试注解(并在Maven central上发布)

作为免责声明,我写了所有这些(OAuth2的spring-security-test中的内容,我是spring-addons的repo的所有者)。我个人偏爱注解,因为它不仅在使用MockMvcWebTestClient测试@Controller时有效,而且在使用方法安全性测试任何其他类型的@Component(如@Service@Repository)时也有效。
非常多价@WithMockAuthentication的样品用法:

  1. @SpringBootTest(classes = { SecurityConfig.class, MessageService.class })
  2. class MessageServiceTests {
  3. @Autowired
  4. private SecuredService securedService;
  5. @Test
  6. @WithMockAuthentication("BAD_BOY")
  7. void givenUserIsNotGrantedWithNice_whenCallNice_thenThrows() {
  8. assertThrows(Exception.class, () -> securedService.nice());
  9. }
  10. @Test
  11. @WithMockAuthentication(name = "brice", authorities = "NICE")
  12. void givenUserIsNice_whenCallNice_thenReturnsGreeting() {
  13. assertThat(securedService.nice()).isEqualTo("Dear brice, glad to see you!");
  14. }
  15. @ParameterizedTest
  16. @AuthenticationSource(
  17. @WithMockAuthentication(name = "brice", authorities = "NICE"),
  18. @WithMockAuthentication(name = "ch4mp", authorities = { "VERY_NICE", "AUTHOR" }))
  19. void givenUserIsAuthenticated_whenCallHello_thenReturnsGreeting(@ParameterizedAuthentication Authentication auth) {
  20. assertThat(securedService.hello()).isEqualTo("Hello %s.".formatted(auth.getName()));
  21. }
  22. }

字符串
@WithJwt的示例用法,它在配置中使用身份验证转换器bean(实现Converter<Jwt, AbstractAuthenticationToken>,就像JwtAuthenticationConverter一样)`:

  1. @Import(AuthenticationFactoriesTestConf.class) // when using spring-addons-oauth2-test but not spring-addons-starter-oidc
  2. // @AddonsWebmvcComponentTest // when using spring-addons-starter-oidc along with spring-addons-starter-oidc-test (already imports AuthenticationFactoriesTestConf for you)
  3. @SpringBootTest(classes = { SecurityConfig.class, MessageService.class })
  4. class MessageServiceTests {
  5. @Autowired
  6. private SecuredService securedService;
  7. @Autowired
  8. WithJwt.AuthenticationFactory authFactory;
  9. @Test
  10. @WithJwt("igor.json")
  11. void givenUserIsIgor_whenCallNice_thenThrows() {
  12. assertThrows(Exception.class, () -> securedService.nice());
  13. }
  14. @Test
  15. @WithJwt("brice.json")
  16. void givenUserIsBrice_whenCallNice_thenReturnsGreeting() {
  17. assertThat(securedService.nice()).isEqualTo("Dear brice, glad to see you!");
  18. }
  19. @ParameterizedTest
  20. @MethodSource("identities")
  21. void givenUserIsAuthenticated_whenCallHello_thenReturnsGreeting(@ParameterizedAuthentication Authentication auth) {
  22. assertThat(securedService.hello()).isEqualTo("Hello %s.".formatted(auth.getName()));
  23. }
  24. Stream<AbstractAuthenticationToken> identities() {
  25. return authFactory.authenticationsFrom("brice.json", "igor.json");
  26. }
  27. }

展开查看全部

相关问题