前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【疑难杂症】-一种简单高效的Spring Security oauth token兼容JSON格式的办法

【疑难杂症】-一种简单高效的Spring Security oauth token兼容JSON格式的办法

作者头像
阿提说说
发布2022-12-02 17:02:57
6470
发布2022-12-02 17:02:57
举报
文章被收录于专栏:Java技术进阶Java技术进阶
在这里插入图片描述
在这里插入图片描述

为了统一接口请求格式,要将Spring Security获取token接口改成接收JSON格式,如下是我的几种尝试,最后一种为简单有效办法。

在Spring Cloud Gateway处理JSON转application/x-www-form-urlencoded(无效)

代码是这样的

代码语言:javascript
复制
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class PlatformRequestGlobalFilter implements GlobalFilter, Ordered {

    @Value("${project.gateway.filter.order: -10}")
    private int order;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        return DataBufferUtils.join(request.getBody())
                .flatMap(dataBuffer -> {
                    byte[] bytes = new byte[dataBuffer.readableByteCount()];
                    dataBuffer.read(bytes);
                    String json = new String(bytes, StandardCharsets.UTF_8);
                    DataBufferUtils.release(dataBuffer);
                    try {
                        //json参数转换
                        HashMap<String, String> result =  new ObjectMapper().readValue(json, HashMap.class);
                        ServerHttpRequest mutatedRequest = new ServerHttpRequestDecorator(exchange.getRequest()) {
                            @Override
                            public HttpHeaders getHeaders() {
                                HttpHeaders httpHeaders = new HttpHeaders();
                                httpHeaders.putAll(super.getHeaders());
                                httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
                                return httpHeaders;
                            }

                            @Override
                            public URI getURI() {
                                //json参数转换成key=value&key1=value1形式
                                String params = mapToString(result);
                                //只是测试,地址写死了,参数是拼接的
                                return URI.create("http://localhost:8010/oauth/token?" + params);
                            }
                        };
                        //替换request
                        return chain.filter(exchange.mutate().request(mutatedRequest).build());
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                    
                    return chain.filter(exchange);

                });
    }

    private String mapToString(HashMap<String, String> map) {
        final StringBuilder strBuilder = StrUtil.builder();
        boolean isFirst = true;
        if (MapUtil.isNotEmpty(map)) {
            for (Entry<String, String> entry : map.entrySet()) {
                if (entry.getKey() != null && entry.getValue() != null) {
                    if (isFirst) {
                        isFirst = false;
                    } else {
                        strBuilder.append("&");
                    }
                    strBuilder.append(CharSequenceUtil.toUnderlineCase(entry.getKey())).append("=").append(Convert.toStr(entry.getValue()));
                }
            }
        }
        return strBuilder.toString();
    }

    @Override
    public int getOrder() {
        return order;
    }

}

原以为将json请求在网关层替换为application/x-www-form-urlencoded格式,并将参数转换key=value&key1=value1形式,拼接最后的请求地址 http://localhost:8010/oauth/token?key=value&key1=value1,这样就可以生效了,但测试后会报:java.lang.IllegalStateException: completed 错误

Filter RouteLocator 路由跳转形式(无效)

代码语言:javascript
复制
@Configuration
public class FilterConfig {

    @Bean
    public RouteLocator routes(RouteLocatorBuilder builder, ObjectMapper objectMapper) {
        return builder
                .routes()
                .route("path_route_change",
                        r -> r.path("/oauth/token")
                                .filters(f -> f
                                        .modifyRequestBody(String.class,String.class,"application/x-www-form-urlencoded",new RequestBodyRewrite(objectMapper))
                                )
                                .uri("http://localhost:8010/oauth/token"))
                .build();
    }
}

stackoverflow 提供的方式(无效)

这种办法根本不进Filter,在独立应用的时候也测试了 可见地址:https://stackoverflow.com/questions/38165131/spring-security-oauth2-accept-json

包装 oauth/token接口(有效)

代码语言:javascript
复制
	@PostMapping("oauth/api/token")
	public OAuth2AccessToken getToken(@Valid @RequestBody AuthTokenReq authTokenReq) {
		Map<String, String> params = new HashMap<>();
		params.put("grant_type", authTokenReq.getGrantType());
		params.put("client_id", authTokenReq.getClientId());
		params.put("client_secret", authTokenReq.getClientSecret());

		UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(authTokenReq.getClientId(), authTokenReq.getClientSecret(), new ArrayList<>());

		ResponseEntity<OAuth2AccessToken> oAuth2AccessToken = tokenEndpoint.postAccessToken(usernamePasswordAuthenticationToken, params);

		return oAuth2AccessToken.getBody();
	}

新写一个REST接口,调用TokenEndpoint 的postAccessToken方法,还是这种办法最简单有效。

这里我通过TokenEndpoint 直接调用了postAccessToken方法,而不是采用Http请求oauth/token再次自我请求的方式,这种方式显得更优雅,性能也更高。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-06-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在Spring Cloud Gateway处理JSON转application/x-www-form-urlencoded(无效)
  • Filter RouteLocator 路由跳转形式(无效)
  • stackoverflow 提供的方式(无效)
  • 包装 oauth/token接口(有效)
相关产品与服务
访问管理
访问管理(Cloud Access Management,CAM)可以帮助您安全、便捷地管理对腾讯云服务和资源的访问。您可以使用CAM创建子用户、用户组和角色,并通过策略控制其访问范围。CAM支持用户和角色SSO能力,您可以根据具体管理场景针对性设置企业内用户和腾讯云的互通能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档