pom.xml
org.apache.cxf
cxf-api
2.7.15
org.apache.cxf
cxf-rt-frontend-jaxws
2.7.15
org.apache.cxf
cxf-rt-bindings-soap
2.7.15
org.apache.cxf
cxf-rt-transports-http
2.7.15
org.apache.cxf
cxf-rt-ws-security
2.7.15
org.springframework
spring-web
4.1.5.RELEASE
web.xml
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
/webservice/*
cxf-servlet.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
class="com.auto.webservice.servlet.IpAddressInInterceptor" />
接口类
WorkflowService.java
package com.auto.webservice.server;
import org.nutz.json.Json;
import javax.jws.WebService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by wizzer on 15-4-13.
*/
@WebService(targetNamespace = "http://server.webservice.auto.com/", name = "workflowServicePortType")
public interface WorkflowService {
@WebResult(name = "return")
@Action(input = "urn:start", output = "urn:startResponse")
@RequestWrapper(localName = "start")
@WebMethod(action = "urn:start")
@ResponseWrapper(localName = "startResponse")
String start(@WebParam(name = "flowKey") String flowKey, @WebParam(name = "userId") String userId);//启动流程
}
实现类
WorkflowServiceImpl.java
package com.auto.webservice.server;
import org.activiti.engine.*;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.StartFormData;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import javax.jws.WebService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by wizzer on 15-4-13.
*/
@IocBean(name = "workflowService")
@WebService
public class WorkflowServiceImpl implements WorkflowService {
private final Log log = Logs.get();
FormService formService=Mvcs.ctx().getDefaultIoc().get(FormService.class);
IdentityService identityService=Mvcs.ctx().getDefaultIoc().get(IdentityService.class);
RuntimeService runtimeService=Mvcs.ctx().getDefaultIoc().get(RuntimeService.class);
TaskService taskService=Mvcs.ctx().getDefaultIoc().get(TaskService.class);
RepositoryService repositoryService=Mvcs.ctx().getDefaultIoc().get(RepositoryService.class);
/**
* 启动一个流程
*
* @param flowKey 流程模型key
* @param userId 用户ID
* @return
*/
public String start(String flowKey, String userId) {
Map map = new HashMap();
try {
if (!Strings.isEmpty(userId)) {
identityService.setAuthenticatedUserId(userId);
}
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(flowKey);
map.put("errcode", 0);
map.put("errmsg", "");
map.put("processInstanceId", processInstance.getId());
} catch (Exception e) {
log.error("WebServcice启动流程出错", e);
map.put("errcode", 1);
map.put("errmsg", e.getMessage());
} finally {
identityService.setAuthenticatedUserId(null);
}
return Json.toJson(map, JsonFormat.compact());
}
}
/**
* IP地址拦截器
*
* @author Sunshine
*/
public class IpAddressInInterceptor extends AbstractPhaseInterceptor {
private final Log log = Logs.get();
public IpAddressInInterceptor() {
super(Phase.RECEIVE);
}
public void handleMessage(Message message) throws Fault {
HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
//获取所有的白名单访问主机
UserInfoServiceImpl userInfoService = Mvcs.ctx().getDefaultIoc().get(UserInfoServiceImpl.class);
List hostList = userInfoService.queryHostListByType("w");
// 取客户端IP地址
String ipAddress = request.getRemoteAddr();
// 如果允许访问的集合非空,继续处理,否则认为全部IP地址均合法
if (!hostList.isEmpty()) {
boolean contains = false;
for (ListInfo hostInfo : hostList) {
log.debug("hostInfo:" + hostInfo.getIpaddress() + "|ipAddress:" + ipAddress);
if (hostInfo.getIpaddress().equals(ipAddress)) {
contains = true;
break;
}
}
if (!contains) {
throw new Fault(new IllegalAccessException("IP address " + ipAddress + " is not allowed"));
}
}
}
}
2,243 total views, 2 views today