mirror of
https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
synced 2025-11-02 11:16:19 +08:00
oauth2 v2 update
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
package love.sola.netsupport.auth;
|
||||
|
||||
import love.sola.netsupport.session.WechatSession;
|
||||
import love.sola.netsupport.session.WxSession;
|
||||
import love.sola.netsupport.util.Checker;
|
||||
import love.sola.netsupport.wechat.WechatSession;
|
||||
import love.sola.netsupport.wechat.WxMpServlet;
|
||||
import me.chanjar.weixin.common.session.WxSession;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.ServletException;
|
||||
@@ -15,6 +14,8 @@ import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
@@ -22,9 +23,20 @@ import java.io.IOException;
|
||||
* Don't modify this source without my agreement
|
||||
* ***********************************************
|
||||
*/
|
||||
@WebServlet(name = "OAuth2", urlPatterns = "/oauth2/callback", loadOnStartup = 21)
|
||||
@WebServlet(name = "OAuth2", urlPatterns = "/oauth2/callback", loadOnStartup = 21, asyncSupported = true)
|
||||
public class OAuth2 extends HttpServlet {
|
||||
|
||||
private static Map<String, OAuth2Handler> oAuth2HandlerMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* for {@link love.sola.netsupport.wechat.WxMpServlet#registerCommands}
|
||||
* @param state the state key from open platform callback.
|
||||
* @param handler handler
|
||||
*/
|
||||
public static void registerOAuth2Handler(String state, OAuth2Handler handler) {
|
||||
oAuth2HandlerMap.put(state, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
AsyncContext acxt = req.startAsync();
|
||||
@@ -34,13 +46,19 @@ public class OAuth2 extends HttpServlet {
|
||||
resp.sendError(HttpServletResponse.SC_FORBIDDEN);
|
||||
return;
|
||||
}
|
||||
OAuth2Handler handler = oAuth2HandlerMap.get(state);
|
||||
if (handler == null) {
|
||||
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
|
||||
return;
|
||||
}
|
||||
acxt.start(() -> {
|
||||
try {
|
||||
WxMpService wxMpService = WxMpServlet.instance.wxMpService;
|
||||
WxMpOAuth2AccessToken token = wxMpService.oauth2getAccessToken(code);
|
||||
WxMpUser wxUser = wxMpService.oauth2getUserInfo(token, "zh_CN");
|
||||
String sid = WechatSession.genId();
|
||||
WxSession session = WechatSession.get(sid, true);
|
||||
String wechat = token.getOpenId();
|
||||
WxSession session = WechatSession.create();
|
||||
handler.onOAuth2(acxt, (HttpServletResponse) acxt.getResponse(), wechat, session);
|
||||
acxt.complete();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
18
src/main/java/love/sola/netsupport/auth/OAuth2Handler.java
Normal file
18
src/main/java/love/sola/netsupport/auth/OAuth2Handler.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package love.sola.netsupport.auth;
|
||||
|
||||
import love.sola.netsupport.session.WxSession;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
* Created by Sola on 2016/3/26.
|
||||
* Don't modify this source without my agreement
|
||||
* ***********************************************
|
||||
*/
|
||||
public interface OAuth2Handler {
|
||||
|
||||
void onOAuth2(AsyncContext acxt, HttpServletResponse resp, String user, WxSession session);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user