《Spring in Action 第6版》第8章以curl模拟客户端向授权服务器申请token返回{error: invalid_client}问题的解决方法

问题:

在学习spring in action第8章时,构建完授权服务器后(127.0.0.1:9000)用curl模拟客户端访问授权服务器申请授权码成功,但是使用授权码再向授权服务器申请token令牌的时候返回{error:invalid_client}(错误码403)。

解决方案:

1、将书中的pom依赖org.springframework.security.experimental改为org.springframework.security,version 0.2.2。

		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-oauth2-authorization-server</artifactId>
			<version>0.2.2</version>
		</dependency>

2、在授权服务器的配置中增加ProviderSettings bean。

    @Bean
    public ProviderSettings providerSettings() {
        return ProviderSettings.builder().issuer("http://127.0.0.1:9000").build();
    }

3、对于原有RegisteredClientRepository的构建中,重构clientSettings方法以及配置对应的ClientSetting对象(新的ClientSetting只允许以clientSetting类型为输入)

    @Bean
    public RegisteredClientRepository registeredClientRepository(PasswordEncoder encoder){
        ClientSettings settings = ClientSettings.builder()
                                  .requireAuthorizationConsent(true)
                                  .build();
        RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
                                                            .clientId("client")
                                                            .clientSecret(encoder.encode("XXXXX"))
                                                            .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                                                            .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                                                            .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
                                                            .redirectUri("http://127.0.0.1:9090/login/oauth2/code/client")
                                                            .scope("writeIngredients")
                                                            .scope("deleteIngredients")
                                                            .clientSettings(settings)
                                                            .build();
        return new InMemoryRegisteredClientRepository(registeredClient);
    }

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值