Enterprise code simple oau2verification interface instance (using SpringMVC non-annotation method), oauth2springmvc
Enterprise code simple oau2verification interface instance (using SpringMVC non-annotation method)
Currently, enterprise numbers only provide application authorization scopes with the scope "snsapi_base", that is, they do not jump to the page for user authorization.
I have previously written examples for verifying and Obtaining user information using annotations. You are not very familiar with them, and there are many problems. Now I have attached a simple example for Obtaining user information!
Enterprise ID oau2verification interface instance (using SpringMVC)
Note the following for OAuth2.0 Verification:
1. The redirect_uri parameter is the authorized callback address, that is, this address can be accessed from the Internet. Therefore, if you use a local ing server, change it to an accessible Internet address!
2. configure a trusted domain name. A trusted domain name is part of redirect_uri in 1. http is not required. It supports second-level domain names and IP addresses!
3. The code parameter is included in the URL of the callback to your address. The value of the code parameter is different each time and is valid for 5 minutes.
4. when using the code parameter to obtain user information, make sure that each code parameter is new, we can see that the code obtained each time is new, but the code passed for example has been used before. So proceed with caution!
Code is attached here:
SimpleOAuth2Controller:
<Span style = "font-family: FangSong_GB2312; font-size: 14px;"> package org. oms. qiye. web; import java. io. unsupportedEncodingException; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpSession; import org. oms. qiye. pojo. accessToken; import org. oms. qiye. util. constants; import org. oms. qiye. util. qiYeUtil; import org. oms. qiye. util. result; import org. springframework. stereotype. controller; impo Rt org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestParam;/*** implements oau2verification, do not use annotation and interceptor * @ author Sunlight **/@ Controllerpublic class SimpleOAuth2Controller {/*** splice webpage authorization link * Here you can also use a page Link instead of * @ return */@ requestMapping (value = {"/oauth2wx. do "}) public String Oauth2API (HttpServletRequest request) {// obtain the project domain name String reqUrl = request. getLocalAddr (); // Concatenate the callback address String backUrl = "http: //" + reqUrl + "/oauth2me. do "; String redirect_uri =" "; try {redirect_uri = java.net. URLEncoder. encode (backUrl, "UTF-8");} catch (UnsupportedEncodingException e) {e. printStackTrace ();} String oauth2Url = "https://open.weixin.qq.com/connect/oauth2/authorize? Appid = "+ Constants. CORPID + "& redirect_uri =" + redirect_uri + "& response_type = code & scope = snsapi_base & state = sunlight # wechat_redirect"; return "redirect:" + oauth2Url ;} /*** authorize callback request processing ** @ return */@ RequestMapping (value = {"/oauth2me. do "}) public String oAuth2Url (HttpServletRequest request, @ RequestParam String code) {AccessToken accessToken = QiYeUtil. getAccessToken (Constants. CORPID, Constants. SECRET); H TtpSession session = request. getSession (); if (accessToken! = Null & accessToken. getToken ()! = Null) {String Userid = getMemberGuidByCode (accessToken. getToken (), code, Constants. AGENTID); if (Userid! = Null) {session. setAttribute ("UserId", Userid) ;}// simple processing, stored in session return "user ";} /*** call the interface to obtain user information ** @ param token * @ param code * @ param agentId * @ return * @ throws SQLException * @ throws RemoteException */public String getMemberGuidByCode (String token, string code, int agentId) {System. out. println ("code =" + code + "\ ntoken =" + token + "\ nagentid =" + agentId); Result <String> result = QiYeUtil. OAuth2GetUserByCode (token, code, agentId); System. out. println ("result =" + result); if (result. getErrcode () = "0") {if (result. getObj ()! = Null) {// you can use the Userid authorized to pay back the code to query the data in your local server. return result. getObj () ;}} return ";}}</span>
Run:
Server debugging diagram:
Mobile phone result:
Please indicate the source for reprinting, so as not to be miserable!
Technical Exchange please join QQ group: click the link to join the group [Enterprise Development Exchange]: http://jq.qq.com /? _ Wv = 1027 & k = RgbtOX
QQ: 89714226