211 lines
7.0 KiB
Java
211 lines
7.0 KiB
Java
package com.nis.web.service;
|
||
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.util.Collection;
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
import java.util.Set;
|
||
|
||
import javax.ws.rs.client.Invocation.Builder;
|
||
import javax.ws.rs.client.WebTarget;
|
||
import javax.ws.rs.core.MediaType;
|
||
import javax.ws.rs.core.Response;
|
||
|
||
import org.apache.axiom.om.OMAbstractFactory;
|
||
import org.apache.axiom.om.OMElement;
|
||
import org.apache.axiom.om.OMFactory;
|
||
import org.apache.axiom.om.impl.OMNamespaceImpl;
|
||
import org.apache.commons.io.FileUtils;
|
||
import org.apache.shiro.session.Session;
|
||
import org.crazycake.shiro.exception.SerializationException;
|
||
import org.crazycake.shiro.serializer.ObjectSerializer;
|
||
import org.crazycake.shiro.serializer.RedisSerializer;
|
||
import org.crazycake.shiro.serializer.StringSerializer;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Service;
|
||
|
||
import com.nis.domain.SrcIp;
|
||
import com.nis.domain.SysRole;
|
||
import com.nis.domain.SysUser;
|
||
import com.nis.util.Configurations;
|
||
import com.nis.util.Constants;
|
||
import com.nis.util.DateUtils;
|
||
import com.nis.util.IpUtil;
|
||
import com.nis.util.ServiceConfigTemplateUtil;
|
||
import com.nis.util.TimeConstants;
|
||
import com.nis.util.httpclient.ClientUtil;
|
||
import com.nis.web.dao.SchedulerDao;
|
||
import com.nis.web.dao.SrcIpDao;
|
||
import com.nis.web.dao.SysOfficeDao;
|
||
import com.nis.web.dao.UserDao;
|
||
import com.nis.web.dao.configuration.CommonPolicyDao;
|
||
import com.nis.web.security.CacheSessionDAO;
|
||
import com.nis.web.security.Servlets;
|
||
import com.nis.web.security.UserUtils;
|
||
import com.nis.web.service.basics.AsnGroupInfoService;
|
||
|
||
@Service
|
||
public class SystemService extends BaseService{
|
||
|
||
@Autowired
|
||
private CacheSessionDAO sessionDao;
|
||
/*private SessionDAO sessionDao;*/
|
||
|
||
@Autowired
|
||
private UserDao userDao;
|
||
|
||
@Autowired
|
||
private SysOfficeDao sysOfficeDao;
|
||
|
||
@Autowired
|
||
private AsnGroupInfoService asnGroupInfoService;
|
||
|
||
@Autowired
|
||
private CommonPolicyDao commonPolicyDao;
|
||
|
||
@Autowired
|
||
private SchedulerDao schedulerDao;
|
||
|
||
@Autowired
|
||
private SrcIpDao srcIpDao;
|
||
private RedisSerializer keySerializer = new StringSerializer();
|
||
private RedisSerializer valueSerializer = new ObjectSerializer();
|
||
|
||
public Collection<Session> getActiveSessions(boolean includeLeave) {
|
||
return sessionDao.getActiveSessions(includeLeave);
|
||
}
|
||
|
||
|
||
public Collection<Session> getActiveSessions(boolean includeLeave, Object principal, Session filterSession) {
|
||
return sessionDao.getActiveSessions(includeLeave, principal, filterSession);
|
||
}
|
||
|
||
public void deleteSession(Session session) {
|
||
sessionDao.delete(session);
|
||
}
|
||
|
||
public void deleteAuthenticationCache(String authenticationInfo) {
|
||
if (org.apache.commons.lang3.StringUtils.isBlank(authenticationInfo)) {
|
||
logger.error("AuthenticationInfo is null");
|
||
return;
|
||
}
|
||
try {
|
||
sessionDao.getRedisManager().del(keySerializer.serialize(authenticationInfo));
|
||
} catch (SerializationException e) {
|
||
logger.error("delete AuthenticationInfo error. AuthenticationInfo key=" + authenticationInfo);
|
||
}
|
||
}
|
||
|
||
public SysUser getUserByLoginName(String loginName) {
|
||
return UserUtils.getByLoginName(loginName);
|
||
}
|
||
|
||
public List<SrcIp> ipLookUp(String ip) {
|
||
return srcIpDao.getIpInfo(IpUtil.getIpHostDesimal(ip));
|
||
}
|
||
|
||
|
||
public SysUser assignUserToRole(SysRole role, SysUser user) {
|
||
if (user == null){
|
||
return null;
|
||
}
|
||
List<Long> roleIds = user.getRoleIdList();
|
||
if (roleIds.contains(role.getId())) {
|
||
return null;
|
||
}
|
||
user.getUserRoleList().clear();
|
||
user.getUserRoleList().add(role);
|
||
userDao.insertUserRole(user);
|
||
UserUtils.clearCache(user);
|
||
return user;
|
||
}
|
||
|
||
public Boolean outUserInRole(SysRole role, SysUser user) {
|
||
List<SysRole> roles = user.getUserRoleList();
|
||
for (SysRole e : roles){
|
||
if (e.getId().equals(role.getId())){
|
||
roles.remove(e);
|
||
userDao.removeUserInRole(user.getId(),role.getId());
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public boolean officeIsValid(Long officeId, Long companyId) {
|
||
return userDao.officeIsExistOfCompany(officeId, companyId) >0 ? true : false;
|
||
}
|
||
|
||
/**
|
||
* 结果信息存入文件
|
||
* @param result
|
||
* @throws IOException
|
||
*/
|
||
private void saveToFile(String prefixName, String result) throws IOException{
|
||
String flieName = prefixName + DateUtils.formatDate(new Date(), TimeConstants.YYYYMMDDHH24MMSS);
|
||
String filePath = Servlets.getRequest().getServletContext().getRealPath(Configurations.getStringProperty("userfiles.basedir", "")) + File.separator
|
||
+ "upload" + File.separator + flieName + ".txt";
|
||
FileUtils.writeStringToFile(new File(filePath), result, false);
|
||
|
||
}
|
||
|
||
/**
|
||
* 新增设置header信息,需要修改里面rid、sid对应的值、sid为服务唯一标识、 rid为请求者唯一标识
|
||
*/
|
||
private static OMElement setHeader(String ns, String rid, String sid) {
|
||
OMFactory fac = OMAbstractFactory.getOMFactory();
|
||
// OMNamespace指定此SOAP文档名称空间。
|
||
OMNamespaceImpl omNs = (OMNamespaceImpl) fac.createOMNamespace(ns, "ns1");
|
||
// 创建header元素,并指定其在omNs指代的名称空间中,header名称固定为CyberpoliceSBReqHeader。
|
||
OMElement method = fac.createOMElement("CyberpoliceSBReqHeader", omNs);
|
||
|
||
// 指定元素的文本内容。
|
||
OMElement ridE = fac.createOMElement("rid", omNs);
|
||
// TODO将下面的值修改为请求者在系统中的唯一标识
|
||
ridE.setText(rid);
|
||
method.addChild(ridE);
|
||
|
||
OMElement sidE = fac.createOMElement("sid", omNs);
|
||
// TODO将下面的值修改要请求服务的唯一标识
|
||
sidE.setText(sid);
|
||
method.addChild(sidE);
|
||
|
||
OMElement timeoutE = fac.createOMElement("timeout", omNs);
|
||
// TODO将下面的值修改为请求的超时时间,单位秒
|
||
timeoutE.setText(Configurations.getStringProperty("webservice.request.timeout", "60"));
|
||
method.addChild(timeoutE);
|
||
|
||
OMElement secE = fac.createOMElement("sec", omNs);
|
||
// TODO将下面的值修改为请求密码,如果使用其他加密方式,则根据要求统一修改即可
|
||
secE.setText("");
|
||
method.addChild(secE);
|
||
return method;
|
||
}
|
||
|
||
public void clearPolicies() throws Exception {
|
||
// 调用服务接口 告知flushAll
|
||
String url = Constants.SERVICE_URL + Constants.DEL_ALL_CFG;
|
||
// 创建连接
|
||
WebTarget wt = ClientUtil.getWebTarger(url);
|
||
// 获取响应结果
|
||
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
|
||
Response response = header.delete();
|
||
if(response.getStatus() == 200){
|
||
//String result= response.readEntity(String.class);
|
||
Set<String> tableNameSet = ServiceConfigTemplateUtil.getAllTableName();
|
||
tableNameSet.add("pxy_obj_spoofing_ip_pool");
|
||
for (String tableName : tableNameSet) {
|
||
commonPolicyDao.clearPolicies(tableName);
|
||
}
|
||
// 初始化分组
|
||
asnGroupInfoService.reLoadGroupInfo();
|
||
// 失效定时任务
|
||
schedulerDao.inValidAllSchedule();
|
||
}else {
|
||
throw new RuntimeException();
|
||
}
|
||
|
||
}
|
||
}
|