微信公眾號提供了微信支付、微信優惠券、微信H5紅包、微信紅包封面等等促銷工具來幫助我們的應用拉新保活。但是這些福利要想正確地發放到用戶的手里就必須拿到用戶特定的(微信應用)微信標識openid
甚至是用戶的微信用戶信息。如果用戶在微信客戶端中訪問我們第三方網頁,公眾號可以通過微信網頁授權機制,來獲取用戶基本信息,進而實現業務邏輯。今天就結合Spring Security來實現一下微信公眾號網頁授權。
環境準備
在開始之前我們需要準備好微信網頁開發的環境。
微信公眾號服務號
請注意,一定是微信公眾號服務號,只有服務號才提供這樣的能力。像胖哥的這樣公眾號雖然也是認證過的公眾號,但是只能發發文章并不具備提供服務的能力。但是微信公眾平臺提供了沙盒功能來模擬服務號,可以降低開發難度,你可以到微信公眾號測試賬號頁面申請,申請成功后別忘了關注測試公眾號。
微信公眾號服務號只有企事業單位、政府機關才能開通。
內網穿透
因為微信服務器需要回調開發者提供的回調接口,為了能夠本地調試,內網穿透工具也是必須的。啟動內網穿透后,需要把內網穿透工具提供的虛擬域名配置到微信測試帳號的回調配置中
打開后只需要填寫域名,不要帶協議頭。例如回調是https://felord.cn/wechat/callback
,只能填寫成這樣:
然后我們就可以開發了。
OAuth2.0客戶端集成
基于 Spring Security 5.x
微信網頁授權的文檔在網頁授權,這里不再贅述。我們只聊聊如何結合Spring Security的事。微信網頁授權是通過OAuth2.0機制實現的,在用戶授權給公眾號后,公眾號可以獲取到一個網頁授權特有的接口調用憑證(網頁授權access_token
),通過網頁授權獲得的access_token
可以進行授權后接口調用,如獲取用戶的基本信息。
我們需要引入Spring Security提供的OAuth2.0相關的模塊:
1
2
3
4
5
6
7
8
|
<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
。
微信網頁授權流程
接著按照微信提供的流程來結合Spring Security。
獲取授權碼code
微信網頁授權使用的是OAuth2.0的授權碼模式。我們先來看如何獲取授權碼。
這是微信獲取code
的OAuth2.0端點模板,這不是一個純粹的OAuth2.0協議。微信做了一些參數上的變動。這里原生的client_id
被替換成了appid
,而且末尾還要加#wechat_redirect
。這無疑增加了集成的難度。
這里先放一放,我們目標轉向Spring Security的code
獲取流程。
Spring Security會提供一個模版鏈接:
1
|
{baseUrl}/oauth2/authorization/{registrationId} |
當使用該鏈接請求OAuth2.0客戶端時會被OAuth2AuthorizationRequestRedirectFilter
攔截。機制這里不講了,在我個人博客felord.cn
中的Spring Security 實戰干貨:客戶端OAuth2授權請求的入口
一文中有詳細闡述。
攔截之后會根據配置組裝獲取授權碼的請求URL,由于微信的不一樣所以我們針對性的定制,也就是改造OAuth2AuthorizationRequestRedirectFilter
中的OAuth2AuthorizationRequestResolver
。
自定義URL
因為Spring Security會根據模板鏈接去組裝一個鏈接而不是我們填參數就行了,所以需要我們對構建URL的處理器進行自定義。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/** * 兼容微信的oauth2 端點. * * @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請求URI * * @author n1 * @since 2021 /8/11 15:31 */ private static class WechatOAuth2RequestUriBuilderCustomizer { /** * 默認情況下Spring Security會生成授權鏈接: * {@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} * 缺少了微信協議要求的{@code #wechat_redirect},同時 {@code client_id}應該替換為{@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); } } } |
配置解析器
把上面個性化改造的邏輯配置到OAuth2AuthorizationRequestResolver
:
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * 用來從{@link javax.servlet.http.HttpServletRequest}中檢索Oauth2需要的參數并封裝成OAuth2請求對象{@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
,偽代碼:
1
2
3
|
httpSecurity.oauth2Login() // 定制化授權端點的參數封裝 .authorizationEndpoint().authorizationRequestResolver(authorizationRequestResolver) |
通過code換取網頁授權access_token
接下來第二步是用code
去換token
。
構建請求參數
這是微信網頁授權獲取access_token
的模板:
1
|
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
來指定;后半段參數需要我們針對微信進行定制。Spring Security中定制token-uri
的工具由OAuth2AuthorizationCodeGrantRequestEntityConverter
這個轉換器負責,這里需要來改造一下。
我們先拼接參數:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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
的請求對象RequestEntity
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@Override public RequestEntity<?> convert(OAuth2AuthorizationCodeGrantRequest authorizationCodeGrantRequest) { ClientRegistration clientRegistration = authorizationCodeGrantRequest.getClientRegistration(); HttpHeaders headers = getTokenRequestHeaders(clientRegistration); String tokenUri = clientRegistration.getProviderDetails().getTokenUri(); // 針對微信的定制 WECHAT_ID表示為微信公眾號專用的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返回解析
微信公眾號授權token-uri
的返回值雖然文檔說是個json
,可它喵的Content-Type
是text-plain
。如果是application/json
,Spring Security就直接接收了。你說微信坑不坑?我們只能再寫個適配來正確的反序列化微信接口的返回值。
Spring Security 中對token-uri
的返回值的解析轉換同樣由OAuth2AccessTokenResponseClient
中的OAuth2AccessTokenResponseHttpMessageConverter
負責。
首先增加Content-Type
為text-plain
的適配;其次因為Spring Security接收token
返回的對象要求必須顯式聲明tokenType
,而微信返回的響應體中沒有,我們一律指定為OAuth2AccessToken.TokenType.BEARER
即可兼容。代碼比較簡單就不放了,有興趣可以去看我給的DEMO。
配置到Spring Security
先配置好我們上面兩個步驟的請求客戶端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/** * 調用token-uri去請求授權服務器獲取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; } |
再把請求客戶端配置到HttpSecurity
:
1
2
3
|
// 獲取token端點配置 比如根據code 獲取 token httpSecurity.oauth2Login() .tokenEndpoint().accessTokenResponseClient(accessTokenResponseClient) |
根據token獲取用戶信息
微信公眾號網頁授權獲取用戶信息需要scope
包含snsapi_userinfo
。
Spring Security中定義了一個OAuth2.0獲取用戶信息的抽象接口:
1
2
3
4
5
6
|
@FunctionalInterface public interface OAuth2UserService<R extends OAuth2UserRequest, U extends OAuth2User> { U loadUser(R userRequest) throws OAuth2AuthenticationException; } |
所以我們針對性的實現即可,需要實現三個相關概念。
OAuth2UserRequest
OAuth2UserRequest
是請求user-info-uri
的入參實體,包含了三大塊屬性:
-
ClientRegistration
微信OAuth2.0客戶端配置 -
OAuth2AccessToken
從token-uri
獲取的access_token
的抽象實體 -
additionalParameters
一些token-uri
返回的額外參數,比如openid
就可以從這里面取得
根據微信獲取用戶信息的端點API這個能滿足需要,不過需要注意的是。如果使用的是 OAuth2.0 Client 就無法從additionalParameters
獲取openid
等額外參數。
OAuth2User
這個用來封裝微信用戶信息,細節看下面的注釋:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/** * 微信授權的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 或者其它你業務邏輯相關的用戶權限集 目前沒有什么用 return null ; } @Override public String getName() { // 用戶唯一標識比較合適,這個不能為空啊,如果你能保證unionid不為空,也是不錯的選擇。 return openid; } } |
注意: getName()
一定不能返回null
。
OAuth2UserService
參數OAuth2UserRequest
和返回值OAuth2User
都準備好了,就剩下去請求微信服務器了。借鑒請求token-uri
的實現,還是一個RestTemplate
調用,核心就這幾行:
1
2
3
4
5
6
7
8
9
10
11
|
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); // 構建 user-info-uri端點 URI userInfoEndpoint = UriComponentsBuilder.fromUriString(userInfoUri).queryParams(queryParams).build().toUri(); // 請求 return this .restOperations.exchange(userInfoEndpoint, HttpMethod.GET, null , OAUTH2_USER_OBJECT); |
配置到Spring Security
1
2
3
|
// 獲取用戶信息端點配置 根據accessToken獲取用戶基本信息 httpSecurity.oauth2Login() .userInfoEndpoint().userService(oAuth2UserService); |
這里補充一下,寫一個授權成功后跳轉的接口并配置為授權登錄成功后的跳轉的url。
1
2
|
// 默認跳轉到 / 如果沒有會 404 所以弄個了接口 httpSecurity.oauth2Login().defaultSuccessUrl( "/weixin/h5/redirect" ) |
在這個接口里可以通過@RegisteredOAuth2AuthorizedClient
和@AuthenticationPrincipal
分別拿到認證客戶端的信息和用戶信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@GetMapping ( "/h5/redirect" ) public void sendRedirect(HttpServletResponse response, @RegisteredOAuth2AuthorizedClient ( "wechat" ) OAuth2AuthorizedClient authorizedClient, @AuthenticationPrincipal WechatOAuth2User principal) throws IOException { //todo 你可以再這里模擬一些授權后的業務邏輯 比如用戶靜默注冊 等等 // 當前認證的客戶端 token 不要暴露給前臺 OAuth2AccessToken accessToken = authorizedClient.getAccessToken(); System.out.println( "accessToken = " + accessToken); // 當前用戶的userinfo System.out.println( "principal = " + principal); response.sendRedirect( "https://felord.cn" ); } |
到此微信公眾號授權就集成到Spring Security中了。
相關配置
application.yaml
相關的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
spring: security: oauth2: client: registration: wechat: # 可以去試一下沙箱 # 公眾號服務號 appid client-id: wxdf9033184b2xxx38e7f # 公眾號服務號 secret client-secret: bf1306baaa0dxxxxxxb15eb02d68df5 # oauth2 login 用 '{baseUrl}/login/oauth2/code/{registrationId}' 會自動解析 # oauth2 client 寫你業務的鏈接即可 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 |
到此這篇關于Spring Security中實現微信網頁授權的文章就介紹到這了,更多相關Spring Security微信網頁授權內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/felordcn/p/15143384.html