Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.alogic.auth.service.GetTokenInfo Maven / Gradle / Ivy
package com.alogic.auth.service;
import java.util.HashMap;
import java.util.Map;
import com.logicbus.backend.message.YamlMessage;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.alogic.auth.Principal;
import com.alogic.auth.PrincipalManager;
import com.alogic.auth.SessionManagerFactory;
import com.anysoft.util.JsonTools;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.XmlTools;
import com.logicbus.backend.AbstractServant;
import com.logicbus.backend.Context;
import com.logicbus.backend.message.JsonMessage;
import com.logicbus.backend.message.XMLMessage;
import com.logicbus.models.servant.ServiceDescription;
/**
* 获取指定Token的信息
*
* @author yyduan
* @since 1.6.10.10
*
* @version 1.6.11.7 [20180107 duanyy]
* - 优化Session管理
*
* @version 1.6.11.20 [20180223 duanyy]
* - app参数id改为$provider
*
* @version 1.6.11.22 [duanyy 20180314]
* - ip绑定功能可配置
*
* @version 1.6.12.27 [20190403 duanyy]
* - 增加对yaml的支持
*/
public class GetTokenInfo extends AbstractServant{
protected String dftApp = "${server.app}";
protected String arguToken = "token";
protected String arguFromIp = "fromIp";
protected String arguCallback = "callback";
protected String arguApp = "$provider";
protected boolean ipBinded = false;
@Override
protected void onDestroy() {
}
@Override
protected void onCreate(ServiceDescription sd) {
Properties p = sd.getProperties();
dftApp = PropertiesConstants.getString(p, "dftApp", dftApp);
arguToken = PropertiesConstants.getString(p, "auth.para.token", arguToken);
arguFromIp = PropertiesConstants.getString(p, "auth.para.fromIp", arguFromIp);
arguCallback = PropertiesConstants.getString(p, "auth.para.callback", arguCallback);
arguApp = PropertiesConstants.getString(p, "auth.para.app", arguApp);
ipBinded = PropertiesConstants.getBoolean(p, "auth.ip.binded", ipBinded);
}
@Override
protected int onJson(Context ctx){
JsonMessage msg = (JsonMessage)ctx.asMessage(JsonMessage.class);
String token = getArgument(arguToken, ctx);
String fromIp = getArgument(arguFromIp,ctx);
String callback = getArgument(arguCallback,"",ctx);
String app = getArgument(arguApp,dftApp,ctx);
Map data = new HashMap();
PrincipalManager sm = (PrincipalManager)SessionManagerFactory.getDefault();
Principal principal = sm.getPrincipal(app, token,callback);
if (principal == null){
JsonTools.setString(data,"isLoggedIn","false");
}else{
if (!ipBinded || fromIp.equals(principal.getLoginIp())){
//token必须绑定ip
JsonTools.setString(data,"isLoggedIn","true");
principal.report(data);
}else{
JsonTools.setString(data,"isLoggedIn","false");
}
}
msg.getRoot().put("data", data);
return 0;
}
@Override
protected int onYaml(Context ctx){
YamlMessage msg = (YamlMessage)ctx.asMessage(YamlMessage.class);
String token = getArgument(arguToken, ctx);
String fromIp = getArgument(arguFromIp,ctx);
String callback = getArgument(arguCallback,"",ctx);
String app = getArgument(arguApp,dftApp,ctx);
Map data = new HashMap();
PrincipalManager sm = (PrincipalManager)SessionManagerFactory.getDefault();
Principal principal = sm.getPrincipal(app, token,callback);
if (principal == null){
JsonTools.setString(data,"isLoggedIn","false");
}else{
if (!ipBinded || fromIp.equals(principal.getLoginIp())){
//token必须绑定ip
JsonTools.setString(data,"isLoggedIn","true");
principal.report(data);
}else{
JsonTools.setString(data,"isLoggedIn","false");
}
}
msg.getRoot().put("data", data);
return 0;
}
@Override
protected int onXml(Context ctx){
XMLMessage msg = (XMLMessage)ctx.asMessage(XMLMessage.class);
String token = getArgument(arguToken, ctx);
String fromIp = getArgument(arguFromIp,ctx);
String app = getArgument("$app",dftApp,ctx);
String callback = getArgument(arguCallback,"",ctx);
Document doc = msg.getDocument();
Element data = doc.createElement("data");
PrincipalManager sm = (PrincipalManager)SessionManagerFactory.getDefault();
Principal principal = sm.getPrincipal(app, token,callback);
if (principal == null){
XmlTools.setString(data,"isLoggedIn","false");
}else{
if (fromIp.equals(principal.getLoginIp())){
//token必须绑定ip
XmlTools.setString(data,"isLoggedIn","true");
principal.report(data);
}else{
XmlTools.setString(data,"isLoggedIn","false");
}
}
msg.getRoot().appendChild(data);
return 0;
}
}