redistemplete请求spring security /oauth/token 报401错误,表示没有权限

本文探讨了如何使用Spring Boot通过RestTemplate发送OAuth 2.0 token请求,遇到401错误的解决方案,重点在于Authorization Server的`allowFormAuthenticationForClients`配置和正确设置`Authorization`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/oauth/token

这个路径就是为了获取认证中心的token


        System.out.println("函数进来了");

        MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
        paramsMap.put("grant_type", Collections.singletonList("password"));
        paramsMap.put("scope", Collections.singletonList("all"));
        paramsMap.put("client_id", Collections.singletonList("admin"));
        paramsMap.put("client_secret", Collections.singletonList("123456"));
        paramsMap.put("username", Collections.singletonList("admin"));
        paramsMap.put("password", Collections.singletonList("123456"));

        org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
//        headers.set(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8");
//        headers.set(HttpHeaders.AUTHORIZATION, "'username':'admin','password':'123456'");
        HttpEntity<MultiValueMap<String,String>> httpEntity = new HttpEntity<MultiValueMap<String,String>>(paramsMap,headers);

        restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){
            @Override
            public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
                //只要重写此方法,不去抛出HttpClientErrorException异常即可
                HttpStatus statusCode = clientHttpResponse.getStatusCode();
                System.out.println("错误码 = "+statusCode);
            }
        });
//        JSONObject response = template.postForObject(url, paramsMap, JSONObject.class);
        JSONObject response = restTemplate.exchange("http://localhost:8085/uac/oauth/token",HttpMethod.POST, httpEntity, JSONObject.class,new Object[]{}).getBody();
        System.out.println(response.toString());
        System.out.println(response.get("access_token"));

大坑来了

有很多小伙伴都出来了401错误,表示没有权限.
首先,这上面的代码是没有错了的,错就错在了认证中心里
在/oauth/token 的请求中我们指定了client_id和client_secret,所以会走ClientCredentialsTokenEndpointFilter,此时需要我们配置支持allowFormAuthenticationForClients。

@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security){
        security
                .tokenKeyAccess("permitAll()")                    //oauth/token_key是公开
                .checkTokenAccess("permitAll()")                  //oauth/check_token公开
                .allowFormAuthenticationForClients()				//表单认证(申请令牌)
        ;
    }

}

如果还有错误,是其他原因,反正肯定不是走错了filter的原因了.

当然如果你没有以上配置,其实也是可以的,我尝试在postman测试的时候我的认证中心只有以下配置:

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
                .tokenKeyAccess("isAuthenticated()");
    }

这样子上面肯定会报权限失败,需要配置Authorization
在这里插入图片描述
在这里插入图片描述
配置这个选项大概意思就是,我们通过密码模式,然后要进行授权的认证

我在postman测试成功了,但是我在RestTemplate发送测试的时候模拟同样的配置没成功,我也不清楚,可能是我没配置成功.

网上出现401错误大多也是这两种解决办法,用代码改变Authorization这个配置项怎么去做就没人说,就说改变这个配置项能成功.

### 配置和使用 `/oauth2/token` 端点 在 Spring Security OAuth2 授权服务器中,正确配置和使用 `/oauth2/token` 端点涉及多个方面。以下是详细的说明: #### 1. 添加依赖项 为了使应用程序能够处理 OAuth2 认证请求并提供令牌服务,需确保项目中的 `pom.xml` 或构建文件已包含必要的依赖项。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-authorization-server</artifactId> </dependency> ``` #### 2. 启用授权服务器功能 通过创建一个配置类来启用授权服务器的功能,并指定支持的客户端认证方式以及授予类型。 ```java @Configuration(proxyBeanMethods = false) public class AuthorizationServerConfig { @Bean public RegisteredClientRepository registeredClientRepository() { RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString()) .clientId("client-id") .clientSecret("{noop}secret") .scope(OidcScopes.OPENID) .redirectUri("http://localhost:8080/login/oauth2/code/messaging-client-oidc") .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC) .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) .build(); return new InMemoryRegisteredClientRepository(registeredClient); } @Bean public OAuth2AuthorizationService authorizationService(JdbcTemplate jdbcTemplate, RegisteredClientRepository registeredClientRepository) { return new JdbcOAuth2AuthorizationService(jdbcTemplate, registeredClientRepository); } } ``` 此部分代码定义了一个内存中的注册客户端仓库,并设置了相应的授权服务实例[^1]。 #### 3. 定义安全配置 设置 HTTP 请求的安全策略,允许访问 `/oauth2/token` 路径下的资源仅限于经过身份验证后的客户端应用。 ```java @EnableWebSecurity(debug = true) public class SecurityConfiguration extends WebSecurityConfigurerAdapter { private final RegisteredClientRepository registeredClientRepository; // 构造函数注入 protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/actuator/**").permitAll() .anyRequest().authenticated(); http.oauth2ResourceServer(oauth2 -> oauth2.jwt()); http.oauth2Login(Customizer.withDefaults()); http.formLogin(form -> form.loginPage("/login") .permitAll() ); http.apply(new OAuth2AuthorizationServerConfigurer<>()) .and() .authorizeRequests(authorizations -> authorizations.mvcMatchers("/oauth2/token").permitAll()); http.csrf(csrf -> csrf.disable()); } } ``` 上述配置启用了基于 JWT 的资源服务器保护机制,并开放了对 `/oauth2/token` API 的无条件访问权限以便测试目的;实际部署时应根据需求调整安全性措施[^2]。 #### 4. 测试接口调用 完成以上步骤之后,可以利用 Postman 或其他工具向该 URL 发送 POST 请求来进行 token 获取操作。注意要携带正确的参数如 client_id 和 client_secret 进行基本认证。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值