Spring Security實(shí)現(xiàn)微信公眾號(hào)網(wǎng)頁授權(quán)功能
微信公眾號(hào)提供了微信支付、微信優(yōu)惠券、微信H5紅包、微信紅包封面等等促銷工具來幫助我們的應(yīng)用拉新?;睢5沁@些福利要想正確地發(fā)放到用戶的手里就必須拿到用戶特定的(微信應(yīng)用)微信標(biāo)識(shí)openid甚至是用戶的微信用戶信息。如果用戶在微信客戶端中訪問我們第三方網(wǎng)頁,公眾號(hào)可以通過微信網(wǎng)頁授權(quán)機(jī)制,來獲取用戶基本信息,進(jìn)而實(shí)現(xiàn)業(yè)務(wù)邏輯。今天就結(jié)合Spring Security來實(shí)現(xiàn)一下微信公眾號(hào)網(wǎng)頁授權(quán)。
環(huán)境準(zhǔn)備
在開始之前我們需要準(zhǔn)備好微信網(wǎng)頁開發(fā)的環(huán)境。
微信公眾號(hào)服務(wù)號(hào)
請(qǐng)注意,一定是微信公眾號(hào)服務(wù)號(hào),只有服務(wù)號(hào)才提供這樣的能力。像胖哥的這樣公眾號(hào)雖然也是認(rèn)證過的公眾號(hào),但是只能發(fā)發(fā)文章并不具備提供服務(wù)的能力。但是微信公眾平臺(tái)提供了沙盒功能來模擬服務(wù)號(hào),可以降低開發(fā)難度,你可以到微信公眾號(hào)測(cè)試賬號(hào)頁面申請(qǐng),申請(qǐng)成功后別忘了關(guān)注測(cè)試公眾號(hào)。
微信公眾號(hào)服務(wù)號(hào)只有企事業(yè)單位、政府機(jī)關(guān)才能開通。
內(nèi)網(wǎng)穿透
因?yàn)槲⑿欧?wù)器需要回調(diào)開發(fā)者提供的回調(diào)接口,為了能夠本地調(diào)試,內(nèi)網(wǎng)穿透工具也是必須的。啟動(dòng)內(nèi)網(wǎng)穿透后,需要把內(nèi)網(wǎng)穿透工具提供的虛擬域名配置到微信測(cè)試帳號(hào)的回調(diào)配置中

打開后只需要填寫域名,不要帶協(xié)議頭。例如回調(diào)是https://felord.cn/wechat/callback,只能填寫成這樣:

然后我們就可以開發(fā)了。
OAuth2.0客戶端集成
基于 Spring Security 5.x
微信網(wǎng)頁授權(quán)的文檔在網(wǎng)頁授權(quán),這里不再贅述。我們只聊聊如何結(jié)合Spring Security的事。微信網(wǎng)頁授權(quán)是通過OAuth2.0機(jī)制實(shí)現(xiàn)的,在用戶授權(quán)給公眾號(hào)后,公眾號(hào)可以獲取到一個(gè)網(wǎng)頁授權(quán)特有的接口調(diào)用憑證(網(wǎng)頁授權(quán)access_token),通過網(wǎng)頁授權(quán)獲得的access_token可以進(jìn)行授權(quán)后接口調(diào)用,如獲取用戶的基本信息。
我們需要引入Spring Security提供的OAuth2.0相關(guān)的模塊:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
由于我們需要獲取用戶的微信信息,所以要用到OAuth2.0 Login;如果你用不到用戶信息可以選擇OAuth2.0 Client。
微信網(wǎng)頁授權(quán)流程
接著按照微信提供的流程來結(jié)合Spring Security。
獲取授權(quán)碼code
微信網(wǎng)頁授權(quán)使用的是OAuth2.0的授權(quán)碼模式。我們先來看如何獲取授權(quán)碼。
這是微信獲取code的OAuth2.0端點(diǎn)模板,這不是一個(gè)純粹的OAuth2.0協(xié)議。微信做了一些參數(shù)上的變動(dòng)。這里原生的client_id被替換成了appid,而且末尾還要加#wechat_redirect 。這無疑增加了集成的難度。
這里先放一放,我們目標(biāo)轉(zhuǎn)向Spring Security的code獲取流程。
Spring Security會(huì)提供一個(gè)模版鏈接:
{baseUrl}/oauth2/authorization/{registrationId}
當(dāng)使用該鏈接請(qǐng)求OAuth2.0客戶端時(shí)會(huì)被OAuth2AuthorizationRequestRedirectFilter攔截。機(jī)制這里不講了,在我個(gè)人博客felord.cn中的Spring Security 實(shí)戰(zhàn)干貨:客戶端OAuth2授權(quán)請(qǐng)求的入口一文中有詳細(xì)闡述。
攔截之后會(huì)根據(jù)配置組裝獲取授權(quán)碼的請(qǐng)求URL,由于微信的不一樣所以我們針對(duì)性的定制,也就是改造OAuth2AuthorizationRequestRedirectFilter中的OAuth2AuthorizationRequestResolver。
自定義URL
因?yàn)镾pring Security會(huì)根據(jù)模板鏈接去組裝一個(gè)鏈接而不是我們填參數(shù)就行了,所以需要我們對(duì)構(gòu)建URL的處理器進(jìn)行自定義。
/**
* 兼容微信的oauth2 端點(diǎn).
*
* @author n1
* @since 2021 /8/11 17:04
*/
public class WechatOAuth2AuthRequestBuilderCustomizer {
private static final String WECHAT_ID= "wechat";
/**
* Customize.
*
* @param builder the builder
*/
public static void customize(OAuth2AuthorizationRequest.Builder builder) {
String regId = (String) builder.build()
.getAttributes()
.get(OAuth2ParameterNames.REGISTRATION_ID);
if (WECHAT_ID.equals(regId)){
builder.authorizationRequestUri(WechatOAuth2RequestUriBuilderCustomizer::customize);
}
}
/**
* 定制微信OAuth2請(qǐng)求URI
*
* @author n1
* @since 2021 /8/11 15:31
*/
private static class WechatOAuth2RequestUriBuilderCustomizer {
/**
* 默認(rèn)情況下Spring Security會(huì)生成授權(quán)鏈接:
* {@code https://open.weixin.qq.com/connect/oauth2/authorize?response_type=code
* &client_id=wxdf9033184b238e7f
* &scope=snsapi_userinfo
* &state=5NDiQTMa9ykk7SNQ5-OIJDbIy9RLaEVzv3mdlj8TjuE%3D
* &redirect_uri=https%3A%2F%2Fmovingsale-h5-test.nashitianxia.com}
* 缺少了微信協(xié)議要求的{@code #wechat_redirect},同時(shí) {@code client_id}應(yīng)該替換為{@code app_id}
*
* @param builder the builder
* @return the uri
*/
public static URI customize(UriBuilder builder) {
String reqUri = builder.build().toString()
.replaceAll("client_id=", "appid=")
.concat("#wechat_redirect");
return URI.create(reqUri);
}
}
}
配置解析器
把上面?zhèn)€性化改造的邏輯配置到OAuth2AuthorizationRequestResolver:
/**
* 用來從{@link javax.servlet.http.HttpServletRequest}中檢索Oauth2需要的參數(shù)并封裝成OAuth2請(qǐng)求對(duì)象{@link OAuth2AuthorizationRequest}
*
* @param clientRegistrationRepository the client registration repository
* @return DefaultOAuth2AuthorizationRequestResolver
*/
private OAuth2AuthorizationRequestResolver oAuth2AuthorizationRequestResolver(ClientRegistrationRepository clientRegistrationRepository) {
DefaultOAuth2AuthorizationRequestResolver resolver = new DefaultOAuth2AuthorizationRequestResolver(clientRegistrationRepository,
OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI);
resolver.setAuthorizationRequestCustomizer(WechatOAuth2AuthRequestBuilderCustomizer::customize);
return resolver;
}
配置到Spring Security
適配好的OAuth2AuthorizationRequestResolver配置到HttpSecurity,偽代碼:
httpSecurity.oauth2Login()
// 定制化授權(quán)端點(diǎn)的參數(shù)封裝
.authorizationEndpoint().authorizationRequestResolver(authorizationRequestResolver)
通過code換取網(wǎng)頁授權(quán)access_token
接下來第二步是用code去換token。
構(gòu)建請(qǐng)求參數(shù)
這是微信網(wǎng)頁授權(quán)獲取access_token的模板:
GET https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
其中前半段https://api.weixin.qq.com/sns/oauth2/refresh_token可以通過配置OAuth2.0的token-uri來指定;后半段參數(shù)需要我們針對(duì)微信進(jìn)行定制。Spring Security中定制token-uri的工具由OAuth2AuthorizationCodeGrantRequestEntityConverter這個(gè)轉(zhuǎn)換器負(fù)責(zé),這里需要來改造一下。
我們先拼接參數(shù):
private MultiValueMap<String, String> buildWechatQueryParameters(OAuth2AuthorizationCodeGrantRequest authorizationCodeGrantRequest) {
// 獲取微信的客戶端配置
ClientRegistration clientRegistration = authorizationCodeGrantRequest.getClientRegistration();
OAuth2AuthorizationExchange authorizationExchange = authorizationCodeGrantRequest.getAuthorizationExchange();
MultiValueMap<String, String> formParameters = new LinkedMultiValueMap<>();
// grant_type
formParameters.add(OAuth2ParameterNames.GRANT_TYPE, authorizationCodeGrantRequest.getGrantType().getValue());
// code
formParameters.add(OAuth2ParameterNames.CODE, authorizationExchange.getAuthorizationResponse().getCode());
// 如果有redirect-uri
String redirectUri = authorizationExchange.getAuthorizationRequest().getRedirectUri();
if (redirectUri != null) {
formParameters.add(OAuth2ParameterNames.REDIRECT_URI, redirectUri);
}
//appid
formParameters.add("appid", clientRegistration.getClientId());
//secret
formParameters.add("secret", clientRegistration.getClientSecret());
return formParameters;
}
然后生成RestTemplate的請(qǐng)求對(duì)象RequestEntity:
@Override
public RequestEntity<?> convert(OAuth2AuthorizationCodeGrantRequest authorizationCodeGrantRequest) {
ClientRegistration clientRegistration = authorizationCodeGrantRequest.getClientRegistration();
HttpHeaders headers = getTokenRequestHeaders(clientRegistration);
String tokenUri = clientRegistration.getProviderDetails().getTokenUri();
// 針對(duì)微信的定制 WECHAT_ID表示為微信公眾號(hào)專用的registrationId
if (WECHAT_ID.equals(clientRegistration.getRegistrationId())) {
MultiValueMap<String, String> queryParameters = this.buildWechatQueryParameters(authorizationCodeGrantRequest);
URI uri = UriComponentsBuilder.fromUriString(tokenUri).queryParams(queryParameters).build().toUri();
return RequestEntity.get(uri).headers(headers).build();
}
// 其它 客戶端
MultiValueMap<String, String> formParameters = this.buildFormParameters(authorizationCodeGrantRequest);
URI uri = UriComponentsBuilder.fromUriString(tokenUri).build()
.toUri();
return new RequestEntity<>(formParameters, headers, HttpMethod.POST, uri);
}
這樣兼容性就改造好了。
兼容token返回解析
微信公眾號(hào)授權(quán)token-uri的返回值雖然文檔說是個(gè)json,可它喵的Content-Type是text-plain。如果是application/json,Spring Security就直接接收了。你說微信坑不坑?我們只能再寫個(gè)適配來正確的反序列化微信接口的返回值。
Spring Security 中對(duì)token-uri的返回值的解析轉(zhuǎn)換同樣由OAuth2AccessTokenResponseClient中的OAuth2AccessTokenResponseHttpMessageConverter負(fù)責(zé)。
首先增加Content-Type為text-plain的適配;其次因?yàn)镾pring Security接收token返回的對(duì)象要求必須顯式聲明tokenType,而微信返回的響應(yīng)體中沒有,我們一律指定為OAuth2AccessToken.TokenType.BEARER即可兼容。代碼比較簡(jiǎn)單就不放了,有興趣可以去看我給的DEMO。
配置到Spring Security
先配置好我們上面兩個(gè)步驟的請(qǐng)求客戶端:
/**
* 調(diào)用token-uri去請(qǐng)求授權(quán)服務(wù)器獲取token的OAuth2 Http 客戶端
*
* @return OAuth2AccessTokenResponseClient
*/
private OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> accessTokenResponseClient() {
DefaultAuthorizationCodeTokenResponseClient tokenResponseClient = new DefaultAuthorizationCodeTokenResponseClient();
tokenResponseClient.setRequestEntityConverter(new WechatOAuth2AuthorizationCodeGrantRequestEntityConverter());
OAuth2AccessTokenResponseHttpMessageConverter tokenResponseHttpMessageConverter = new OAuth2AccessTokenResponseHttpMessageConverter();
// 微信返回的content-type 是 text-plain
tokenResponseHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON,
MediaType.TEXT_PLAIN,
new MediaType("application", "*+json")));
// 兼容微信解析
tokenResponseHttpMessageConverter.setTokenResponseConverter(new WechatMapOAuth2AccessTokenResponseConverter());
RestTemplate restTemplate = new RestTemplate(
Arrays.asList(new FormHttpMessageConverter(),
tokenResponseHttpMessageConverter
));
restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
tokenResponseClient.setRestOperations(restTemplate);
return tokenResponseClient;
}
再把請(qǐng)求客戶端配置到HttpSecurity:
// 獲取token端點(diǎn)配置 比如根據(jù)code 獲取 token httpSecurity.oauth2Login() .tokenEndpoint().accessTokenResponseClient(accessTokenResponseClient)
根據(jù)token獲取用戶信息
微信公眾號(hào)網(wǎng)頁授權(quán)獲取用戶信息需要scope包含snsapi_userinfo。
Spring Security中定義了一個(gè)OAuth2.0獲取用戶信息的抽象接口:
@FunctionalInterface
public interface OAuth2UserService<R extends OAuth2UserRequest, U extends OAuth2User> {
U loadUser(R userRequest) throws OAuth2AuthenticationException;
}
所以我們針對(duì)性的實(shí)現(xiàn)即可,需要實(shí)現(xiàn)三個(gè)相關(guān)概念。
OAuth2UserRequest
OAuth2UserRequest是請(qǐng)求user-info-uri的入?yún)?shí)體,包含了三大塊屬性:
ClientRegistration微信OAuth2.0客戶端配置OAuth2AccessToken從token-uri獲取的access_token的抽象實(shí)體additionalParameters一些token-uri返回的額外參數(shù),比如openid就可以從這里面取得
根據(jù)微信獲取用戶信息的端點(diǎn)API這個(gè)能滿足需要,不過需要注意的是。如果使用的是 OAuth2.0 Client 就無法從additionalParameters獲取openid等額外參數(shù)。
OAuth2User
這個(gè)用來封裝微信用戶信息,細(xì)節(jié)看下面的注釋:
/**
* 微信授權(quán)的OAuth2User用戶信息
*
* @author n1
* @since 2021/8/12 17:37
*/
@Data
public class WechatOAuth2User implements OAuth2User {
private String openid;
private String nickname;
private Integer sex;
private String province;
private String city;
private String country;
private String headimgurl;
private List<String> privilege;
private String unionid;
@Override
public Map<String, Object> getAttributes() {
// 原本返回前端token 但是微信給的token比較敏感 所以不返回
return Collections.emptyMap();
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
// 這里放scopes 或者其它你業(yè)務(wù)邏輯相關(guān)的用戶權(quán)限集 目前沒有什么用
return null;
}
@Override
public String getName() {
// 用戶唯一標(biāo)識(shí)比較合適,這個(gè)不能為空啊,如果你能保證unionid不為空,也是不錯(cuò)的選擇。
return openid;
}
}
注意: getName()一定不能返回null。
OAuth2UserService
參數(shù)OAuth2UserRequest和返回值OAuth2User都準(zhǔn)備好了,就剩下去請(qǐng)求微信服務(wù)器了。借鑒請(qǐng)求token-uri的實(shí)現(xiàn),還是一個(gè)RestTemplate調(diào)用,核心就這幾行:
LinkedMultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(); // access_token queryParams.add(OAuth2ParameterNames.ACCESS_TOKEN, userRequest.getAccessToken().getTokenValue()); // openid queryParams.add(OPENID_KEY, String.valueOf(userRequest.getAdditionalParameters().get(OPENID_KEY))); // lang=zh_CN queryParams.add(LANG_KEY, DEFAULT_LANG); // 構(gòu)建 user-info-uri端點(diǎn) URI userInfoEndpoint = UriComponentsBuilder.fromUriString(userInfoUri).queryParams(queryParams).build().toUri(); // 請(qǐng)求 return this.restOperations.exchange(userInfoEndpoint, HttpMethod.GET, null, OAUTH2_USER_OBJECT);
配置到Spring Security
// 獲取用戶信息端點(diǎn)配置 根據(jù)accessToken獲取用戶基本信息
httpSecurity.oauth2Login()
.userInfoEndpoint().userService(oAuth2UserService);
這里補(bǔ)充一下,寫一個(gè)授權(quán)成功后跳轉(zhuǎn)的接口并配置為授權(quán)登錄成功后的跳轉(zhuǎn)的url。
// 默認(rèn)跳轉(zhuǎn)到 / 如果沒有會(huì) 404 所以弄個(gè)了接口
httpSecurity.oauth2Login().defaultSuccessUrl("/weixin/h5/redirect")
在這個(gè)接口里可以通過@RegisteredOAuth2AuthorizedClient和@AuthenticationPrincipal分別拿到認(rèn)證客戶端的信息和用戶信息。
@GetMapping("/h5/redirect")
public void sendRedirect(HttpServletResponse response,
@RegisteredOAuth2AuthorizedClient("wechat") OAuth2AuthorizedClient authorizedClient,
@AuthenticationPrincipal WechatOAuth2User principal) throws IOException {
//todo 你可以再這里模擬一些授權(quán)后的業(yè)務(wù)邏輯 比如用戶靜默注冊(cè) 等等
// 當(dāng)前認(rèn)證的客戶端 token 不要暴露給前臺(tái)
OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
System.out.println("accessToken = " + accessToken);
// 當(dāng)前用戶的userinfo
System.out.println("principal = " + principal);
response.sendRedirect("https://felord.cn");
}
到此微信公眾號(hào)授權(quán)就集成到Spring Security中了。
相關(guān)配置
application.yaml相關(guān)的配置:
spring:
security:
oauth2:
client:
registration:
wechat:
# 可以去試一下沙箱
# 公眾號(hào)服務(wù)號(hào) appid
client-id: wxdf9033184b2xxx38e7f
# 公眾號(hào)服務(wù)號(hào) secret
client-secret: bf1306baaa0dxxxxxxb15eb02d68df5
# oauth2 login 用 '{baseUrl}/login/oauth2/code/{registrationId}' 會(huì)自動(dòng)解析
# oauth2 client 寫你業(yè)務(wù)的鏈接即可
redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
authorization-grant-type: authorization_code
scope: snsapi_userinfo
provider:
wechat:
authorization-uri: https://open.weixin.qq.com/connect/oauth2/authorize
token-uri: https://api.weixin.qq.com/sns/oauth2/access_token
user-info-uri: https://api.weixin.qq.com/sns/userinfo
到此這篇關(guān)于Spring Security中實(shí)現(xiàn)微信網(wǎng)頁授權(quán)的文章就介紹到這了,更多相關(guān)Spring Security微信網(wǎng)頁授權(quán)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解使用Spring Security OAuth 實(shí)現(xiàn)OAuth 2.0 授權(quán)
- 基于Spring Security的Oauth2授權(quán)實(shí)現(xiàn)方法
- Spring Security OAuth2認(rèn)證授權(quán)示例詳解
- Spring Security 控制授權(quán)的方法
- Spring Security OAuth 自定義授權(quán)方式實(shí)現(xiàn)手機(jī)驗(yàn)證碼
- Spring?Security使用數(shù)據(jù)庫登錄認(rèn)證授權(quán)
- SpringBoot Security前后端分離登錄驗(yàn)證的實(shí)現(xiàn)
- 解析SpringSecurity自定義登錄驗(yàn)證成功與失敗的結(jié)果處理問題
- 詳解Spring Security中的HttpBasic登錄驗(yàn)證模式
- 淺析Spring Security登錄驗(yàn)證流程源碼
- 詳解使用Spring Security進(jìn)行自動(dòng)登錄驗(yàn)證
- SpringSecurity頁面授權(quán)與登錄驗(yàn)證實(shí)現(xiàn)(內(nèi)存取值與數(shù)據(jù)庫取值)
相關(guān)文章
SpringBoot接口如何對(duì)參數(shù)進(jìn)行校驗(yàn)
這篇文章主要介紹了SpringBoot接口如何對(duì)參數(shù)進(jìn)行校驗(yàn),在以SpringBoot開發(fā)Restful接口時(shí),?對(duì)于接口的查詢參數(shù)后臺(tái)也是要進(jìn)行校驗(yàn)的,同時(shí)還需要給出校驗(yàn)的返回信息放到上文我們統(tǒng)一封裝的結(jié)構(gòu)中2022-07-07
Java開發(fā)中的23種設(shè)計(jì)模式詳解(推薦)
本篇文章主要介紹了Java開發(fā)中的23種設(shè)計(jì)模式詳解,現(xiàn)在分享給大家,也給大家做個(gè)參考。感興趣的小伙伴們可以參考一下。 設(shè)計(jì)模式(Design Patterns)2016-11-11
Java實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
SpringBoot項(xiàng)目nohup啟動(dòng)運(yùn)行日志過大的解決方案
這篇文章主要介紹了SpringBoot項(xiàng)目nohup啟動(dòng)運(yùn)行日志過大的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Spring Boot集成Sorl搜索客戶端的實(shí)現(xiàn)代碼
本篇文章主要介紹了Spring Boot集成Sorl搜索客戶端的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Kotlin 語言中調(diào)用 JavaScript 方法實(shí)例詳解
這篇文章主要介紹了Kotlin 語言中調(diào)用 JavaScript 方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06

