commit
1480d51fd8
16 changed files with 2375 additions and 0 deletions
@ -0,0 +1,6 @@
|
||||
# open-JSD-9915 |
||||
|
||||
JSD-9915 单点集成+用户同步\ |
||||
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系【pioneer】处理。 |
Binary file not shown.
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin> |
||||
<id>com.eco.plugin.xx.xytsso</id> |
||||
<name><![CDATA[单点登录及用户同步]]></name> |
||||
<active>yes</active> |
||||
<version>1.0.17</version> |
||||
<env-version>10.0</env-version> |
||||
<jartime>2018-07-31</jartime> |
||||
<vendor>fr.open</vendor> |
||||
<description><![CDATA[单点登录及用户同步]]></description> |
||||
<change-notes><![CDATA[ |
||||
]]></change-notes> |
||||
<main-package>com.eco.plugin.xx.xytsso</main-package> |
||||
<lifecycle-monitor class="com.eco.plugin.xx.xytsso.config.InitializeMonitor"/> |
||||
|
||||
<extra-decision> |
||||
<GlobalRequestFilterProvider class="com.eco.plugin.xx.xytsso.filter.SSOFilter"/> |
||||
</extra-decision> |
||||
<extra-core> |
||||
<FunctionDefineProvider class="com.eco.plugin.xx.xytsso.function.AuthFunction" name="syncData" description="同步数据"/> |
||||
<DBAccessProvider class="com.eco.plugin.xx.xytsso.db.controller.DBController"/> |
||||
</extra-core> |
||||
<function-recorder class="com.eco.plugin.xx.xytsso.config.PluginSimpleConfig"/> |
||||
|
||||
</plugin> |
@ -0,0 +1,21 @@
|
||||
package com.eco.plugin.xx.xytsso.config; |
||||
|
||||
import com.fr.plugin.context.PluginContext; |
||||
import com.fr.plugin.observer.inner.AbstractPluginLifecycleMonitor; |
||||
|
||||
/** |
||||
* @author xx |
||||
* @version 10.0 |
||||
* Created by xx on 2021-12-03 |
||||
*/ |
||||
public class InitializeMonitor extends AbstractPluginLifecycleMonitor { |
||||
@Override |
||||
public void afterRun(PluginContext pluginContext) { |
||||
PluginSimpleConfig.getInstance(); |
||||
} |
||||
|
||||
@Override |
||||
public void beforeStop(PluginContext pluginContext) { |
||||
|
||||
} |
||||
} |
@ -0,0 +1,97 @@
|
||||
package com.eco.plugin.xx.xytsso.config; |
||||
|
||||
import com.fr.config.*; |
||||
import com.fr.config.holder.Conf; |
||||
import com.fr.config.holder.factory.Holders; |
||||
import com.fr.intelli.record.Focus; |
||||
import com.fr.intelli.record.Original; |
||||
import com.fr.record.analyzer.EnableMetrics; |
||||
|
||||
@Visualization(category = "单点登录及用户同步配置") |
||||
@EnableMetrics |
||||
public class PluginSimpleConfig extends DefaultConfiguration { |
||||
|
||||
private static volatile PluginSimpleConfig config = null; |
||||
|
||||
@Focus(id="com.eco.plugin.xx.xytsso.config", text = "单点登录及用户同步配置", source = Original.PLUGIN) |
||||
public static PluginSimpleConfig getInstance() { |
||||
if (config == null) { |
||||
config = ConfigContext.getConfigInstance(PluginSimpleConfig.class); |
||||
} |
||||
return config; |
||||
} |
||||
|
||||
@Identifier(value = "apptoken", name = "AppToken", description = "AppToken", status = Status.SHOW) |
||||
private Conf<String> apptoken = Holders.simple(""); |
||||
|
||||
@Identifier(value = "secret", name = "私钥", description = "私钥", status = Status.SHOW) |
||||
private Conf<String> secret = Holders.simple(""); |
||||
|
||||
@Identifier(value = "orgdomainurl", name = "获取组织域列表接口", description = "获取组织域列表接口", status = Status.SHOW) |
||||
private Conf<String> orgdomainurl = Holders.simple(""); |
||||
|
||||
@Identifier(value = "orgversionurl", name = "获取组织域版本接口", description = "获取组织域版本接口", status = Status.SHOW) |
||||
private Conf<String> orgversionurl = Holders.simple(""); |
||||
|
||||
@Identifier(value = "depturl", name = "获取部门列表接口", description = "获取部门列表接口", status = Status.SHOW) |
||||
private Conf<String> depturl = Holders.simple(""); |
||||
|
||||
@Identifier(value = "deptuserurl", name = "获取部门人员接口", description = "获取部门人员接口", status = Status.SHOW) |
||||
private Conf<String> deptuserurl = Holders.simple(""); |
||||
|
||||
public String getApptoken() { |
||||
return apptoken.get(); |
||||
} |
||||
|
||||
public void setApptoken(String url) { |
||||
this.apptoken.set(url); |
||||
} |
||||
|
||||
public String getSecret() { |
||||
return secret.get(); |
||||
} |
||||
|
||||
public void setSecret(String url) { |
||||
this.secret.set(url); |
||||
} |
||||
|
||||
public String getOrgdomainurl() { |
||||
return orgdomainurl.get(); |
||||
} |
||||
|
||||
public void setOrgdomainurl(String url) { |
||||
this.orgdomainurl.set(url); |
||||
} |
||||
|
||||
public String getDepturl() { |
||||
return depturl.get(); |
||||
} |
||||
|
||||
public void setDepturl(String url) { |
||||
this.depturl.set(url); |
||||
} |
||||
|
||||
public String getOrgversionurl() { |
||||
return orgversionurl.get(); |
||||
} |
||||
|
||||
public void setOrgversionurl(String url) { |
||||
this.orgversionurl.set(url); |
||||
} |
||||
|
||||
public String getDeptuserurl() { |
||||
return deptuserurl.get(); |
||||
} |
||||
|
||||
public void setDeptuserurl(String url) { |
||||
this.deptuserurl.set(url); |
||||
} |
||||
|
||||
@Override |
||||
public Object clone() throws CloneNotSupportedException { |
||||
PluginSimpleConfig cloned = (PluginSimpleConfig) super.clone(); |
||||
|
||||
return cloned; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,216 @@
|
||||
package com.eco.plugin.xx.xytsso.db.bean; |
||||
|
||||
import com.fr.stable.db.entity.BaseEntity; |
||||
import com.fr.stable.db.entity.TableAssociation; |
||||
import com.fr.third.javax.persistence.Column; |
||||
import com.fr.third.javax.persistence.Entity; |
||||
import com.fr.third.javax.persistence.Table; |
||||
|
||||
/** |
||||
* @author xx |
||||
* @version 10.0 |
||||
* Created by xx on 2021-07-29 |
||||
**/ |
||||
@Entity |
||||
@Table(name = "plugin_xx_entity_org") |
||||
@TableAssociation(associated = true) |
||||
public class DBEntity extends BaseEntity { |
||||
|
||||
@Column(name = "code") |
||||
private String code = null; |
||||
|
||||
@Column(name = "name") |
||||
private String name = null; |
||||
|
||||
@Column(name = "node_id") |
||||
private String node_id = null; |
||||
|
||||
@Column(name = "parent_id") |
||||
private String parent_id = null; |
||||
|
||||
@Column(name = "version") |
||||
private String version = null; |
||||
|
||||
@Column(name = "is_virtual") |
||||
private String is_virtual = null; |
||||
|
||||
@Column(name = "is_visible") |
||||
private String is_visible = null; |
||||
|
||||
@Column(name = "jianpin") |
||||
private String jianpin = null; |
||||
|
||||
@Column(name = "mark") |
||||
private String mark = null; |
||||
|
||||
@Column(name = "create_time") |
||||
private String create_time = null; |
||||
|
||||
@Column(name = "create_user") |
||||
private String create_user = null; |
||||
|
||||
@Column(name = "modify_user") |
||||
private String modify_user = null; |
||||
|
||||
@Column(name = "ordinal") |
||||
private String ordinal = null; |
||||
|
||||
@Column(name = "path_id") |
||||
private String path_id = null; |
||||
|
||||
@Column(name = "path_name") |
||||
private String path_name = null; |
||||
|
||||
@Column(name = "quanpin") |
||||
private String quanpin = null; |
||||
|
||||
@Column(name = "enabled") |
||||
private String enabled = null; |
||||
|
||||
@Column(name = "extends_properties") |
||||
private String extends_properties = null; |
||||
|
||||
public String getCode() { |
||||
return code; |
||||
} |
||||
|
||||
public void setCode(String code) { |
||||
this.code = code; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public String getNode_id() { |
||||
return node_id; |
||||
} |
||||
|
||||
public void setNode_id(String node_id) { |
||||
this.node_id = node_id; |
||||
} |
||||
|
||||
public String getParent_id() { |
||||
return parent_id; |
||||
} |
||||
|
||||
public void setParent_id(String parent_id) { |
||||
this.parent_id = parent_id; |
||||
} |
||||
|
||||
public String getVersion() { |
||||
return version; |
||||
} |
||||
|
||||
public void setVersion(String version) { |
||||
this.version = version; |
||||
} |
||||
|
||||
public String getIs_virtual() { |
||||
return is_virtual; |
||||
} |
||||
|
||||
public void setIs_virtual(String is_virtual) { |
||||
this.is_virtual = is_virtual; |
||||
} |
||||
|
||||
public String getIs_visible() { |
||||
return is_visible; |
||||
} |
||||
|
||||
public void setIs_visible(String is_visible) { |
||||
this.is_visible = is_visible; |
||||
} |
||||
|
||||
public String getJianpin() { |
||||
return jianpin; |
||||
} |
||||
|
||||
public void setJianpin(String jianpin) { |
||||
this.jianpin = jianpin; |
||||
} |
||||
|
||||
public String getMark() { |
||||
return mark; |
||||
} |
||||
|
||||
public void setMark(String mark) { |
||||
this.mark = mark; |
||||
} |
||||
|
||||
public String getCreate_time() { |
||||
return create_time; |
||||
} |
||||
|
||||
public void setCreate_time(String create_time) { |
||||
this.create_time = create_time; |
||||
} |
||||
|
||||
public String getCreate_user() { |
||||
return create_user; |
||||
} |
||||
|
||||
public void setCreate_user(String create_user) { |
||||
this.create_user = create_user; |
||||
} |
||||
|
||||
public String getModify_user() { |
||||
return modify_user; |
||||
} |
||||
|
||||
public void setModify_user(String modify_user) { |
||||
this.modify_user = modify_user; |
||||
} |
||||
|
||||
public String getOrdinal() { |
||||
return ordinal; |
||||
} |
||||
|
||||
public void setOrdinal(String ordinal) { |
||||
this.ordinal = ordinal; |
||||
} |
||||
|
||||
public String getPath_id() { |
||||
return path_id; |
||||
} |
||||
|
||||
public void setPath_id(String path_id) { |
||||
this.path_id = path_id; |
||||
} |
||||
|
||||
public String getPath_name() { |
||||
return path_name; |
||||
} |
||||
|
||||
public void setPath_name(String path_name) { |
||||
this.path_name = path_name; |
||||
} |
||||
|
||||
public String getQuanpin() { |
||||
return quanpin; |
||||
} |
||||
|
||||
public void setQuanpin(String quanpin) { |
||||
this.quanpin = quanpin; |
||||
} |
||||
|
||||
public String getEnabled() { |
||||
return enabled; |
||||
} |
||||
|
||||
public void setEnabled(String enabled) { |
||||
this.enabled = enabled; |
||||
} |
||||
|
||||
public String getExtends_properties() { |
||||
return extends_properties; |
||||
} |
||||
|
||||
public void setExtends_properties(String extends_properties) { |
||||
this.extends_properties = extends_properties; |
||||
} |
||||
} |
@ -0,0 +1,227 @@
|
||||
package com.eco.plugin.xx.xytsso.db.bean; |
||||
|
||||
import com.fr.stable.db.entity.BaseEntity; |
||||
import com.fr.stable.db.entity.TableAssociation; |
||||
import com.fr.third.javax.persistence.Column; |
||||
import com.fr.third.javax.persistence.Entity; |
||||
import com.fr.third.javax.persistence.Table; |
||||
|
||||
/** |
||||
* @author xx |
||||
* @version 10.0 |
||||
* Created by xx on 2021-07-29 |
||||
**/ |
||||
@Entity |
||||
@Table(name = "plugin_xx_entity_user") |
||||
@TableAssociation(associated = true) |
||||
public class UserEntity extends BaseEntity { |
||||
|
||||
@Column(name = "user_id") |
||||
private String user_id = null; |
||||
|
||||
@Column(name = "user_name") |
||||
private String user_name = null; |
||||
|
||||
@Column(name = "birthday") |
||||
private String birthday = null; |
||||
|
||||
@Column(name = "code") |
||||
private String code = null; |
||||
|
||||
@Column(name = "employeeid") |
||||
private String employeeid = null; |
||||
|
||||
@Column(name = "enabled") |
||||
private String enabled = null; |
||||
|
||||
@Column(name = "idcard") |
||||
private String idcard = null; |
||||
|
||||
@Column(name = "name") |
||||
private String name = null; |
||||
|
||||
@Column(name = "quanpin") |
||||
private String quanpin = null; |
||||
|
||||
@Column(name = "jianpin") |
||||
private String jianpin = null; |
||||
|
||||
@Column(name = "office") |
||||
private String office = null; |
||||
|
||||
@Column(name = "owner_place") |
||||
private String owner_place = null; |
||||
|
||||
@Column(name = "regist_email") |
||||
private String regist_email = null; |
||||
|
||||
@Column(name = "regist_mobile") |
||||
private String regist_mobile = null; |
||||
|
||||
@Column(name = "telephone") |
||||
private String telephone = null; |
||||
|
||||
@Column(name = "user_scope") |
||||
private String user_scope = null; |
||||
|
||||
@Column(name = "create_time") |
||||
private String create_time = null; |
||||
|
||||
@Column(name = "extends_properties") |
||||
private String extends_properties = null; |
||||
|
||||
@Column(name = "orgid") |
||||
private String orgid = null; |
||||
|
||||
public String getOrgid() { |
||||
return orgid; |
||||
} |
||||
|
||||
public void setOrgid(String orgid) { |
||||
this.orgid = orgid; |
||||
} |
||||
|
||||
public String getUser_id() { |
||||
return user_id; |
||||
} |
||||
|
||||
public void setUser_id(String user_id) { |
||||
this.user_id = user_id; |
||||
} |
||||
|
||||
public String getUser_name() { |
||||
return user_name; |
||||
} |
||||
|
||||
public void setUser_name(String user_name) { |
||||
this.user_name = user_name; |
||||
} |
||||
|
||||
public String getBirthday() { |
||||
return birthday; |
||||
} |
||||
|
||||
public void setBirthday(String birthday) { |
||||
this.birthday = birthday; |
||||
} |
||||
|
||||
public String getCode() { |
||||
return code; |
||||
} |
||||
|
||||
public void setCode(String code) { |
||||
this.code = code; |
||||
} |
||||
|
||||
public String getEmployeeid() { |
||||
return employeeid; |
||||
} |
||||
|
||||
public void setEmployeeid(String employeeid) { |
||||
this.employeeid = employeeid; |
||||
} |
||||
|
||||
public String getEnabled() { |
||||
return enabled; |
||||
} |
||||
|
||||
public void setEnabled(String enabled) { |
||||
this.enabled = enabled; |
||||
} |
||||
|
||||
public String getIdcard() { |
||||
return idcard; |
||||
} |
||||
|
||||
public void setIdcard(String idcard) { |
||||
this.idcard = idcard; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public String getQuanpin() { |
||||
return quanpin; |
||||
} |
||||
|
||||
public void setQuanpin(String quanpin) { |
||||
this.quanpin = quanpin; |
||||
} |
||||
|
||||
public String getJianpin() { |
||||
return jianpin; |
||||
} |
||||
|
||||
public void setJianpin(String jianpin) { |
||||
this.jianpin = jianpin; |
||||
} |
||||
|
||||
public String getOffice() { |
||||
return office; |
||||
} |
||||
|
||||
public void setOffice(String office) { |
||||
this.office = office; |
||||
} |
||||
|
||||
public String getOwner_place() { |
||||
return owner_place; |
||||
} |
||||
|
||||
public void setOwner_place(String owner_place) { |
||||
this.owner_place = owner_place; |
||||
} |
||||
|
||||
public String getRegist_email() { |
||||
return regist_email; |
||||
} |
||||
|
||||
public void setRegist_email(String regist_email) { |
||||
this.regist_email = regist_email; |
||||
} |
||||
|
||||
public String getRegist_mobile() { |
||||
return regist_mobile; |
||||
} |
||||
|
||||
public void setRegist_mobile(String regist_mobile) { |
||||
this.regist_mobile = regist_mobile; |
||||
} |
||||
|
||||
public String getTelephone() { |
||||
return telephone; |
||||
} |
||||
|
||||
public void setTelephone(String telephone) { |
||||
this.telephone = telephone; |
||||
} |
||||
|
||||
public String getUser_scope() { |
||||
return user_scope; |
||||
} |
||||
|
||||
public void setUser_scope(String user_scope) { |
||||
this.user_scope = user_scope; |
||||
} |
||||
|
||||
public String getCreate_time() { |
||||
return create_time; |
||||
} |
||||
|
||||
public void setCreate_time(String create_time) { |
||||
this.create_time = create_time; |
||||
} |
||||
|
||||
public String getExtends_properties() { |
||||
return extends_properties; |
||||
} |
||||
|
||||
public void setExtends_properties(String extends_properties) { |
||||
this.extends_properties = extends_properties; |
||||
} |
||||
} |
@ -0,0 +1,194 @@
|
||||
package com.eco.plugin.xx.xytsso.db.controller; |
||||
|
||||
import com.eco.plugin.xx.xytsso.db.bean.DBEntity; |
||||
import com.eco.plugin.xx.xytsso.db.bean.UserEntity; |
||||
import com.eco.plugin.xx.xytsso.db.dao.OrgDao; |
||||
import com.eco.plugin.xx.xytsso.db.dao.UserDao; |
||||
import com.eco.plugin.xx.xytsso.utils.FRUtils; |
||||
import com.fr.db.fun.impl.AbstractDBAccessProvider; |
||||
import com.fr.plugin.transform.FunctionRecorder; |
||||
import com.fr.record.analyzer.EnableMetrics; |
||||
import com.fr.stable.db.accessor.DBAccessor; |
||||
import com.fr.stable.db.action.DBAction; |
||||
import com.fr.stable.db.dao.DAOContext; |
||||
import com.fr.stable.db.dao.DAOProvider; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author xx |
||||
* @version 10.0 |
||||
* Created by xx on 2021-07-29 |
||||
**/ |
||||
@EnableMetrics |
||||
@FunctionRecorder |
||||
public class DBController extends AbstractDBAccessProvider { |
||||
|
||||
private static DBAccessor accessor; |
||||
|
||||
public static DBAccessor getAccessor() { |
||||
return accessor; |
||||
} |
||||
|
||||
@Override |
||||
public DAOProvider[] registerDAO() { |
||||
return new DAOProvider[]{ |
||||
OrgDao.DAO, UserDao.DAO |
||||
}; |
||||
} |
||||
|
||||
@Override |
||||
public void onDBAvailable(DBAccessor accessor) { |
||||
DBController.accessor = accessor; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 批量操作 |
||||
* @param addOrUpdate |
||||
* @param delete |
||||
* @return |
||||
*/ |
||||
public static boolean batch(List<DBEntity> addOrUpdate, List<DBEntity> delete){ |
||||
//新增或者删除
|
||||
if(addOrUpdate != null && addOrUpdate.size() > 0){ |
||||
for(final DBEntity dbe : addOrUpdate){ |
||||
|
||||
try{ |
||||
accessor.runDMLAction(new DBAction<Boolean>() { |
||||
@Override |
||||
public Boolean run(DAOContext context) throws Exception { |
||||
DBEntity ae =context.getDAO(OrgDao.class).getById(dbe.getId()); |
||||
if(ae != null ){ |
||||
context.getDAO(OrgDao.class).update(dbe); |
||||
}else{ |
||||
context.getDAO(OrgDao.class).add(dbe); |
||||
} |
||||
return true; |
||||
} |
||||
}); |
||||
}catch(Throwable e){ |
||||
FRUtils.FRLogError("batch addOrUpdate exception ->"+e.getMessage() + dbe.toString()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
if(delete != null && delete.size() > 0){ |
||||
for(final DBEntity dbe : delete){ |
||||
|
||||
try{ |
||||
accessor.runDMLAction(new DBAction<Boolean>() { |
||||
@Override |
||||
public Boolean run(DAOContext context) throws Exception { |
||||
context.getDAO(OrgDao.class).remove(dbe.getId()); |
||||
return true; |
||||
} |
||||
}); |
||||
}catch(Throwable e){ |
||||
FRUtils.FRLogError("batch delete exception ->"+e.getMessage() + dbe.toString()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
|
||||
|
||||
/** |
||||
* 根据username获取信息 |
||||
* @param |
||||
* @return |
||||
*/ |
||||
public static List<DBEntity> getByAllOrg(){ |
||||
try{ |
||||
return accessor.runQueryAction(new DBAction<List<DBEntity>>() { |
||||
@Override |
||||
public List<DBEntity> run(DAOContext context) throws Exception { |
||||
List<DBEntity> result = context.getDAO(OrgDao.class).getAllOrg(); |
||||
|
||||
return result == null ? new ArrayList<DBEntity>() : result; |
||||
} |
||||
}); |
||||
}catch(Throwable e){ |
||||
FRUtils.FRLogError("exception getByUsername:"+e.getMessage()); |
||||
} |
||||
|
||||
return new ArrayList<DBEntity>(); |
||||
} |
||||
|
||||
/** |
||||
* 批量操作 |
||||
* @param addOrUpdate |
||||
* @param delete |
||||
* @return |
||||
*/ |
||||
public static boolean batchUser(List<UserEntity> addOrUpdate, List<UserEntity> delete){ |
||||
//新增或者删除
|
||||
if(addOrUpdate != null && addOrUpdate.size() > 0){ |
||||
for(final UserEntity dbe : addOrUpdate){ |
||||
|
||||
try{ |
||||
accessor.runDMLAction(new DBAction<Boolean>() { |
||||
@Override |
||||
public Boolean run(DAOContext context) throws Exception { |
||||
UserEntity ae =context.getDAO(UserDao.class).getById(dbe.getId()); |
||||
if(ae != null ){ |
||||
context.getDAO(UserDao.class).update(dbe); |
||||
}else{ |
||||
context.getDAO(UserDao.class).add(dbe); |
||||
} |
||||
return true; |
||||
} |
||||
}); |
||||
}catch(Throwable e){ |
||||
FRUtils.FRLogError("batch addOrUpdate exception ->"+e.getMessage() + dbe.toString()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
if(delete != null && delete.size() > 0){ |
||||
for(final UserEntity dbe : delete){ |
||||
|
||||
try{ |
||||
accessor.runDMLAction(new DBAction<Boolean>() { |
||||
@Override |
||||
public Boolean run(DAOContext context) throws Exception { |
||||
context.getDAO(UserDao.class).remove(dbe.getId()); |
||||
return true; |
||||
} |
||||
}); |
||||
}catch(Throwable e){ |
||||
FRUtils.FRLogError("batch delete exception ->"+e.getMessage() + dbe.toString()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
|
||||
|
||||
/** |
||||
* 根据username获取信息 |
||||
* @param |
||||
* @return |
||||
*/ |
||||
public static List<UserEntity> getAllUser(){ |
||||
try{ |
||||
return accessor.runQueryAction(new DBAction<List<UserEntity>>() { |
||||
@Override |
||||
public List<UserEntity> run(DAOContext context) throws Exception { |
||||
List<UserEntity> result = context.getDAO(UserDao.class).getAllUser(); |
||||
|
||||
return result == null ? new ArrayList<UserEntity>() : result; |
||||
} |
||||
}); |
||||
}catch(Throwable e){ |
||||
FRUtils.FRLogError("exception getByUsername:"+e.getMessage()); |
||||
} |
||||
|
||||
return new ArrayList<UserEntity>(); |
||||
} |
||||
} |
@ -0,0 +1,58 @@
|
||||
package com.eco.plugin.xx.xytsso.db.dao; |
||||
|
||||
import com.eco.plugin.xx.xytsso.db.bean.DBEntity; |
||||
import com.fr.stable.db.dao.BaseDAO; |
||||
import com.fr.stable.db.dao.DAOProvider; |
||||
import com.fr.stable.db.session.DAOSession; |
||||
import com.fr.stable.query.QueryFactory; |
||||
import com.fr.stable.query.condition.QueryCondition; |
||||
import com.fr.stable.query.restriction.RestrictionFactory; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author xx |
||||
* @version 10.0 |
||||
* Created by xx on 2021-12-03 |
||||
**/ |
||||
public class OrgDao extends BaseDAO<DBEntity> { |
||||
|
||||
public OrgDao(DAOSession session) { |
||||
super(session); |
||||
} |
||||
|
||||
@Override |
||||
protected Class<DBEntity> getEntityClass() { |
||||
return DBEntity.class; |
||||
} |
||||
|
||||
public final static DAOProvider DAO = new DAOProvider() { |
||||
@Override |
||||
public Class getEntityClass() { |
||||
return DBEntity.class; |
||||
} |
||||
|
||||
@Override |
||||
public Class<? extends BaseDAO> getDAOClass() { |
||||
return OrgDao.class; |
||||
} |
||||
}; |
||||
|
||||
public void add(DBEntity entity) throws Exception { |
||||
getSession().persist(entity); |
||||
} |
||||
|
||||
public void remove(String id) throws Exception { |
||||
getSession().remove(QueryFactory.create() |
||||
.addRestriction(RestrictionFactory.eq("id", id)), |
||||
this.getEntityClass()); |
||||
} |
||||
|
||||
public void update(DBEntity entity) throws Exception { |
||||
getSession().merge(entity); |
||||
} |
||||
|
||||
public List<DBEntity> getAllOrg() throws Exception { |
||||
QueryCondition condition = QueryFactory.create(); |
||||
return find(condition); |
||||
} |
||||
} |
@ -0,0 +1,59 @@
|
||||
package com.eco.plugin.xx.xytsso.db.dao; |
||||
|
||||
import com.eco.plugin.xx.xytsso.db.bean.UserEntity; |
||||
import com.fr.stable.db.dao.BaseDAO; |
||||
import com.fr.stable.db.dao.DAOProvider; |
||||
import com.fr.stable.db.session.DAOSession; |
||||
import com.fr.stable.query.QueryFactory; |
||||
import com.fr.stable.query.condition.QueryCondition; |
||||
import com.fr.stable.query.restriction.RestrictionFactory; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author xx |
||||
* @version 10.0 |
||||
* Created by xx on 2021-12-03 |
||||
**/ |
||||
public class UserDao extends BaseDAO<UserEntity> { |
||||
|
||||
public UserDao(DAOSession session) { |
||||
super(session); |
||||
} |
||||
|
||||
@Override |
||||
protected Class<UserEntity> getEntityClass() { |
||||
return UserEntity.class; |
||||
} |
||||
|
||||
public final static DAOProvider DAO = new DAOProvider() { |
||||
@Override |
||||
public Class getEntityClass() { |
||||
return UserEntity.class; |
||||
} |
||||
|
||||
@Override |
||||
public Class<? extends BaseDAO> getDAOClass() { |
||||
return UserDao.class; |
||||
} |
||||
}; |
||||
|
||||
public void add(UserEntity entity) throws Exception { |
||||
getSession().persist(entity); |
||||
} |
||||
|
||||
public void remove(String id) throws Exception { |
||||
getSession().remove(QueryFactory.create() |
||||
.addRestriction(RestrictionFactory.eq("id", id)), |
||||
this.getEntityClass()); |
||||
} |
||||
|
||||
public void update(UserEntity entity) throws Exception { |
||||
getSession().merge(entity); |
||||
} |
||||
|
||||
public List<UserEntity> getAllUser() throws Exception { |
||||
QueryCondition condition = QueryFactory.create(); |
||||
return find(condition); |
||||
} |
||||
} |
@ -0,0 +1,58 @@
|
||||
package com.eco.plugin.xx.xytsso.filter; |
||||
|
||||
import com.eco.plugin.xx.xytsso.utils.FRUtils; |
||||
import com.eco.plugin.xx.xytsso.utils.Utils; |
||||
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||
import com.fr.plugin.context.PluginContexts; |
||||
import com.fr.record.analyzer.EnableMetrics; |
||||
import com.fr.stable.fun.Authorize; |
||||
|
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
@EnableMetrics |
||||
@Authorize(callSignKey = "com.eco.plugin.xx.xytsso") |
||||
public class SSOFilter extends AbstractGlobalRequestFilterProvider { |
||||
@Override |
||||
public String filterName() { |
||||
return "xytssoFilter"; |
||||
} |
||||
|
||||
@Override |
||||
public String[] urlPatterns() { |
||||
return new String[]{"/*"}; |
||||
} |
||||
|
||||
@Override |
||||
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain ){ |
||||
|
||||
if(PluginContexts.currentContext().isAvailable()){ |
||||
if(FRUtils.isLogin(req)){ |
||||
release(req,res,chain); |
||||
return ; |
||||
} |
||||
String username = req.getHeader("iv-user"); |
||||
|
||||
if(Utils.isNullStr(username)){ |
||||
release(req,res,chain); |
||||
return ; |
||||
} |
||||
|
||||
String url = Utils.getRedirectUrl(req,""); |
||||
FRUtils.login(req,res,username,url); |
||||
} |
||||
|
||||
release(req,res,chain); |
||||
} |
||||
|
||||
//放行拦截器
|
||||
private void release(HttpServletRequest req, HttpServletResponse res, FilterChain chain) { |
||||
try{ |
||||
chain.doFilter(req,res); |
||||
}catch (Exception e){ |
||||
FRUtils.FRLogInfo("拦截失败"); |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,407 @@
|
||||
package com.eco.plugin.xx.xytsso.function; |
||||
|
||||
import com.eco.plugin.xx.xytsso.config.PluginSimpleConfig; |
||||
import com.eco.plugin.xx.xytsso.db.bean.DBEntity; |
||||
import com.eco.plugin.xx.xytsso.db.bean.UserEntity; |
||||
import com.eco.plugin.xx.xytsso.db.controller.DBController; |
||||
import com.eco.plugin.xx.xytsso.utils.FRUtils; |
||||
import com.eco.plugin.xx.xytsso.utils.HttpUtils; |
||||
import com.eco.plugin.xx.xytsso.utils.Utils; |
||||
import com.fr.json.JSONArray; |
||||
import com.fr.json.JSONObject; |
||||
import com.fr.script.AbstractFunction; |
||||
import gateway.api.JwtContext; |
||||
import gateway.api.RSAUtils; |
||||
import java.security.PrivateKey; |
||||
import java.security.interfaces.RSAKey; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
public class AuthFunction extends AbstractFunction { |
||||
|
||||
@Override |
||||
public Object run(Object[] objects) { |
||||
PluginSimpleConfig psc = PluginSimpleConfig.getInstance(); |
||||
String orgdomain = getOrgDomain(psc); |
||||
String orgversion = getOrgVersion(psc,orgdomain); |
||||
Map<String,JSONObject> orgArray = null; |
||||
try { |
||||
orgArray = getOrg(psc,orgversion); |
||||
} catch (Exception e) { |
||||
return "fail"; |
||||
} |
||||
operateOrg(orgArray); |
||||
Map<String,JSONArray> users = null; |
||||
try { |
||||
users = getUser(psc,orgArray); |
||||
} catch (Exception e) { |
||||
return "fail"; |
||||
} |
||||
operateUser(users); |
||||
|
||||
return "success"; |
||||
} |
||||
|
||||
/** |
||||
* 获取token |
||||
* @return |
||||
*/ |
||||
private static String getToken(PluginSimpleConfig psc){ |
||||
//token
|
||||
String appToken = psc.getApptoken(); |
||||
//密钥
|
||||
String secret = "-----BEGIN RSA PRIVATE KEY-----\n" + psc.getSecret() + "\n-----END RSA PRIVATE KEY"; |
||||
PrivateKey privatekey = RSAUtils.parsePrivateKeyFromPEM(secret); |
||||
JwtContext jwtContext = JwtContext.create(appToken,(RSAKey)privatekey,1800); |
||||
return jwtContext.createJwtToken().toAuthorizationString(); |
||||
} |
||||
|
||||
private static Map<String,String> getHeader(PluginSimpleConfig psc){ |
||||
String token = getToken(psc); |
||||
Map<String,String> header = new HashMap<String,String>(); |
||||
header.put("Authorization",token); |
||||
|
||||
return header; |
||||
} |
||||
|
||||
/** |
||||
* 获取组织域 |
||||
* @return |
||||
*/ |
||||
private static String getOrgDomain(PluginSimpleConfig psc){ |
||||
String url = psc.getOrgdomainurl(); |
||||
String result = HttpUtils.httpGet(url,null,getHeader(psc)); |
||||
|
||||
if(Utils.isNullStr(result)){ |
||||
return ""; |
||||
} |
||||
|
||||
JSONObject resultJson = new JSONObject(result); |
||||
int code = resultJson.getInt("code"); |
||||
|
||||
if(code != 0){ |
||||
return ""; |
||||
} |
||||
|
||||
JSONArray data = resultJson.getJSONArray("data"); |
||||
|
||||
String orgCode = ""; |
||||
|
||||
for(int i=0;i<data.length();i++){ |
||||
JSONObject json = data.getJSONObject(i); |
||||
String name = json.getString("name"); |
||||
if(!"企业组织".equals(name)){ |
||||
continue; |
||||
} |
||||
|
||||
orgCode =json.getString("code"); |
||||
break; |
||||
} |
||||
|
||||
return orgCode; |
||||
} |
||||
|
||||
/** |
||||
* 获取组织域版本 |
||||
* @return |
||||
*/ |
||||
private static String getOrgVersion(PluginSimpleConfig psc,String orgCode){ |
||||
String url = psc.getOrgversionurl(); |
||||
url = url.replace("{{code}}",orgCode); |
||||
String result = HttpUtils.httpGet(url,null,getHeader(psc)); |
||||
|
||||
if(Utils.isNullStr(result)){ |
||||
return ""; |
||||
} |
||||
|
||||
JSONObject resultJson = new JSONObject(result); |
||||
int code = resultJson.getInt("code"); |
||||
|
||||
if(code != 0){ |
||||
return ""; |
||||
} |
||||
|
||||
JSONObject data = resultJson.getJSONObject("data"); |
||||
|
||||
String version = data.getString("version"); |
||||
|
||||
return version; |
||||
} |
||||
|
||||
private static JSONArray getOrgData(PluginSimpleConfig psc,String url) throws Exception { |
||||
String sxresult = HttpUtils.httpGet(url,null,getHeader(psc)); |
||||
if(Utils.isNullStr(sxresult)){ |
||||
throw new Exception(); |
||||
} |
||||
|
||||
JSONObject sxresultJson = new JSONObject(sxresult); |
||||
int code = sxresultJson.getInt("code"); |
||||
|
||||
if(code != 0){ |
||||
throw new Exception(); |
||||
} |
||||
|
||||
JSONObject sxdata = sxresultJson.getJSONObject("data"); |
||||
|
||||
return sxdata.getJSONArray("data"); |
||||
} |
||||
|
||||
/** |
||||
* 获取机构列表 |
||||
* @return |
||||
*/ |
||||
private static Map<String,JSONObject> getOrg(PluginSimpleConfig psc,String version) throws Exception { |
||||
JSONArray resultArray = new JSONArray(); |
||||
//获取三峡能源机构
|
||||
String url = psc.getDepturl()+"?version="+version+"&whole_tree=true&offset=0&limit=1000&filter=三峡能源"; |
||||
|
||||
JSONArray sxArray =getOrgData(psc,url); |
||||
|
||||
if(sxArray == null || sxArray.size() <= 0){ |
||||
return new HashMap<>(); |
||||
} |
||||
String sxnyid = ""; |
||||
for(int i=0;i<sxArray.size();i++){ |
||||
JSONObject sxny = sxArray.getJSONObject(i); |
||||
String name = sxny.getString("name"); |
||||
if(name.equals("三峡能源")){ |
||||
sxnyid = sxny.getString("node_id"); |
||||
sxny.put("parent_id",""); |
||||
resultArray.add(sxny); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
if(Utils.isNullStr(sxnyid)){ |
||||
return new HashMap<>(); |
||||
} |
||||
|
||||
//获取三峡能源子机构
|
||||
String childurl = psc.getDepturl()+"?version="+version+"&whole_tree=true&offset=0&limit=1000&parentid="+sxnyid; |
||||
|
||||
JSONArray childArray =getOrgData(psc,childurl); |
||||
resultArray.addAll(childArray); |
||||
return operateOrgArray(resultArray); |
||||
} |
||||
|
||||
/** |
||||
* 去除重复数据 |
||||
* @param resultArray |
||||
* @return |
||||
*/ |
||||
private static Map<String,JSONObject> operateOrgArray(JSONArray resultArray) { |
||||
Map<String,JSONObject> result = new HashMap<>(); |
||||
Map<String,JSONObject> temp = new HashMap<>(); |
||||
for(int i=0;i<resultArray.size();i++){ |
||||
JSONObject org = resultArray.getJSONObject(i); |
||||
String pathName = org.getString("path_name"); |
||||
if(result.containsKey(pathName)){ |
||||
result.remove(pathName); |
||||
temp.put(pathName,org); |
||||
} |
||||
|
||||
if(!result.containsKey(pathName) && !temp.containsKey(pathName)){ |
||||
result.put(pathName,org); |
||||
} |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* 获取用户列表 |
||||
* @return |
||||
*/ |
||||
private static Map<String,JSONArray> getUser(PluginSimpleConfig psc,Map<String,JSONObject> deptArray) throws Exception { |
||||
Map<String,JSONArray> result = new HashMap<String,JSONArray>(); |
||||
Map<String,String> header = getHeader(psc); |
||||
FRUtils.FRLogInfo(deptArray.toString()); |
||||
for(Map.Entry<String,JSONObject> org : deptArray.entrySet()){ |
||||
String orgid =org.getValue().getString("id"); |
||||
JSONArray userArray = new JSONArray(); |
||||
getDeptUser(orgid,psc,header,0,userArray); |
||||
result.put(orgid,userArray); |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
|
||||
private static void getDeptUser(String orgid,PluginSimpleConfig psc,Map<String,String> header,int pageNum,JSONArray result) throws Exception { |
||||
String url = psc.getDeptuserurl().replace("{{departmentid}}",orgid)+"?offset="+pageNum+"&limit=1000"; |
||||
|
||||
|
||||
String resultStr = HttpUtils.httpGet(url,null,header); |
||||
|
||||
if(Utils.isNullStr(resultStr)){ |
||||
throw new Exception(); |
||||
} |
||||
|
||||
JSONObject resultJson = new JSONObject(resultStr); |
||||
int code = resultJson.getInt("code"); |
||||
|
||||
if(code != 0){ |
||||
throw new Exception(); |
||||
} |
||||
|
||||
JSONObject data = resultJson.getJSONObject("data"); |
||||
int total = data.getInt("total"); |
||||
result.addAll(data.getJSONArray("data")); |
||||
pageNum = pageNum+1; |
||||
if(pageNum*1000 >= total){ |
||||
return; |
||||
} |
||||
|
||||
getDeptUser(orgid, psc, header, pageNum, result); |
||||
} |
||||
|
||||
private static void operateOrg(Map<String,JSONObject> orgArray){ |
||||
List<DBEntity> newOrg = new ArrayList<DBEntity>(); |
||||
Map<String,DBEntity> newOrgMap = new HashMap<String,DBEntity>(); |
||||
for(Map.Entry<String,JSONObject> org : orgArray.entrySet()){ |
||||
DBEntity db =jsonToOrg(org.getValue()); |
||||
newOrg.add(db); |
||||
newOrgMap.put(db.getId(),db); |
||||
} |
||||
|
||||
List<DBEntity> oldOrg = DBController.getByAllOrg(); |
||||
List<DBEntity> deleteOrg = new ArrayList<DBEntity>(); |
||||
|
||||
if(oldOrg != null && oldOrg.size() > 0){ |
||||
for(DBEntity oldEntity : oldOrg){ |
||||
String id = oldEntity.getId(); |
||||
if(newOrgMap.containsKey(id)){ |
||||
continue; |
||||
} |
||||
|
||||
deleteOrg.add(oldEntity); |
||||
} |
||||
} |
||||
|
||||
DBController.batch(newOrg,deleteOrg); |
||||
} |
||||
|
||||
private static void operateUser(Map<String,JSONArray> userArray){ |
||||
List<UserEntity> newOrg = new ArrayList<UserEntity>(); |
||||
Map<String,UserEntity> newOrgMap = new HashMap<String,UserEntity>(); |
||||
for(Map.Entry<String,JSONArray> entryset : userArray.entrySet()){ |
||||
String orgid = entryset.getKey(); |
||||
JSONArray users = entryset.getValue(); |
||||
|
||||
for(int i =0;i<users.length();i++){ |
||||
JSONObject user = users.getJSONObject(i); |
||||
UserEntity db =jsonToUser(user,orgid); |
||||
newOrg.add(db); |
||||
newOrgMap.put(db.getId(),db); |
||||
} |
||||
|
||||
} |
||||
|
||||
List<UserEntity> oldOrg = DBController.getAllUser(); |
||||
List<UserEntity> deleteOrg = new ArrayList<UserEntity>(); |
||||
|
||||
if(oldOrg != null && oldOrg.size() > 0){ |
||||
for(UserEntity oldEntity : oldOrg){ |
||||
String id = oldEntity.getId(); |
||||
if(newOrgMap.containsKey(id)){ |
||||
continue; |
||||
} |
||||
|
||||
deleteOrg.add(oldEntity); |
||||
} |
||||
} |
||||
|
||||
DBController.batchUser(newOrg,deleteOrg); |
||||
} |
||||
|
||||
private static DBEntity jsonToOrg(JSONObject json){ |
||||
DBEntity db = new DBEntity(); |
||||
String id = json.getString("id"); |
||||
db.setId(id); |
||||
String code = json.getString("code"); |
||||
db.setCode(code); |
||||
String name = json.getString("name"); |
||||
db.setName(name); |
||||
String node_id = json.getString("node_id"); |
||||
db.setNode_id(node_id); |
||||
String parent_id = json.getString("parent_id"); |
||||
if(parent_id.equals("root")){ |
||||
parent_id = ""; |
||||
} |
||||
db.setParent_id(parent_id); |
||||
String version = json.getString("version"); |
||||
db.setVersion(version); |
||||
String is_virtual = json.getString("is_virtual"); |
||||
db.setIs_virtual(is_virtual); |
||||
String is_visible = json.getString("is_visible"); |
||||
db.setIs_visible(is_visible); |
||||
String jianpin = json.getString("jianpin"); |
||||
db.setJianpin(jianpin); |
||||
String mark = json.getString("mark"); |
||||
db.setMark(mark); |
||||
String create_time = json.getString("create_time"); |
||||
db.setCreate_time(create_time); |
||||
String create_user = json.getString("create_user"); |
||||
db.setCreate_user(create_user); |
||||
String modify_user = json.getString("modify_user"); |
||||
db.setModify_user(modify_user); |
||||
String ordinal = json.getString("ordinal"); |
||||
db.setOrdinal(ordinal); |
||||
String path_id = json.getString("path_id"); |
||||
db.setPath_id(path_id); |
||||
String path_name = json.getString("path_name"); |
||||
db.setPath_name(path_name); |
||||
String quanpin = json.getString("quanpin"); |
||||
db.setQuanpin(quanpin); |
||||
String enabled = json.getString("enabled"); |
||||
db.setEnabled(enabled); |
||||
String extends_properties = json.getString("extends_properties"); |
||||
db.setExtends_properties(extends_properties); |
||||
|
||||
return db; |
||||
} |
||||
|
||||
private static UserEntity jsonToUser(JSONObject json,String orgid){ |
||||
UserEntity user = new UserEntity(); |
||||
String user_id = json.getString("user_id"); |
||||
user.setId(user_id); |
||||
user.setUser_id(user_id); |
||||
String user_name = json.getString("user_name"); |
||||
user.setUser_name(user_name); |
||||
String birthday = json.getString("birthday"); |
||||
user.setBirthday(birthday); |
||||
String code = json.getString("code"); |
||||
user.setCode(code); |
||||
String employeeid = json.getString("employeeid"); |
||||
user.setEmployeeid(employeeid); |
||||
String enabled = json.getString("enabled"); |
||||
user.setEnabled(enabled); |
||||
String idcard = json.getString("idcard"); |
||||
user.setIdcard(idcard); |
||||
String name = json.getString("name"); |
||||
user.setName(name); |
||||
String quanpin = json.getString("quanpin"); |
||||
user.setQuanpin(quanpin); |
||||
String jianpin = json.getString("jianpin"); |
||||
user.setJianpin(jianpin); |
||||
String office = json.getString("office"); |
||||
user.setOffice(office); |
||||
String owner_place = json.getString("owner_place"); |
||||
user.setOwner_place(owner_place); |
||||
String regist_email = json.getString("regist_email"); |
||||
user.setRegist_email(regist_email); |
||||
String regist_mobile = json.getString("regist_mobile"); |
||||
user.setRegist_mobile(regist_mobile); |
||||
String telephone = json.getString("telephone"); |
||||
user.setTelephone(telephone); |
||||
String user_scope = json.getString("user_scope"); |
||||
user.setUser_scope(user_scope); |
||||
String create_time = json.getString("create_time"); |
||||
user.setCreate_time(create_time); |
||||
String extends_properties = json.getString("extends_properties"); |
||||
user.setExtends_properties(extends_properties); |
||||
user.setOrgid(orgid); |
||||
return user; |
||||
} |
||||
} |
@ -0,0 +1,308 @@
|
||||
package com.eco.plugin.xx.xytsso.utils; |
||||
|
||||
import com.fr.base.ServerConfig; |
||||
import com.fr.base.TableData; |
||||
import com.fr.base.TemplateUtils; |
||||
import com.fr.decision.authority.AuthorityContext; |
||||
import com.fr.decision.authority.base.constant.type.operation.ManualOperationType; |
||||
import com.fr.decision.authority.data.User; |
||||
import com.fr.decision.base.util.UUIDUtil; |
||||
import com.fr.decision.privilege.encrpt.PasswordValidator; |
||||
import com.fr.decision.webservice.bean.authentication.OriginUrlResponseBean; |
||||
import com.fr.decision.webservice.interceptor.handler.ReportTemplateRequestChecker; |
||||
import com.fr.decision.webservice.login.LogInOutResultInfo; |
||||
import com.fr.decision.webservice.utils.DecisionServiceConstants; |
||||
import com.fr.decision.webservice.utils.DecisionStatusService; |
||||
import com.fr.decision.webservice.utils.UserSourceFactory; |
||||
import com.fr.decision.webservice.v10.login.LoginService; |
||||
import com.fr.decision.webservice.v10.login.event.LogInOutEvent; |
||||
import com.fr.decision.webservice.v10.user.UserService; |
||||
import com.fr.event.EventDispatcher; |
||||
import com.fr.file.TableDataConfig; |
||||
import com.fr.general.data.DataModel; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.script.Calculator; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.query.QueryFactory; |
||||
import com.fr.stable.query.restriction.RestrictionFactory; |
||||
import com.fr.third.springframework.web.method.HandlerMethod; |
||||
import com.fr.web.controller.ReportRequestService; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.Cookie; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import javax.servlet.http.HttpSession; |
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
|
||||
public class FRUtils { |
||||
/** |
||||
* 判断用户是否存在 |
||||
* @param userName |
||||
* @return |
||||
*/ |
||||
public static boolean isUserExist(String userName){ |
||||
if (StringUtils.isEmpty(userName)) { |
||||
return false; |
||||
} else { |
||||
try { |
||||
List var1 = AuthorityContext.getInstance().getUserController().find(QueryFactory.create().addRestriction(RestrictionFactory.eq("userName", userName))); |
||||
return var1 != null && !var1.isEmpty(); |
||||
} catch (Exception var2) { |
||||
FineLoggerFactory.getLogger().error(var2.getMessage()); |
||||
return false; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 判断是否登录FR |
||||
* @param req |
||||
* @return |
||||
*/ |
||||
public static boolean isLogin(HttpServletRequest req){ |
||||
return LoginService.getInstance().isLogged(req); |
||||
} |
||||
|
||||
/** |
||||
* 帆软登录 |
||||
* @param httpServletRequest |
||||
* @param httpServletResponse |
||||
* @param userName |
||||
* @param url |
||||
*/ |
||||
public static void login(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String userName,String url){ |
||||
|
||||
FineLoggerFactory.getLogger().info("FRLOG:用户名:"+userName); |
||||
FineLoggerFactory.getLogger().info("FRLOG:跳转链接:"+url); |
||||
|
||||
|
||||
//判断用户名是否为空
|
||||
if(!Utils.isNullStr(userName)){ |
||||
if(isUserExist(userName)){ |
||||
String FRToken = ""; |
||||
|
||||
try { |
||||
//HttpSession session = httpServletRequest.getSession(true);
|
||||
|
||||
FRToken = LoginService.getInstance().login(httpServletRequest, httpServletResponse, userName); |
||||
|
||||
//httpServletRequest.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME,FRToken);
|
||||
|
||||
//session.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, FRToken);
|
||||
EventDispatcher.fire(LogInOutEvent.LOGIN,new LogInOutResultInfo(httpServletRequest,httpServletResponse,userName,true)); |
||||
FineLoggerFactory.getLogger().info("FRLOG:登陆成功!"); |
||||
|
||||
if(!Utils.isNullStr(url)){ |
||||
httpServletResponse.sendRedirect(url); |
||||
} |
||||
} catch (Exception e) { |
||||
ResponseUtils.failedResponse(httpServletResponse,"登录异常,请联系管理员!"); |
||||
FineLoggerFactory.getLogger().info("FRLOG:登录异常,请联系管理员!"); |
||||
FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage()); |
||||
} |
||||
}else{ |
||||
ResponseUtils.failedResponse(httpServletResponse,"用户在报表系统中不存在!"); |
||||
FineLoggerFactory.getLogger().info("FRLOG:用户在报表系统中不存在!"); |
||||
} |
||||
}else{ |
||||
ResponseUtils.failedResponse(httpServletResponse,"用户名不能为空!"); |
||||
FineLoggerFactory.getLogger().info("FRLOG:用户名不能为空!"); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 帆软登录 |
||||
* @param httpServletRequest |
||||
* @param httpServletResponse |
||||
* @param token |
||||
* @param url |
||||
*/ |
||||
public static void loginByToken(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String token,String url){ |
||||
|
||||
FineLoggerFactory.getLogger().info("FRLOG:token:"+token); |
||||
FineLoggerFactory.getLogger().info("FRLOG:跳转链接:"+url); |
||||
|
||||
|
||||
//判断用户名是否为空
|
||||
if(!Utils.isNullStr(token)){ |
||||
writeToken2Cookie(httpServletResponse,token,-1); |
||||
|
||||
HttpSession session = httpServletRequest.getSession(true); |
||||
|
||||
httpServletRequest.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME,token); |
||||
|
||||
session.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, token); |
||||
|
||||
if(!Utils.isNullStr(url)){ |
||||
try { |
||||
httpServletResponse.sendRedirect(url); |
||||
} catch (IOException e) { |
||||
ResponseUtils.failedResponse(httpServletResponse,"跳转异常!"); |
||||
FineLoggerFactory.getLogger().info("FRLOG:跳转异常!"); |
||||
} |
||||
} |
||||
}else{ |
||||
ResponseUtils.failedResponse(httpServletResponse,"token不能为空!"); |
||||
FineLoggerFactory.getLogger().info("FRLOG:token不能为空!"); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 获取token |
||||
* @param httpServletRequest |
||||
* @param httpServletResponse |
||||
* @param username |
||||
* @return |
||||
*/ |
||||
public static String getToken(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String username){ |
||||
String token = ""; |
||||
try { |
||||
token = LoginService.getInstance().login(httpServletRequest, httpServletResponse, username); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:获取token失败"+e.getMessage()); |
||||
} |
||||
|
||||
return token; |
||||
} |
||||
|
||||
private static void writeToken2Cookie(HttpServletResponse var1, String var2, int var3) { |
||||
try { |
||||
if (StringUtils.isNotEmpty(var2)) { |
||||
Cookie var4 = new Cookie("fine_auth_token", var2); |
||||
long var5 = var3 == -2 ? 1209600000L : (long)var3; |
||||
var4.setMaxAge((int)var5); |
||||
var4.setPath(ServerConfig.getInstance().getCookiePath()); |
||||
var1.addCookie(var4); |
||||
Cookie var7 = new Cookie("fine_remember_login", String.valueOf(var3 == -2 ? -2 : -1)); |
||||
var7.setMaxAge((int)var5); |
||||
var7.setPath(ServerConfig.getInstance().getCookiePath()); |
||||
var1.addCookie(var7); |
||||
} else { |
||||
FineLoggerFactory.getLogger().error("empty token cannot save."); |
||||
} |
||||
} catch (Exception var8) { |
||||
FineLoggerFactory.getLogger().error(var8.getMessage(), var8); |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param httpServletRequest |
||||
* @param httpServletResponse |
||||
*/ |
||||
public static void logout(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) |
||||
{ |
||||
if(!isLogin(httpServletRequest)){ |
||||
return ; |
||||
} |
||||
|
||||
try { |
||||
LoginService.getInstance().logout(httpServletRequest,httpServletResponse); |
||||
} catch (Exception e) { |
||||
ResponseUtils.failedResponse(httpServletResponse,"登出异常,请联系管理员!"); |
||||
FineLoggerFactory.getLogger().info("FRLOG:登出异常,请联系管理员!"); |
||||
FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 打印FR日志 |
||||
* @param message |
||||
*/ |
||||
public static void FRLogInfo(String message){ |
||||
FineLoggerFactory.getLogger().info("FRLOG:"+message); |
||||
} |
||||
|
||||
/** |
||||
* 打印FR日志-error |
||||
* @param message |
||||
*/ |
||||
public static void FRLogError(String message){ |
||||
FineLoggerFactory.getLogger().error("FRLOG:"+message); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 根据用户名获取用户信息 |
||||
* @param userName |
||||
* @return |
||||
*/ |
||||
public static User getFRUserByUserName(String userName){ |
||||
try { |
||||
return UserService.getInstance().getUserByUserName(userName); |
||||
} catch (Exception e) { |
||||
FRLogInfo("获取用户信息异常:"+e.getMessage()); |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 根据明文密码生成数据库中的密码,用户密码校验用 |
||||
* @return |
||||
*/ |
||||
public static String getDBPsd(String username,String password){ |
||||
PasswordValidator pv = UserSourceFactory.getInstance().getUserSource(ManualOperationType.KEY).getPasswordValidator(); |
||||
String uuid = UUIDUtil.generate(); |
||||
|
||||
return pv.encode(username, password, uuid); |
||||
} |
||||
|
||||
/** |
||||
* 获取带参数的访问链接 |
||||
* @return |
||||
*/ |
||||
public static String getAllUrl(HttpServletRequest httpServletRequest){ |
||||
return WebUtils.getOriginalURL(httpServletRequest); |
||||
} |
||||
|
||||
/** |
||||
* 根据originKey获取源链接 |
||||
* @param originKey |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static String getOriginUrl(String originKey) throws Exception { |
||||
if (StringUtils.isNotEmpty(originKey)) { |
||||
OriginUrlResponseBean originUrlResponseBean = (OriginUrlResponseBean) DecisionStatusService.originUrlStatusService().get(originKey); |
||||
DecisionStatusService.originUrlStatusService().delete(originKey); |
||||
if (originUrlResponseBean != null) { |
||||
return originUrlResponseBean.getOriginUrl(); |
||||
} |
||||
} |
||||
|
||||
return new OriginUrlResponseBean(TemplateUtils.render("${fineServletURL}")).getOriginUrl(); |
||||
} |
||||
|
||||
/** |
||||
* 判断是否开启模板认证 |
||||
* @param |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static boolean isTempAuth(HttpServletRequest req,HttpServletResponse res) throws Exception { |
||||
ReportTemplateRequestChecker checker = new ReportTemplateRequestChecker(); |
||||
HandlerMethod hm = new HandlerMethod(new ReportRequestService(),ReportRequestService.class.getMethod("preview", HttpServletRequest.class, HttpServletResponse.class, String.class)); |
||||
return checker.checkRequest(req,res,hm); |
||||
} |
||||
|
||||
/** |
||||
* 获取数据集数据 |
||||
* @param serverDataSetName |
||||
* @return |
||||
*/ |
||||
public static DataModel getTableData(String serverDataSetName){ |
||||
TableData userInfo = TableDataConfig.getInstance().getTableData(serverDataSetName); |
||||
DataModel userInfoDM = userInfo.createDataModel(Calculator.createCalculator()); |
||||
|
||||
return userInfoDM; |
||||
} |
||||
|
||||
public static String getIndex(HttpServletRequest req){ |
||||
String url = req.getScheme()+"://"+req.getServerName()+":"+String.valueOf(req.getServerPort())+req.getRequestURI(); |
||||
return url; |
||||
} |
||||
} |
@ -0,0 +1,263 @@
|
||||
package com.eco.plugin.xx.xytsso.utils; |
||||
|
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.third.org.apache.http.HttpEntity; |
||||
import com.fr.third.org.apache.http.HttpResponse; |
||||
import com.fr.third.org.apache.http.HttpStatus; |
||||
import com.fr.third.org.apache.http.NameValuePair; |
||||
import com.fr.third.org.apache.http.client.CookieStore; |
||||
import com.fr.third.org.apache.http.client.entity.UrlEncodedFormEntity; |
||||
import com.fr.third.org.apache.http.client.methods.HttpGet; |
||||
import com.fr.third.org.apache.http.client.methods.HttpPost; |
||||
import com.fr.third.org.apache.http.conn.ssl.NoopHostnameVerifier; |
||||
import com.fr.third.org.apache.http.entity.StringEntity; |
||||
import com.fr.third.org.apache.http.impl.client.BasicCookieStore; |
||||
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; |
||||
import com.fr.third.org.apache.http.impl.client.HttpClients; |
||||
import com.fr.third.org.apache.http.impl.cookie.BasicClientCookie; |
||||
import com.fr.third.org.apache.http.message.BasicNameValuePair; |
||||
import com.fr.third.org.apache.http.ssl.SSLContexts; |
||||
import com.fr.third.org.apache.http.ssl.TrustStrategy; |
||||
import com.fr.third.org.apache.http.util.EntityUtils; |
||||
|
||||
import javax.net.ssl.SSLContext; |
||||
import javax.servlet.http.Cookie; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.security.cert.CertificateException; |
||||
import java.security.cert.X509Certificate; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
public class HttpUtils { |
||||
|
||||
/** |
||||
* httpGet请求 |
||||
* @param url |
||||
* @return |
||||
*/ |
||||
public static String httpGet(String url,Cookie[] cookies,Map<String,String> header){ |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--url:"+url); |
||||
|
||||
//创建httpClient
|
||||
CloseableHttpClient httpclient = createHttpClient(cookies); |
||||
|
||||
HttpGet getMethod = new HttpGet(url); |
||||
|
||||
if(header != null && header.size() > 0){ |
||||
FRUtils.FRLogInfo("header:"+header.toString()); |
||||
Set<String> keySet = header.keySet(); |
||||
|
||||
for(String key : keySet){ |
||||
getMethod.setHeader(key,header.get(key)); |
||||
} |
||||
} |
||||
|
||||
try { |
||||
HttpResponse response = httpclient.execute(getMethod); |
||||
int status =response.getStatusLine().getStatusCode(); |
||||
HttpEntity entity = response.getEntity(); |
||||
String returnResult = EntityUtils.toString(entity, "utf-8"); |
||||
|
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--status:"+status); |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--returnResult:"+returnResult); |
||||
|
||||
httpclient.close(); |
||||
|
||||
if (status == HttpStatus.SC_OK) { |
||||
return returnResult; |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--exception:"+e.getMessage()); |
||||
} |
||||
|
||||
try { |
||||
httpclient.close(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:http关闭异常:"+e.getMessage()); |
||||
} |
||||
|
||||
return ""; |
||||
} |
||||
|
||||
/** |
||||
* HttpPost请求 |
||||
* @param postMethod |
||||
* @return |
||||
*/ |
||||
private static String HttpPost(HttpPost postMethod){ |
||||
CloseableHttpClient httpclient = createHttpClient(null); |
||||
|
||||
try { |
||||
HttpResponse response = httpclient.execute(postMethod); |
||||
int status = response.getStatusLine().getStatusCode(); |
||||
HttpEntity entity = response.getEntity(); |
||||
String returnResult = EntityUtils.toString(entity, "utf-8"); |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:status:"+status); |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:returnResult:"+returnResult); |
||||
httpclient.close(); |
||||
|
||||
if (status == HttpStatus.SC_OK) { |
||||
return returnResult; |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:exception:"+e.getMessage()); |
||||
} |
||||
|
||||
try { |
||||
httpclient.close(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:http关闭异常:"+e.getMessage()); |
||||
} |
||||
|
||||
return ""; |
||||
} |
||||
|
||||
public static String HttpPostXML(String url, String xmlParam){ |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:url:"+url); |
||||
|
||||
HttpPost postMethod = new HttpPost(url); |
||||
|
||||
postMethod.setHeader("Content-type", "text/html"); |
||||
HttpEntity entity2 = null; |
||||
try { |
||||
entity2 = new StringEntity(xmlParam); |
||||
} catch (UnsupportedEncodingException e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:参数异常:"+e.getMessage()); |
||||
return ""; |
||||
} |
||||
|
||||
postMethod.setEntity(entity2); |
||||
|
||||
return HttpPost(postMethod); |
||||
} |
||||
|
||||
public static String HttpPostText(String url, String xmlParam){ |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPostText:url:"+url); |
||||
|
||||
HttpPost postMethod = new HttpPost(url); |
||||
|
||||
postMethod.setHeader("Content-type", "text/plain"); |
||||
HttpEntity entity2 = null; |
||||
try { |
||||
entity2 = new StringEntity(xmlParam); |
||||
} catch (UnsupportedEncodingException e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPostText:参数异常:"+e.getMessage()); |
||||
return ""; |
||||
} |
||||
|
||||
postMethod.setEntity(entity2); |
||||
|
||||
return HttpPost(postMethod); |
||||
} |
||||
|
||||
public static String HttpPostJson(String url, String param,Map<String,String> header){ |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPostJSON:url:"+url); |
||||
|
||||
HttpPost postMethod = new HttpPost(url); |
||||
|
||||
postMethod.setHeader("Content-Type","application/json"); |
||||
|
||||
if(header != null && header.size() > 0){ |
||||
Set<String> keySet = header.keySet(); |
||||
|
||||
for(String key : keySet){ |
||||
postMethod.setHeader(key,header.get(key)); |
||||
} |
||||
} |
||||
|
||||
if(!Utils.isNullStr(param)){ |
||||
HttpEntity entity2 = null; |
||||
try { |
||||
entity2 = new StringEntity(param); |
||||
} catch (UnsupportedEncodingException e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPostJSON:参数异常:"+e.getMessage()); |
||||
return ""; |
||||
} |
||||
|
||||
postMethod.setEntity(entity2); |
||||
} |
||||
|
||||
return HttpPost(postMethod); |
||||
} |
||||
|
||||
public static String HttpPostWWWForm(String url, Map<String,String> header,Map<String,String> param){ |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpWWWForm:url:"+url); |
||||
|
||||
HttpPost postMethod = new HttpPost(url); |
||||
|
||||
if(header != null && header.size() > 0){ |
||||
Set<String> keySet = header.keySet(); |
||||
|
||||
for(String key : keySet){ |
||||
postMethod.setHeader(key,header.get(key)); |
||||
} |
||||
} |
||||
|
||||
if(param != null && param.size() > 0){ |
||||
List<NameValuePair> params = new ArrayList<NameValuePair>(param.size()); |
||||
|
||||
for(Map.Entry<String,String> map : param.entrySet()){ |
||||
params.add(new BasicNameValuePair(map.getKey(), map.getValue())); |
||||
} |
||||
|
||||
try { |
||||
postMethod.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); |
||||
} catch (UnsupportedEncodingException e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpWWWForm:异常:"+e.getMessage()); |
||||
return ""; |
||||
} |
||||
} |
||||
|
||||
return HttpPost(postMethod); |
||||
} |
||||
|
||||
private static CloseableHttpClient createHttpClient(Cookie[] cookies){ |
||||
|
||||
SSLContext sslContext = null; |
||||
try { |
||||
sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { |
||||
@Override |
||||
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { |
||||
return true; |
||||
} |
||||
}).build(); |
||||
} catch (Exception e) { |
||||
FRUtils.FRLogInfo("exception:"+e.getMessage()); |
||||
} |
||||
|
||||
CloseableHttpClient httpclient = null; |
||||
|
||||
if(cookies != null && cookies.length > 0){ |
||||
CookieStore cookieStore = cookieToCookieStore(cookies); |
||||
|
||||
httpclient = HttpClients.custom().setSslcontext(sslContext). |
||||
setSSLHostnameVerifier(new NoopHostnameVerifier()).setDefaultCookieStore(cookieStore).build(); |
||||
} |
||||
else{ |
||||
httpclient = HttpClients.custom().setSslcontext(sslContext). |
||||
setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); |
||||
} |
||||
|
||||
return httpclient; |
||||
} |
||||
|
||||
/** |
||||
* cookies转cookieStore |
||||
* @param cookies |
||||
* @return |
||||
*/ |
||||
public static CookieStore cookieToCookieStore(Cookie[] cookies){ |
||||
CookieStore cookieStore = new BasicCookieStore(); |
||||
|
||||
if(cookies != null && cookies.length>0){ |
||||
for(Cookie cookie : cookies){ |
||||
BasicClientCookie cookie1 = new BasicClientCookie(cookie.getName(), cookie.getValue()); |
||||
cookieStore.addCookie(cookie1); |
||||
} |
||||
} |
||||
|
||||
return cookieStore; |
||||
} |
||||
} |
@ -0,0 +1,108 @@
|
||||
package com.eco.plugin.xx.xytsso.utils; |
||||
|
||||
import com.fr.json.JSONObject; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.PrintWriter; |
||||
|
||||
public class ResponseUtils { |
||||
private static final int SUCCESS = 200; |
||||
private static final int FAILED = -1; |
||||
|
||||
public static void successResponse(HttpServletResponse res, String body) { |
||||
response(res, body, SUCCESS); |
||||
} |
||||
|
||||
public static void failedResponse(HttpServletResponse res, String body) { |
||||
response(res, body, FAILED); |
||||
} |
||||
|
||||
private static void response(HttpServletResponse res, String body, int code) { |
||||
JSONObject object = new JSONObject(); |
||||
PrintWriter pw; |
||||
try { |
||||
object.put("code", code); |
||||
object.put("data", body); |
||||
pw = WebUtils.createPrintWriter(res); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info(e.getMessage()); |
||||
return; |
||||
} |
||||
res.setContentType("application/json;charset=utf-8"); |
||||
String result = object.toString(); |
||||
pw.println(result); |
||||
pw.flush(); |
||||
pw.close(); |
||||
} |
||||
|
||||
public static void response(HttpServletResponse res,JSONObject json){ |
||||
PrintWriter pw; |
||||
try { |
||||
pw = WebUtils.createPrintWriter(res); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info(e.getMessage()); |
||||
return; |
||||
} |
||||
res.setContentType("application/json;charset=utf-8"); |
||||
String result = json.toString(); |
||||
pw.println(result); |
||||
pw.flush(); |
||||
pw.close(); |
||||
} |
||||
|
||||
public static void responseText(HttpServletResponse res,String text){ |
||||
PrintWriter pw; |
||||
try { |
||||
pw = WebUtils.createPrintWriter(res); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info(e.getMessage()); |
||||
return; |
||||
} |
||||
res.setContentType("text/html;charset=utf-8"); |
||||
pw.println(text); |
||||
pw.flush(); |
||||
pw.close(); |
||||
} |
||||
|
||||
public static void responseXml(HttpServletResponse res,String xml){ |
||||
PrintWriter pw; |
||||
try { |
||||
pw = WebUtils.createPrintWriter(res); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info(e.getMessage()); |
||||
return; |
||||
} |
||||
res.setContentType("text/xml;charset=utf-8"); |
||||
pw.println(xml); |
||||
pw.flush(); |
||||
pw.close(); |
||||
} |
||||
|
||||
public static void setCSRFHeader(HttpServletResponse httpServletResponse){ |
||||
httpServletResponse.setHeader("Access-Control-Allow-Origin", "*"); |
||||
httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,HEAD,PUT,PATCH"); |
||||
httpServletResponse.setHeader("Access-Control-Max-Age", "36000"); |
||||
httpServletResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,Authorization,authorization"); |
||||
} |
||||
|
||||
public static void responseJsonp(HttpServletRequest req, HttpServletResponse res, JSONObject json){ |
||||
PrintWriter pw; |
||||
try { |
||||
pw = WebUtils.createPrintWriter(res); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info(e.getMessage()); |
||||
return; |
||||
} |
||||
res.setContentType("text/javascript;charset=utf-8;charset=utf-8"); |
||||
String result = json.toString(); |
||||
|
||||
String jsonp=req.getParameter("callback"); |
||||
|
||||
pw.println(jsonp+"("+result+")"); |
||||
pw.flush(); |
||||
pw.close(); |
||||
} |
||||
} |
@ -0,0 +1,329 @@
|
||||
package com.eco.plugin.xx.xytsso.utils; |
||||
|
||||
import com.fr.base.TemplateUtils; |
||||
import com.fr.data.NetworkHelper; |
||||
import com.fr.decision.webservice.v10.user.UserService; |
||||
import com.fr.io.utils.ResourceIOUtils; |
||||
import com.fr.json.JSONObject; |
||||
import com.fr.stable.CodeUtils; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.third.org.apache.commons.codec.digest.DigestUtils; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.Cookie; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.BufferedReader; |
||||
import java.io.InputStream; |
||||
import java.net.URLEncoder; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.UUID; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
public class Utils { |
||||
|
||||
/** |
||||
* 判断字符串是否为空 |
||||
* @param str |
||||
* @return true 空字符串 false 非空字符串 |
||||
*/ |
||||
public static boolean isNullStr(String str){ |
||||
return !(str != null && !str.isEmpty() && !"null".equals(str)); |
||||
} |
||||
|
||||
/** |
||||
* 判断字符串是否非空 |
||||
* @param str |
||||
* @return |
||||
*/ |
||||
public static boolean isNotNullStr(String str){ |
||||
return !isNullStr(str); |
||||
} |
||||
|
||||
/** |
||||
* MD5加密 |
||||
* @param str |
||||
* @return |
||||
*/ |
||||
public static String getMd5Str(String str) |
||||
{ |
||||
return DigestUtils.md5Hex(str); |
||||
} |
||||
|
||||
/** |
||||
* 帆软shaEncode加密 |
||||
*/ |
||||
|
||||
public static String shaEncode(String str){ |
||||
return CodeUtils.sha256Encode(str); |
||||
} |
||||
|
||||
/** |
||||
* 获取uuid |
||||
*/ |
||||
public static String uuid(){ |
||||
return UUID.randomUUID().toString(); |
||||
} |
||||
|
||||
/** |
||||
* 替换空字符串 |
||||
* @param str |
||||
* @param replace |
||||
* @return |
||||
*/ |
||||
public static String replaceNullStr(String str,String replace){ |
||||
if(isNullStr(str)){ |
||||
return replace; |
||||
} |
||||
|
||||
return str; |
||||
} |
||||
|
||||
/** |
||||
* 获取请求体 |
||||
* @param req |
||||
* @return |
||||
*/ |
||||
public static JSONObject getRequestBody(HttpServletRequest req){ |
||||
StringBuffer sb = new StringBuffer(); |
||||
String line = null; |
||||
try { |
||||
BufferedReader reader = req.getReader(); |
||||
while ((line = reader.readLine()) != null) |
||||
sb.append(line); |
||||
} catch (Exception e) { |
||||
FRUtils.FRLogInfo("getRequestBody:exception:"+e.getMessage()); |
||||
} |
||||
//将空格和换行符替换掉避免使用反序列化工具解析对象时失败
|
||||
String jsonString = sb.toString().replaceAll("\\s","").replaceAll("\n",""); |
||||
|
||||
JSONObject json = new JSONObject(jsonString); |
||||
|
||||
return json; |
||||
} |
||||
|
||||
/** |
||||
* 获取ip |
||||
* @return |
||||
*/ |
||||
public static String getIp(HttpServletRequest req){ |
||||
String realIp = req.getHeader("X-Real-IP"); |
||||
String fw = req.getHeader("X-Forwarded-For"); |
||||
if (StringUtils.isNotEmpty(fw) && !"unKnown".equalsIgnoreCase(fw)) { |
||||
int var3 = fw.indexOf(","); |
||||
return var3 != -1 ? fw.substring(0, var3) : fw; |
||||
} else { |
||||
fw = realIp; |
||||
if (StringUtils.isNotEmpty(realIp) && !"unKnown".equalsIgnoreCase(realIp)) { |
||||
return realIp; |
||||
} else { |
||||
if (StringUtils.isBlank(realIp) || "unknown".equalsIgnoreCase(realIp)) { |
||||
fw = req.getHeader("Proxy-Client-IP"); |
||||
} |
||||
|
||||
if (StringUtils.isBlank(fw) || "unknown".equalsIgnoreCase(fw)) { |
||||
fw = req.getHeader("WL-Proxy-Client-IP"); |
||||
} |
||||
|
||||
if (StringUtils.isBlank(fw) || "unknown".equalsIgnoreCase(fw)) { |
||||
fw = req.getHeader("HTTP_CLIENT_IP"); |
||||
} |
||||
|
||||
if (StringUtils.isBlank(fw) || "unknown".equalsIgnoreCase(fw)) { |
||||
fw = req.getHeader("HTTP_X_FORWARDED_FOR"); |
||||
} |
||||
|
||||
if (StringUtils.isBlank(fw) || "unknown".equalsIgnoreCase(fw)) { |
||||
fw = req.getRemoteAddr(); |
||||
} |
||||
|
||||
return fw; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 根据key获取cookie |
||||
* @param req |
||||
* @return |
||||
*/ |
||||
public static String getCookieByKey(HttpServletRequest req,String key){ |
||||
Cookie[] cookies = req.getCookies(); |
||||
String cookie = ""; |
||||
|
||||
if(cookies == null || cookies.length <=0){ |
||||
return ""; |
||||
} |
||||
|
||||
for(int i = 0; i < cookies.length; i++) { |
||||
Cookie item = cookies[i]; |
||||
if (item.getName().equalsIgnoreCase(key)) { |
||||
cookie = item.getValue(); |
||||
} |
||||
} |
||||
|
||||
FRUtils.FRLogInfo("cookie:"+cookie); |
||||
|
||||
return cookie; |
||||
} |
||||
|
||||
/** |
||||
* 判断是否是手机端的链接 |
||||
* @param req |
||||
* @return |
||||
*/ |
||||
public static boolean isMobile(HttpServletRequest req) { |
||||
String[] mobileArray = {"iPhone", "iPad", "android", "windows phone", "xiaomi"}; |
||||
String userAgent = req.getHeader("user-agent"); |
||||
if (userAgent != null && userAgent.toUpperCase().contains("MOBILE")) { |
||||
for(String mobile : mobileArray) { |
||||
if(userAgent.toUpperCase().contains(mobile.toUpperCase())) { |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
return NetworkHelper.getDevice(req).isMobile(); |
||||
} |
||||
|
||||
/** |
||||
* 只编码中文 |
||||
* @param url |
||||
* @return |
||||
*/ |
||||
public static String encodeCH(String url ){ |
||||
Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url); |
||||
|
||||
while(matcher.find()){ |
||||
String chn = matcher.group(); |
||||
url = url.replaceAll(chn, URLEncoder.encode(chn)); |
||||
} |
||||
|
||||
return url; |
||||
} |
||||
|
||||
/** |
||||
* 获取web-inf文件夹下的文件 |
||||
* filename /resources/ip4enc.properties |
||||
*/ |
||||
public static InputStream getResourcesFile(String filename){ |
||||
return ResourceIOUtils.read(filename); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param res |
||||
* @param path /com/fr/plugin/loginAuth/html/getMac.html |
||||
* @param parameterMap |
||||
*/ |
||||
public static void toErrorPage(HttpServletResponse res,String path,Map<String, String> parameterMap){ |
||||
if(parameterMap == null){ |
||||
parameterMap = new HashMap<String, String>(); |
||||
} |
||||
|
||||
try { |
||||
String macPage = TemplateUtils.renderTemplate(path, parameterMap); |
||||
WebUtils.printAsString(res, macPage); |
||||
}catch (Exception e){ |
||||
FRUtils.FRLogError("跳转页面异常"); |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 判断是否是管理员 |
||||
* @param username |
||||
* @return |
||||
*/ |
||||
public static boolean isAdmin(String username) throws Exception{ |
||||
return UserService.getInstance().isAdmin(UserService.getInstance().getUserByUserName(username).getId()); |
||||
} |
||||
|
||||
/** |
||||
* 去掉浏览器中的参数 |
||||
* @param url |
||||
* @param param |
||||
* @return |
||||
*/ |
||||
public static String removeParam(String url,String param){ |
||||
if(!url.contains("?"+param) && !url.contains("&"+param)){ |
||||
return url; |
||||
} |
||||
|
||||
return url.substring(0,url.indexOf(url.contains("?"+param) ? "?"+param : "&"+param)); |
||||
} |
||||
|
||||
/** |
||||
* 获取跳转链接 |
||||
* @param req |
||||
* @param param |
||||
* @return |
||||
*/ |
||||
public static String getRedirectUrl(HttpServletRequest req,String param){ |
||||
String url = FRUtils.getAllUrl(req); |
||||
|
||||
if(isNotNullStr(param)){ |
||||
url = removeParam(url,param); |
||||
} |
||||
|
||||
url = encodeCH(url); |
||||
|
||||
return url; |
||||
} |
||||
|
||||
/** |
||||
* 去除空格换行 |
||||
* @param str |
||||
* @return |
||||
*/ |
||||
public static String trim(String str){ |
||||
return str.trim().replaceAll("\n","").replaceAll("\r",""); |
||||
} |
||||
|
||||
/** |
||||
* list 转化为指定字符分割的字符串 |
||||
* @param list |
||||
* @param list |
||||
* @return |
||||
*/ |
||||
public static String listToStr(List<String> list, String split){ |
||||
String result = ""; |
||||
|
||||
if(list == null || list.size() <= 0){ |
||||
return result; |
||||
} |
||||
|
||||
for(String str : list){ |
||||
result+=","+str; |
||||
} |
||||
|
||||
result = result.substring(1); |
||||
|
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* array 转化为指定字符分割的字符串 |
||||
* @param list |
||||
* @param list |
||||
* @return |
||||
*/ |
||||
public static String arrayToStr(String[] list, String split){ |
||||
String result = ""; |
||||
|
||||
if(list == null ||list.length <= 0){ |
||||
return result; |
||||
} |
||||
|
||||
for(int i=0;i<list.length;i++){ |
||||
String str = list[i]; |
||||
result+=","+str; |
||||
} |
||||
|
||||
result = result.substring(1); |
||||
|
||||
return result; |
||||
} |
||||
} |
Loading…
Reference in new issue