提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.common.util.http; |
H |
2 |
|
|
3 |
import cn.hutool.core.codec.Base64; |
|
4 |
import cn.hutool.core.map.TableMap; |
|
5 |
import cn.hutool.core.net.url.UrlBuilder; |
|
6 |
import cn.hutool.core.util.ReflectUtil; |
|
7 |
import cn.hutool.core.util.StrUtil; |
|
8 |
import org.springframework.util.StringUtils; |
|
9 |
import org.springframework.web.util.UriComponents; |
|
10 |
import org.springframework.web.util.UriComponentsBuilder; |
|
11 |
|
|
12 |
import javax.servlet.http.HttpServletRequest; |
|
13 |
import java.net.URI; |
|
14 |
import java.nio.charset.Charset; |
|
15 |
import java.util.Map; |
|
16 |
|
|
17 |
/** |
|
18 |
* HTTP 工具类 |
|
19 |
* |
|
20 |
* @author iailab |
|
21 |
*/ |
|
22 |
public class HttpUtils { |
|
23 |
|
|
24 |
@SuppressWarnings("unchecked") |
|
25 |
public static String replaceUrlQuery(String url, String key, String value) { |
|
26 |
UrlBuilder builder = UrlBuilder.of(url, Charset.defaultCharset()); |
|
27 |
// 先移除 |
|
28 |
TableMap<CharSequence, CharSequence> query = (TableMap<CharSequence, CharSequence>) |
|
29 |
ReflectUtil.getFieldValue(builder.getQuery(), "query"); |
|
30 |
query.remove(key); |
|
31 |
// 后添加 |
|
32 |
builder.addQuery(key, value); |
|
33 |
return builder.build(); |
|
34 |
} |
|
35 |
|
|
36 |
private String append(String base, Map<String, ?> query, boolean fragment) { |
|
37 |
return append(base, query, null, fragment); |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* 拼接 URL |
|
42 |
* |
|
43 |
* copy from Spring Security OAuth2 的 AuthorizationEndpoint 类的 append 方法 |
|
44 |
* |
|
45 |
* @param base 基础 URL |
|
46 |
* @param query 查询参数 |
|
47 |
* @param keys query 的 key,对应的原本的 key 的映射。例如说 query 里有个 key 是 xx,实际它的 key 是 extra_xx,则通过 keys 里添加这个映射 |
|
48 |
* @param fragment URL 的 fragment,即拼接到 # 中 |
|
49 |
* @return 拼接后的 URL |
|
50 |
*/ |
|
51 |
public static String append(String base, Map<String, ?> query, Map<String, String> keys, boolean fragment) { |
|
52 |
UriComponentsBuilder template = UriComponentsBuilder.newInstance(); |
|
53 |
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(base); |
|
54 |
URI redirectUri; |
|
55 |
try { |
|
56 |
// assume it's encoded to start with (if it came in over the wire) |
|
57 |
redirectUri = builder.build(true).toUri(); |
|
58 |
} catch (Exception e) { |
|
59 |
// ... but allow client registrations to contain hard-coded non-encoded values |
|
60 |
redirectUri = builder.build().toUri(); |
|
61 |
builder = UriComponentsBuilder.fromUri(redirectUri); |
|
62 |
} |
|
63 |
template.scheme(redirectUri.getScheme()).port(redirectUri.getPort()).host(redirectUri.getHost()) |
|
64 |
.userInfo(redirectUri.getUserInfo()).path(redirectUri.getPath()); |
|
65 |
|
|
66 |
if (fragment) { |
|
67 |
StringBuilder values = new StringBuilder(); |
|
68 |
if (redirectUri.getFragment() != null) { |
|
69 |
String append = redirectUri.getFragment(); |
|
70 |
values.append(append); |
|
71 |
} |
|
72 |
for (String key : query.keySet()) { |
|
73 |
if (values.length() > 0) { |
|
74 |
values.append("&"); |
|
75 |
} |
|
76 |
String name = key; |
|
77 |
if (keys != null && keys.containsKey(key)) { |
|
78 |
name = keys.get(key); |
|
79 |
} |
|
80 |
values.append(name).append("={").append(key).append("}"); |
|
81 |
} |
|
82 |
if (values.length() > 0) { |
|
83 |
template.fragment(values.toString()); |
|
84 |
} |
|
85 |
UriComponents encoded = template.build().expand(query).encode(); |
|
86 |
builder.fragment(encoded.getFragment()); |
|
87 |
} else { |
|
88 |
for (String key : query.keySet()) { |
|
89 |
String name = key; |
|
90 |
if (keys != null && keys.containsKey(key)) { |
|
91 |
name = keys.get(key); |
|
92 |
} |
|
93 |
template.queryParam(name, "{" + key + "}"); |
|
94 |
} |
|
95 |
template.fragment(redirectUri.getFragment()); |
|
96 |
UriComponents encoded = template.build().expand(query).encode(); |
|
97 |
builder.query(encoded.getQuery()); |
|
98 |
} |
|
99 |
return builder.build().toUriString(); |
|
100 |
} |
|
101 |
|
|
102 |
public static String[] obtainBasicAuthorization(HttpServletRequest request) { |
|
103 |
String clientId; |
|
104 |
String clientSecret; |
|
105 |
// 先从 Header 中获取 |
|
106 |
String authorization = request.getHeader("Authorization"); |
|
107 |
authorization = StrUtil.subAfter(authorization, "Basic ", true); |
|
108 |
if (StringUtils.hasText(authorization)) { |
|
109 |
authorization = Base64.decodeStr(authorization); |
|
110 |
clientId = StrUtil.subBefore(authorization, ":", false); |
|
111 |
clientSecret = StrUtil.subAfter(authorization, ":", false); |
|
112 |
// 再从 Param 中获取 |
|
113 |
} else { |
|
114 |
clientId = request.getParameter("client_id"); |
|
115 |
clientSecret = request.getParameter("client_secret"); |
|
116 |
} |
|
117 |
|
|
118 |
// 如果两者非空,则返回 |
|
119 |
if (StrUtil.isNotEmpty(clientId) && StrUtil.isNotEmpty(clientSecret)) { |
|
120 |
return new String[]{clientId, clientSecret}; |
|
121 |
} |
|
122 |
return null; |
|
123 |
} |
|
124 |
|
|
125 |
|
|
126 |
} |