JSD-8694 开源任务材料
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.6 KiB

package com.fr.plugin.llcassso.handler;
import com.fr.base.ServerConfig;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.plugin.llcassso.config.PluginSimpleConfig;
import com.fr.plugin.llcassso.utils.FRUtils;
import com.fr.plugin.llcassso.utils.HttpUtils;
import com.fr.plugin.llcassso.utils.ResponseUtils;
import com.fr.plugin.llcassso.utils.Utils;
import com.fr.plugin.transform.FunctionRecorder;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LoginHandler extends BaseHttpHandler {
public LoginHandler() {
}
@Override
public RequestMethod getMethod() {
return RequestMethod.GET;
}
@Override
public String getPath() {
return "/ssoLogin";
}
@Override
public boolean isPublic() {
return true;
}
@Override
public void handle(HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException {
String redirect = req.getParameter("redirect");
redirect = URLDecoder.decode(redirect);
String ticket = req.getParameter("ticket");
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
String url = FRUtils.getAllUrl(req);
int index = url.indexOf("decision");
FRUtils.FRLogInfo("url " + url);
//两层编码是为了解决中文模板名称的问题
String newRedirect = url.substring(0,index+8)+URLEncoder.encode(encodeCH(url.substring(index+8,url.length())),"UTF-8");
if(Utils.isNullStr(ticket)){
String ticketUrl = psc.getTicket()+"?service="+ newRedirect;
FRUtils.FRLogInfo("request ticket:"+ticketUrl);
try {
res.sendRedirect(ticketUrl);
} catch (IOException e) {
FRUtils.FRLogError("request ticket exception:"+e.getMessage());
}
return ;
}
url = URLDecoder.decode(url);
//将ticket截取掉
url = url.substring(0,url.indexOf(url.contains("&ticket") ? "&ticket" : "?ticket"));
FRUtils.FRLogInfo("validate service:"+url);
String newRedirect2 = url.substring(0,index+8)+URLEncoder.encode(encodeCH(url.substring(index+8,url.length())),"UTF-8");
String validateUrl = psc.getValidate() + "?ticket="+ticket + "&service=" + newRedirect2;
String result = HttpUtils.get(validateUrl,null,null);
if(Utils.isNullStr(result) || !result.contains("yes")){
ResponseUtils.failedResponse(res,"validate fail!");
return ;
}
String username = result.trim().replace("yes\n","");
String redirect2 = FRUtils.getAllUrl(req).split("redirect=")[1];
redirect2 = redirect2.substring(0,redirect2.indexOf(redirect2.contains("&ticket") ? "&ticket" : "?ticket"));
FRUtils.FRLogInfo("redirect2:"+redirect2);
FRUtils.login(req,res,username,encodeCH(redirect2));
}
private static String encodeCH(String url ) throws UnsupportedEncodingException {
Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url);
while(matcher.find()){
String chn = matcher.group();
url = url.replaceAll(chn,URLEncoder.encode(chn,"UTF-8"));
}
return url;
}
public static void main(String[] args) throws UnsupportedEncodingException {
}
}