クライアントが認可コードを取得すると、常に authserver/login にリダイレクトされるため、クライアントはトークンを取得できません。
温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
Spring を使用して Oauth2 フレームワークを構築しています。認証サーバー (localhost:9000) がクライアント (localhost:9090) に認証コードを送信すると、リクエストは localhost:9000/login にリダイレクトされます。
クライアントが使用する依存関係:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
認証サーバーが使用する依存関係:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-authorization-server</artifactId>
<version>0.2.2</version>
</dependency>
クライアントからのログレコードを見ました:
2024-04-07 23:25:21.601 DEBUG 67746 --- [nio-9090-exec-8] o.s.s.web.DefaultRedirectStrategy : Redirecting to http://127.0.0.1:9000/oauth2/authorize?response_type=code&client_id=client&scope=writeIngredients%20deleteIngredients%20openid&state=BR7IScHAvLcE4HuJ6yFsDb-G2kr7lrXjC5-n1X30SH0%3D&redirect_uri=http://127.0.0.1:9090/login/oauth2/code/client&nonce=ncVAULQ2io7A93dSgYeDH5y3mzdM52r27H70XGMkyNI
2024-04-07 23:25:21.601 DEBUG 67746 --- [nio-9090-exec-8] w.c.HttpSessionSecurityContextRepository : Did not store empty SecurityContext
2024-04-07 23:25:21.601 DEBUG 67746 --- [nio-9090-exec-8] w.c.HttpSessionSecurityContextRepository : Did not store empty SecurityContext
2024-04-07 23:25:21.601 DEBUG 67746 --- [nio-9090-exec-8] s.s.w.c.SecurityContextPersistenceFilter : Cleared SecurityContextHolder to complete request
2024-04-07 23:26:52.224 DEBUG 67746 --- [nio-9090-exec-1] o.s.security.web.FilterChainProxy : Securing GET /login/oauth2/code/client?code=T2coxAsHHnVHM7Ix-VaPIuKHfJ3R4RqxQJQogyX1LdBspIX-KehoZP7RVtyGxp5ARwri5PaQ9GNtfB8zFfhPqKUGTUaFyu6ijjQkyfywtRAFt44cBWU0e05bpqg5hfzP&state=BR7IScHAvLcE4HuJ6yFsDb-G2kr7lrXjC5-n1X30SH0%3D
2024-04-07 23:26:52.224 DEBUG 67746 --- [nio-9090-exec-1] s.s.w.c.SecurityContextPersistenceFilter : Created session 4F3EE4C7276FD87524AB76621E228958 eagerly
2024-04-07 23:26:52.224 DEBUG 67746 --- [nio-9090-exec-1] s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to empty SecurityContext
2024-04-07 23:26:52.225 DEBUG 67746 --- [nio-9090-exec-1] o.s.s.web.DefaultRedirectStrategy : Redirecting to /oauth2/authorization/client?error
2024-04-07 23:26:52.225 DEBUG 67746 --- [nio-9090-exec-1] w.c.HttpSessionSecurityContextRepository : Did not store empty SecurityContext
2024-04-07 23:26:52.225 DEBUG 67746 --- [nio-9090-exec-1] w.c.HttpSessionSecurityContextRepository : Did not store empty SecurityContext
2024-04-07 23:26:52.225 DEBUG 67746 --- [nio-9090-exec-1] s.s.w.c.SecurityContextPersistenceFilter : Cleared SecurityContextHolder to complete request
2024-04-07 23:26:52.228 DEBUG 67746 --- [nio-9090-exec-2] o.s.security.web.FilterChainProxy : Securing GET /oauth2/authorization/client?error
2024-04-07 23:26:52.228 DEBUG 67746 --- [nio-9090-exec-2] s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to empty SecurityContext
2024-04-07 23:26:52.229 DEBUG 67746 --- [nio-9090-exec-2] o.s.s.web.DefaultRedirectStrategy : Redirecting to http://127.0.0.1:9000/oauth2/authorize?response_type=code&client_id=client&scope=writeIngredients%20deleteIngredients%20openid&state=uO5wWfx0staGwByp8odWAoXgVnTHs9zsVcwoNvOKDEA%3D&redirect_uri=http://127.0.0.1:9090/login/oauth2/code/client&nonce=6olVlElcsbTR5Ex8nJ4bnhmCAlf9P5coxF-Cu1j9c8E
これは、クライアントエンドポイント:/login/oauth2/code/client が認証コードを正常に受信したことを示していますが、/oauth2/authorization/client?error にリダイレクトされます。
すでにサーバーとクライアントのクライアント設定を確認し、セッションに情報を含めるために sessionManagement も追加しました。
これはクライアントの SecurityFilterChain です。
@Bean
public SecurityFilterChain defaultChain(HttpSecurity http) throws Exception{
return http
.authorizeHttpRequests(authorize->authorize
.anyRequest().authenticated())
.oauth2Login(login->login
.loginPage("/oauth2/authorization/client"))
.oauth2Client(Customizer.withDefaults())
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
.and()
.build();
}
正文到此结束
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2635
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
热门推荐
-
浏览(193) 评论(0)