package com.fr.plugin; import com.fr.decision.fun.impl.BaseHttpHandler; import com.fr.decision.webservice.v10.login.LoginService; import com.fr.third.org.apache.commons.codec.digest.DigestUtils; import com.fr.third.org.apache.commons.lang3.StringUtils; import com.fr.third.springframework.web.bind.annotation.RequestMethod; import com.fr.web.utils.WebUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; public class GoAuthApi extends BaseHttpHandler { @Override public RequestMethod getMethod() { return null; } @Override public String getPath() { return "/goAuth"; } @Override public boolean isPublic() { return true; } @Override public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { if (isLogin(httpServletRequest)) { sendRedirect(httpServletResponse, HttpUtils.getDefaultUrl(httpServletRequest)); return; } else { String valAddr = Oauth2Config.getInstance().getValAddr(); String appId = Oauth2Config.getInstance().getAppId(); String frUrl = Oauth2Config.getInstance().getFrUrl(); String redirectUrl = String.format("%s/url/iam/authCallBack", frUrl); redirectUrl = URLEncoder.encode(redirectUrl, "utf-8"); String goUrl = String.format("%s/oauth2/rest/authz?response_type=code&client_id=%s&domain=IdmDomain&state=xyz&scope=IdmResServer.UserProfile.me openid email phone profile&redirect_uri=%s", valAddr, appId, redirectUrl); sendRedirect(httpServletResponse, goUrl); } } private void sendRedirect(HttpServletResponse res, String url) throws IOException { Map params = new HashMap<>(); params.put("callBack", url); WebUtils.writeOutTemplate("com/fr/plugin/redirect.html", res, params); } private boolean isLogin(HttpServletRequest req) { return LoginService.getInstance().isLogged(req); } private String md5(String str) { return DigestUtils.md5Hex(str); } }