oauth2添加get请求方式获取token

oauth2通过/oauth/token接口请求获取token,以下为oauth2源码中获取token代码能看到allowedRequestMethods中只存放了post的请求所以默认为只支持post请求。
但我们需要通过get请求或者别的请求去访问怎么实现呢?例如nigix做域名跳转时,默认将post的请求更改为get请求,这个时候/oauth/token就获取不到token了。

private Set<HttpMethod> allowedRequestMethods = new HashSet<HttpMethod>(Arrays.asList(HttpMethod.POST));

    @RequestMapping(value = "/oauth/token", method=RequestMethod.GET)
    public ResponseEntity<OAuth2AccessToken> getAccessToken(Principal principal, @RequestParam
    Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        if (!allowedRequestMethods.contains(HttpMethod.GET)) {
            throw new HttpRequestMethodNotSupportedException("GET");
        }
        return postAccessToken(principal, parameters);
    }

    @RequestMapping(value = "/oauth/token", method=RequestMethod.POST)
    public ResponseEntity<OAuth2AccessToken> postAccessToken(Principal principal, @RequestParam
    Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {

根据以上需求优先想到的是通过修改配置文件往allowedRequestMethods中加入get方法,具体该方式未实现,以下为添加配置类实现配置更新

@Configuration
public class AllowedMethodConfig {
    @Autowired
    private TokenEndpoint tokenEndpoint;

    @PostConstruct
    public void reconfigure() {
        Set<HttpMethod> allowedMethods =
            new HashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.POST));
        tokenEndpoint.setAllowedRequestMethods(allowedMethods);
    }
}

添加以上类,重新启动完成后就能通过get请求/oauth/token获取token。
以上为全部内容,如有问题或建议希望大家指出。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您可以使用Spring Security OAuth2来获取token。以下是基本的步骤: 1. 首先,您需要添加所需的依赖项。在您的项目的pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> ``` 2. 在application.properties或application.yml文件中配置OAuth2客户端的信息。例如: ```yaml spring: security: oauth2: client: registration: my-client-id: client-id: your-client-id client-secret: your-client-secret provider: your-authorization-server ``` 请替换`my-client-id`,`your-client-id`,`your-client-secret`和`your-authorization-server`为您的实际值。 3. 在您的代码中,您可以使用`RestTemplate`或`WebClient`来发送请求获取token。以下是使用`RestTemplate`的示例代码: ```java import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder; import org.springframework.security.oauth2.client.registration.ClientRegistration; import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; import org.springframework.security.oauth2.core.AuthorizationGrantType; import org.springframework.security.oauth2.core.OAuth2AccessToken; import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationCodeGrantRequest; import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.net.URI; import java.util.Collections; @RestController public class OAuth2Controller { private final ClientRegistrationRepository clientRegistrationRepository; private final OAuth2AuthorizedClientManager authorizedClientManager; public OAuth2Controller(ClientRegistrationRepository clientRegistrationRepository, OAuth2AuthorizedClientManager authorizedClientManager) { this.clientRegistrationRepository = clientRegistrationRepository; this.authorizedClientManager = authorizedClientManager; } @GetMapping("/oauth2/token") public String getToken() { // 获取ClientRegistration ClientRegistration clientRegistration = clientRegistrationRepository.findByRegistrationId("my-client-id"); // 构建OAuth2AuthorizationRequest OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode() .clientId(clientRegistration.getClientId()) .authorizationUri(clientRegistration.getProviderDetails().getAuthorizationUri()) .redirectUri(URI.create("http://localhost:8080/oauth2/callback")) .scopes(clientRegistration.getScopes()) .state("state") .build(); // 构建OAuth2AuthorizationResponse OAuth2AuthorizationResponse authorizationResponse = OAuth2AuthorizationResponse.success("authorization-code") .redirectUri("http://localhost:8080/oauth2/callback") .state("state") .build(); // 构建OAuth2AuthorizationCodeGrantRequest OAuth2AuthorizationCodeGrantRequest authorizationCodeGrantRequest = new OAuth2AuthorizationCodeGrantRequest( clientRegistration, authorizationRequest, authorizationResponse); // 构建OAuth2AuthorizedClientProvider OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder() .authorizationCode() .build(); // 获取OAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient = authorizedClientManager.authorize(authorizationCodeGrantRequest); // 构建请求头 HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.setBearerAuth(authorizedClient.getAccessToken().getTokenValue()); // 发送请求获取响应 RequestEntity<Void> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, URI.create("http://api.example.com/resource")); ResponseEntity<String> responseEntity = new RestTemplate().exchange(requestEntity, String.class); return responseEntity.getBody(); } } ``` 请确保替换`my-client-id`和`http://localhost:8080/oauth2/callback`为实际值。在这个例子中,我们模拟了一个授权码授权流程来获取token,并使用token发送请求获取资源的响应。 这只是一个简单的示例,实际的实现可能会有所不同,具体取决于您的认证服务器和需求。您可以根据您的情况进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值