Spring Resource Server + Spring Security O2Auth(Google),由于JWT无效,无法进行身份验证

wz1wpwve  于 2023-11-19  发布在  Spring
关注(0)|答案(1)|浏览(190)

我正在尝试为我的宠物项目实现“使用谷歌登录”授权。我决定先在一个小项目上练习,所以我创建了一个简单的REST应用程序,只有一个控制器。我设法完成了在谷歌控制台中创建项目的所有步骤,在那里创建了所有必要的密钥,并成功地将谷歌令牌放入 Postman 标题中。它看起来像这样ya29.A0AVA9y1v0OZNHTtELFgP-OGQdT_GY_V9mJ3FKXB2ZcOYjS7_AWx2nX1IzP8Sfnr9uxosNZNyNZK2h_q4zF1Qvhhu2bptv-tL-kSn3qVjHwfDdjqIydn3bKBkqVhIHKhmCE3PLrXuSuDJ2sML-66uFFwR28IpyaCgYKATASATASFQE65dr8xXC0Q6jWHExbZ0yL0utlKw0163
所以我开始实现spring Boot 应用程序,遇到了一些问题。我按照网络上的沿着教程配置了Spring Security ,将所有客户端id和所有这些东西放在www.example.com中application.properties,然后运行我的应用程序。惊喜,什么都不起作用。我开始挖掘,发现JwtAuthenticationProvider抱怨他“Failed to authenticate since the JWT was invalid“。经过一些谷歌搜索,我发现谷歌所谓的“访问令牌”不是一个实际的JWT令牌,但我不知道如何处理这些信息。我一定是错过了一些重要的东西,但我不知道是什么。
我的SpringSecurityConfig:

  1. @Configuration
  2. @EnableWebSecurity(debug = true)
  3. @EnableGlobalMethodSecurity(prePostEnabled = true)
  4. class SpringSecurityConfig {
  5. @Bean
  6. fun filterChain(http: HttpSecurity): SecurityFilterChain {
  7. http
  8. .authorizeRequests()
  9. .antMatchers("/**").fullyAuthenticated()
  10. .and()
  11. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  12. .and()
  13. .oauth2ResourceServer().jwt()
  14. .and()
  15. .and()
  16. .cors().and().csrf().disable()
  17. return http.build()
  18. }
  19. }

字符串
我的属性:

  1. spring.security.oauth2.client.registration.google.client-id=<MY_CLIENT_ACTUAL_VALUE>
  2. spring.security.oauth2.client.registration.google.client-secret=<MY_SECRET_ACTUAL_VALUE>
  3. spring.security.oauth2.client.registration.google.scope=openid,profile,email
  4. spring.security.oauth2.resourceserver.jwt.issuer-uri=https://accounts.google.com
  5. spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://www.googleapis.com/oauth2/v3/certs


我的控制器:

  1. @RestController
  2. @RequestMapping("/api")
  3. class TestController {
  4. @GetMapping(path = ["/alive"])
  5. private fun alive() = "OK"
  6. }


在我尝试用token header连接postman后,我得到了这个:

  1. ************************************************************
  2. Request received for GET '/api/alive':
  3. org.apache.catalina.connector.RequestFacade@51219c91
  4. servletPath:/api/alive
  5. pathInfo:null
  6. headers:
  7. authorization: Bearer ya29.A0AVA9y1v0OZNHTtELFgP-OGQdT_GY_V9mJ3FKXB2ZcOYjS7_AWx2nX1IzP8Sfnr9uxosNZNyNZK2h_q4zF1Qvhhu2bptv-tL-kSn3qVjHwfDdjqIydn3bKBkqVhIHKhmCE3PLrXuSuDJ2sML-66uFFwR28IpyaCgYKATASATASFQE65dr8xXC0Q6jWHExbZ0yL0utlKw0163
  8. user-agent: PostmanRuntime/7.29.2
  9. accept: */*
  10. postman-token: b6dd2ef8-a4e5-46bf-8d62-141831146efb
  11. host: localhost:8080
  12. accept-encoding: gzip, deflate, br
  13. connection: keep-alive
  14. cookie: JSESSIONID=715201B0645DD1329566CD5AB8509BE6
  15. Security filter chain: [
  16. DisableEncodeUrlFilter
  17. WebAsyncManagerIntegrationFilter
  18. SecurityContextPersistenceFilter
  19. HeaderWriterFilter
  20. CorsFilter
  21. LogoutFilter
  22. BearerTokenAuthenticationFilter
  23. RequestCacheAwareFilter
  24. SecurityContextHolderAwareRequestFilter
  25. AnonymousAuthenticationFilter
  26. SessionManagementFilter
  27. ExceptionTranslationFilter
  28. FilterSecurityInterceptor
  29. ]
  30. ************************************************************
  31. 2022-08-19 17:07:29,005 DEBUG [http-nio-8080-exec-1] org.springframework.security.web.FilterChainProxy: Securing GET /api/alive
  32. 2022-08-19 17:07:29,009 DEBUG [http-nio-8080-exec-1] org.springframework.security.web.context.SecurityContextPersistenceFilter: Set SecurityContextHolder to empty SecurityContext
  33. 2022-08-19 17:07:29,031 DEBUG [http-nio-8080-exec-1] org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationProvider: Failed to authenticate since the JWT was invalid
  34. 2022-08-19 17:07:29,033 DEBUG [http-nio-8080-exec-1] org.springframework.security.web.context.SecurityContextPersistenceFilter: Cleared SecurityContextHolder to complete request

dm7nw8vv

dm7nw8vv1#

您应该向服务器发送id_token(这是一个有效的JWT),而不是access_token(这不是)。

相关问题