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

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

在这里插入图片描述

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

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

代码是这样的

@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 路由跳转形式(无效)

@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接口(有效)

    @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再次自我请求的方式,这种方式显得更优雅,性能也更高。

相关文章
|
11天前
|
安全 Java 数据安全/隐私保护
|
2天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
9 0
|
6天前
|
JSON Java 数据处理
Spring Boot与Jsonson对象:灵活的JSON操作实战
【4月更文挑战第28天】在现代Web应用开发中,JSON数据格式的处理至关重要。假设 "Jsonson" 代表一个类似于Jackson的库,这样的工具在Spring Boot中用于处理JSON。本篇博客将介绍Spring Boot中处理JSON数据的基本概念,并通过实际例子展示如何使用类似Jackson的工具进行数据处理。
16 0
|
6天前
|
JSON fastjson Java
|
9天前
|
XML JSON Java
[AIGC] 在Spring Boot中指定请求体格式
[AIGC] 在Spring Boot中指定请求体格式
|
15天前
|
存储 安全 Java
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)(下)
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)
20 2
|
15天前
|
安全 Cloud Native Java
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)(上)
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)
24 2
|
15天前
|
存储 安全 Java
第9章 Spring Security 的测试与维护 (2024 最新版)(下)
第9章 Spring Security 的测试与维护 (2024 最新版)
20 1
|
15天前
|
安全 Java 测试技术
第9章 Spring Security 的测试与维护 (2024 最新版)(上)
第9章 Spring Security 的测试与维护 (2024 最新版)
21 0
|
15天前
|
缓存 Java 数据库
第8章 Spring Security 的常见问题与解决方案(2024 最新版)(下)
第8章 Spring Security 的常见问题与解决方案(2024 最新版)
24 0