commit
5fcf87058d
15 changed files with 503 additions and 0 deletions
@ -0,0 +1,8 @@ |
|||||||
|
|
||||||
|
# open-JSD- |
||||||
|
9853 |
||||||
|
|
||||||
|
JSD-9934 记录请求访问的mac地址\ |
||||||
|
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||||
|
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||||
|
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系【pioneer】处理。 |
Binary file not shown.
@ -0,0 +1,31 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||||||
|
<plugin> |
||||||
|
<id>com.fr.plugin.sln3425</id> |
||||||
|
<name><![CDATA[记录mac地址-新]]></name> |
||||||
|
<active>yes</active> |
||||||
|
<version>1.6.3</version> |
||||||
|
<env-version>10.0</env-version> |
||||||
|
<jartime>2019-03-10</jartime> |
||||||
|
<vendor>fr.open</vendor> |
||||||
|
<description><![CDATA[记录mac地址]]></description> |
||||||
|
<change-notes> |
||||||
|
<![CDATA[ |
||||||
|
<p>[2019-6-4]项目启动</p> |
||||||
|
<p>[2019-6-14]新增deviceName</p> |
||||||
|
<p>[2022-03-18]修改拦截点 |
||||||
|
</p> |
||||||
|
]]> |
||||||
|
</change-notes> |
||||||
|
<main-package>com.fr.plugin</main-package> |
||||||
|
|
||||||
|
<function-recorder class="com.fr.plugin.LogMacFunction"/> |
||||||
|
<extra-decision> |
||||||
|
<GlobalRequestFilterProvider class="com.fr.plugin.LogMacFilter"/> |
||||||
|
<HttpHandlerProvider class="com.fr.plugin.AllOutHander"/> |
||||||
|
</extra-decision> |
||||||
|
<extra-core> |
||||||
|
<!--插件注入数据库--> |
||||||
|
<DBAccessProvider class="com.fr.plugin.LogMacDBAccess"/> |
||||||
|
</extra-core> |
||||||
|
</plugin> |
||||||
|
|
@ -0,0 +1,12 @@ |
|||||||
|
package com.fr.plugin; |
||||||
|
|
||||||
|
import com.fr.decision.fun.HttpHandler; |
||||||
|
import com.fr.decision.fun.impl.AbstractHttpHandlerProvider; |
||||||
|
|
||||||
|
public class AllOutHander extends AbstractHttpHandlerProvider { |
||||||
|
@Override |
||||||
|
public HttpHandler[] registerHandlers() { |
||||||
|
return new HttpHandler[]{ |
||||||
|
}; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,45 @@ |
|||||||
|
package com.fr.plugin; |
||||||
|
|
||||||
|
import com.fr.plugin.dao.LogDao; |
||||||
|
import com.fr.plugin.dao.RelationLogDao; |
||||||
|
import com.fr.plugin.db.AbstractDBAccessProvider; |
||||||
|
import com.fr.plugin.entitys.AccessLogEntity; |
||||||
|
import com.fr.plugin.entitys.RelationLogEntity; |
||||||
|
import com.fr.stable.db.accessor.DBAccessor; |
||||||
|
import com.fr.stable.db.dao.BaseDAO; |
||||||
|
import com.fr.stable.db.dao.DAOProvider; |
||||||
|
|
||||||
|
public class LogMacDBAccess extends AbstractDBAccessProvider { |
||||||
|
private static DBAccessor accessor; |
||||||
|
public static DBAccessor getAccessor() { |
||||||
|
return accessor; |
||||||
|
} |
||||||
|
@Override |
||||||
|
public DAOProvider[] registerDAO() { |
||||||
|
return new DAOProvider[]{ |
||||||
|
new DAOProvider() { |
||||||
|
public Class getEntityClass() { |
||||||
|
return RelationLogEntity.class; |
||||||
|
} |
||||||
|
|
||||||
|
public Class<? extends BaseDAO> getDAOClass() { |
||||||
|
return LogDao.class; |
||||||
|
} |
||||||
|
}, |
||||||
|
new DAOProvider() { |
||||||
|
public Class getEntityClass() { |
||||||
|
return AccessLogEntity.class; |
||||||
|
} |
||||||
|
|
||||||
|
public Class<? extends BaseDAO> getDAOClass() { |
||||||
|
return RelationLogDao.class; |
||||||
|
} |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onDBAvailable(DBAccessor dbAccessor) { |
||||||
|
accessor = dbAccessor; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,78 @@ |
|||||||
|
package com.fr.plugin; |
||||||
|
|
||||||
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||||
|
import com.fr.decision.webservice.v10.login.LoginService; |
||||||
|
import com.fr.json.JSONObject; |
||||||
|
import com.fr.log.FineLoggerFactory; |
||||||
|
import com.fr.plugin.beans.UserLogBean; |
||||||
|
import com.fr.plugin.entitys.RelationLogEntity; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
|
||||||
|
import javax.servlet.FilterChain; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
import java.util.stream.Collectors; |
||||||
|
|
||||||
|
public class LogMacFilter extends AbstractGlobalRequestFilterProvider { |
||||||
|
@Override |
||||||
|
public String filterName() { |
||||||
|
return "logMac"; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String[] urlPatterns() { |
||||||
|
return new String[]{ |
||||||
|
"/decision/login", |
||||||
|
"/decision/v10/entry/access/*" |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) { |
||||||
|
try { |
||||||
|
MultiReadHttpServletRequest request = new MultiReadHttpServletRequest(req); |
||||||
|
LogMacFunction myFunction = new LogMacFunction(); |
||||||
|
String uri = req.getRequestURI(); |
||||||
|
if (uri.contains("v10/entry/access")) { |
||||||
|
String terminal = req.getHeader("terminal"); |
||||||
|
String op = req.getParameter("op"); |
||||||
|
String cmd = req.getParameter("cmd"); |
||||||
|
if ("APP".equals(terminal) && StringUtils.equals(op, "fs_main") && StringUtils.equals(cmd, "entry_report")) { |
||||||
|
String name = LoginService.getInstance().getCurrentUserNameFromRequest(request); |
||||||
|
RelationLogEntity relation = myFunction.findRelation(name); |
||||||
|
if (relation != null) { |
||||||
|
myFunction.addLoginHistory(relation); |
||||||
|
FineLoggerFactory.getLogger().info("login app access log success " + relation.getUserName() + " " + relation.getMacAddr()); |
||||||
|
} else { |
||||||
|
FineLoggerFactory.getLogger().info("login not find mac relation"); |
||||||
|
} |
||||||
|
} |
||||||
|
} else { |
||||||
|
if ("POST".equalsIgnoreCase(req.getMethod())) { |
||||||
|
String terminal = req.getHeader("terminal"); |
||||||
|
if ("APP".equals(terminal)) { |
||||||
|
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); |
||||||
|
JSONObject json = new JSONObject(body); |
||||||
|
UserLogBean bean = new UserLogBean(); |
||||||
|
if (json.has("username")) { |
||||||
|
bean.setName(json.getString("username")); |
||||||
|
} |
||||||
|
if (json.has("deviceName")) { |
||||||
|
bean.setDeviceName(json.getString("deviceName")); |
||||||
|
} |
||||||
|
if (json.has("macAddress")) { |
||||||
|
bean.setMac(json.getString("macAddress")); |
||||||
|
} |
||||||
|
|
||||||
|
myFunction.save(bean); |
||||||
|
} |
||||||
|
FineLoggerFactory.getLogger().info("login with" + req.getHeaderNames()); |
||||||
|
} |
||||||
|
} |
||||||
|
filterChain.doFilter(request, res); |
||||||
|
} catch (Exception e) { |
||||||
|
e.printStackTrace(); |
||||||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,87 @@ |
|||||||
|
package com.fr.plugin; |
||||||
|
|
||||||
|
import com.fr.plugin.beans.UserLogBean; |
||||||
|
import com.fr.plugin.dao.LogDao; |
||||||
|
import com.fr.plugin.dao.RelationLogDao; |
||||||
|
import com.fr.plugin.entitys.AccessLogEntity; |
||||||
|
import com.fr.plugin.entitys.RelationLogEntity; |
||||||
|
import com.fr.plugin.transform.ExecuteFunctionRecord; |
||||||
|
import com.fr.plugin.transform.FunctionRecorder; |
||||||
|
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.query.QueryFactory; |
||||||
|
import com.fr.stable.query.condition.QueryCondition; |
||||||
|
import com.fr.stable.query.restriction.RestrictionFactory; |
||||||
|
import com.fr.third.org.apache.commons.lang3.time.DateFormatUtils; |
||||||
|
|
||||||
|
import java.util.Date; |
||||||
|
import java.util.List; |
||||||
|
import java.util.UUID; |
||||||
|
|
||||||
|
@FunctionRecorder(localeKey = "logmac") |
||||||
|
public class LogMacFunction { |
||||||
|
@ExecuteFunctionRecord |
||||||
|
public void save(UserLogBean bean) throws Exception { |
||||||
|
DBAccessor accessor = LogMacDBAccess.getAccessor(); |
||||||
|
RelationLogEntity logEntity = new RelationLogEntity(); |
||||||
|
logEntity.setTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")); |
||||||
|
logEntity.setMacAddr(bean.getMac()); |
||||||
|
logEntity.setUserName(bean.getName()); |
||||||
|
logEntity.setDeviceName(bean.getDeviceName()); |
||||||
|
logEntity.setId(UUID.randomUUID().toString()); |
||||||
|
final RelationLogEntity count = accessor.runQueryAction(new DBAction<RelationLogEntity>() { |
||||||
|
public RelationLogEntity run(DAOContext content) throws Exception { |
||||||
|
QueryCondition queryCondition = QueryFactory.create(); |
||||||
|
queryCondition.addRestriction(RestrictionFactory.eq("userName", logEntity.getUserName())); |
||||||
|
return content.getDAO(RelationLogDao.class).findOne(queryCondition); |
||||||
|
} |
||||||
|
}); |
||||||
|
accessor.runDMLAction(new DBAction<RelationLogEntity>() { |
||||||
|
public RelationLogEntity run(DAOContext content) throws Exception { |
||||||
|
if (count != null) { |
||||||
|
count.setMacAddr(bean.getMac()); |
||||||
|
count.setDeviceName(bean.getDeviceName()); |
||||||
|
content.getDAO(RelationLogDao.class).update(count); |
||||||
|
} else { |
||||||
|
content.getDAO(RelationLogDao.class).add(logEntity); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
public RelationLogEntity findRelation(String name) throws Exception { |
||||||
|
DBAccessor accessor = LogMacDBAccess.getAccessor(); |
||||||
|
return accessor.runQueryAction(new DBAction<RelationLogEntity>() { |
||||||
|
public RelationLogEntity run(DAOContext content) throws Exception { |
||||||
|
QueryCondition queryCondition = QueryFactory.create(); |
||||||
|
queryCondition.addRestriction(RestrictionFactory.eq("userName", name)); |
||||||
|
return content.getDAO(RelationLogDao.class).findOne(queryCondition); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
public void addLoginHistory(RelationLogEntity relation) throws Exception { |
||||||
|
AccessLogEntity logEntity = new AccessLogEntity(); |
||||||
|
logEntity.setMacAddr(relation.getMacAddr()); |
||||||
|
logEntity.setUserName(relation.getUserName()); |
||||||
|
logEntity.setDeviceName(relation.getDeviceName()); |
||||||
|
logEntity.setId(UUID.randomUUID().toString()); |
||||||
|
logEntity.setTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd")); |
||||||
|
DBAccessor accessor = LogMacDBAccess.getAccessor(); |
||||||
|
accessor.runDMLAction(new DBAction<RelationLogEntity>() { |
||||||
|
public RelationLogEntity run(DAOContext content) throws Exception { |
||||||
|
LogDao dao = content.getDAO(LogDao.class); |
||||||
|
QueryCondition queryCondition = QueryFactory.create(); |
||||||
|
queryCondition.addRestriction(RestrictionFactory.eq("time", logEntity.getTime())).addRestriction(RestrictionFactory.eq("macAddr", logEntity.getMacAddr())).addRestriction(RestrictionFactory.eq("userName", logEntity.getUserName())); |
||||||
|
List<AccessLogEntity> accessLogEntities = dao.find(queryCondition); |
||||||
|
//如果不存在则插入,保障一天,一个用户,一个MAC只插入1条
|
||||||
|
if (accessLogEntities.isEmpty()) { |
||||||
|
dao.add(logEntity); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,60 @@ |
|||||||
|
package com.fr.plugin; |
||||||
|
|
||||||
|
import javax.servlet.ReadListener; |
||||||
|
import javax.servlet.ServletInputStream; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletRequestWrapper; |
||||||
|
import java.io.BufferedReader; |
||||||
|
import java.io.ByteArrayInputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStreamReader; |
||||||
|
|
||||||
|
/** |
||||||
|
* 二次封装请求,避免获取了body之后,无法继续处理请求 |
||||||
|
*/ |
||||||
|
public class MultiReadHttpServletRequest extends HttpServletRequestWrapper { |
||||||
|
public String _body; |
||||||
|
|
||||||
|
public MultiReadHttpServletRequest(HttpServletRequest request) throws IOException { |
||||||
|
super(request); |
||||||
|
StringBuffer sBuffer = new StringBuffer(); |
||||||
|
BufferedReader bufferedReader = request.getReader(); |
||||||
|
String line; |
||||||
|
while ((line = bufferedReader.readLine()) != null) { |
||||||
|
sBuffer.append(line); |
||||||
|
} |
||||||
|
_body = sBuffer.toString(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public ServletInputStream getInputStream() { |
||||||
|
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_body.getBytes()); |
||||||
|
return new ServletInputStream() { |
||||||
|
@Override |
||||||
|
public int read() { |
||||||
|
return byteArrayInputStream.read(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isFinished() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isReady() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setReadListener(ReadListener listener) { |
||||||
|
|
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public BufferedReader getReader() { |
||||||
|
return new BufferedReader(new InputStreamReader(this.getInputStream())); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,32 @@ |
|||||||
|
package com.fr.plugin.beans; |
||||||
|
|
||||||
|
import java.io.Serializable; |
||||||
|
|
||||||
|
public class UserLogBean implements Serializable { |
||||||
|
private String name; |
||||||
|
private String mac; |
||||||
|
private String deviceName; |
||||||
|
public String getName() { |
||||||
|
return name; |
||||||
|
} |
||||||
|
|
||||||
|
public void setName(String name) { |
||||||
|
this.name = name; |
||||||
|
} |
||||||
|
|
||||||
|
public String getMac() { |
||||||
|
return mac; |
||||||
|
} |
||||||
|
|
||||||
|
public void setMac(String mac) { |
||||||
|
this.mac = mac; |
||||||
|
} |
||||||
|
|
||||||
|
public String getDeviceName() { |
||||||
|
return deviceName; |
||||||
|
} |
||||||
|
|
||||||
|
public void setDeviceName(String deviceName) { |
||||||
|
this.deviceName = deviceName; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,15 @@ |
|||||||
|
package com.fr.plugin.dao; |
||||||
|
|
||||||
|
import com.fr.plugin.entitys.AccessLogEntity; |
||||||
|
import com.fr.stable.db.dao.BaseDAO; |
||||||
|
import com.fr.stable.db.session.DAOSession; |
||||||
|
|
||||||
|
public class LogDao extends BaseDAO<AccessLogEntity> { |
||||||
|
public LogDao(DAOSession daoSession) { |
||||||
|
super(daoSession); |
||||||
|
} |
||||||
|
|
||||||
|
protected Class<AccessLogEntity> getEntityClass() { |
||||||
|
return AccessLogEntity.class; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,15 @@ |
|||||||
|
package com.fr.plugin.dao; |
||||||
|
|
||||||
|
import com.fr.plugin.entitys.RelationLogEntity; |
||||||
|
import com.fr.stable.db.dao.BaseDAO; |
||||||
|
import com.fr.stable.db.session.DAOSession; |
||||||
|
|
||||||
|
public class RelationLogDao extends BaseDAO<RelationLogEntity> { |
||||||
|
public RelationLogDao(DAOSession daoSession) { |
||||||
|
super(daoSession); |
||||||
|
} |
||||||
|
|
||||||
|
protected Class<RelationLogEntity> getEntityClass() { |
||||||
|
return RelationLogEntity.class; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,59 @@ |
|||||||
|
package com.fr.plugin.entitys; |
||||||
|
|
||||||
|
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; |
||||||
|
|
||||||
|
@Entity |
||||||
|
@Table( |
||||||
|
name = "fine_access_log" |
||||||
|
) |
||||||
|
@TableAssociation( |
||||||
|
associated = true |
||||||
|
) |
||||||
|
public class AccessLogEntity extends BaseEntity { |
||||||
|
@Column(name = "userName") |
||||||
|
private String userName = ""; |
||||||
|
@Column(name = "macAddr") |
||||||
|
private String macAddr = ""; |
||||||
|
@Column(name = "deviceName") |
||||||
|
private String deviceName = ""; |
||||||
|
@Column(name = "createTime") |
||||||
|
private String time = ""; |
||||||
|
|
||||||
|
public String getDeviceName() { |
||||||
|
return deviceName; |
||||||
|
} |
||||||
|
|
||||||
|
public void setDeviceName(String deviceName) { |
||||||
|
this.deviceName = deviceName; |
||||||
|
} |
||||||
|
|
||||||
|
public String getUserName() { |
||||||
|
return userName; |
||||||
|
} |
||||||
|
|
||||||
|
public void setUserName(String userName) { |
||||||
|
this.userName = userName; |
||||||
|
} |
||||||
|
|
||||||
|
public String getMacAddr() { |
||||||
|
return macAddr; |
||||||
|
} |
||||||
|
|
||||||
|
public void setMacAddr(String macAddr) { |
||||||
|
this.macAddr = macAddr; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public String getTime() { |
||||||
|
return time; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTime(String time) { |
||||||
|
this.time = time; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,59 @@ |
|||||||
|
package com.fr.plugin.entitys; |
||||||
|
|
||||||
|
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; |
||||||
|
|
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
@Entity |
||||||
|
@Table( |
||||||
|
name = "fine_user_mac_relation" |
||||||
|
) |
||||||
|
@TableAssociation( |
||||||
|
associated = true |
||||||
|
) |
||||||
|
public class RelationLogEntity extends BaseEntity { |
||||||
|
@Column(name = "userName") |
||||||
|
private String userName = ""; |
||||||
|
@Column(name = "macAddr") |
||||||
|
private String macAddr = ""; |
||||||
|
@Column(name = "deviceName") |
||||||
|
private String deviceName = ""; |
||||||
|
@Column(name = "createTime") |
||||||
|
private String time = ""; |
||||||
|
|
||||||
|
public String getUserName() { |
||||||
|
return userName; |
||||||
|
} |
||||||
|
|
||||||
|
public void setUserName(String userName) { |
||||||
|
this.userName = userName; |
||||||
|
} |
||||||
|
|
||||||
|
public String getMacAddr() { |
||||||
|
return macAddr; |
||||||
|
} |
||||||
|
|
||||||
|
public void setMacAddr(String macAddr) { |
||||||
|
this.macAddr = macAddr; |
||||||
|
} |
||||||
|
|
||||||
|
public String getDeviceName() { |
||||||
|
return deviceName; |
||||||
|
} |
||||||
|
|
||||||
|
public void setDeviceName(String deviceName) { |
||||||
|
this.deviceName = deviceName; |
||||||
|
} |
||||||
|
|
||||||
|
public String getTime() { |
||||||
|
return time; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTime(String time) { |
||||||
|
this.time = time; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1 @@ |
|||||||
|
Plugin-Test_Function_Abs=Test ABS |
Loading…
Reference in new issue