详见E:\学习文档子目录压缩\专项进阶,如ngnix,webservice,linux等\webservice\webservice springcxfservice3自定义标签及获得httpservletrquest
写发布接口类的接口
package com.xiang.iwebservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.spcxfwebsercie.pojo.CXFXmlMessage;
@WebService
publicinterfaceILoingWebservice {
@WebMethod
publicCXFXmlMessage login(@WebParam(name="deviceID")String deviceID,@WebParam(name="timestamp")String timestamp,@WebParam(name="signature")String signature);
}
如图响应数据 Result,Token应该封装成一个类,这个xml类型还必须和ILoingWebservice.java的响应类型CXFXmlMessage一致
发布的webserver接口类实现这个接口
package com.xiang.iplewebservice;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.servlet.ServletContext;
importjavax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import com.spcxfwebsercie.pojo.CXFXmlMessage;
import com.spcxfwebsercie.pojo.Message;
importcom.xiang.iwebservice.ILoingWebservice;
import com.xiang.util.MakeToken;
@WebService
public class ImplLoingWebservice implementsILoingWebservice {
privateList
publicImplLoingWebservice() {
//用户及密码map
messages=new ArrayList
Message one = newMessage("GPS001|caozuoyuan01");
messages.add(one);
}
@Resource
privateWebServiceContext context;
@Override
publicCXFXmlMessage login(String deviceID, String timestamp, String signature) {
MessageContext messageContext =context.getMessageContext();
HttpServletRequest request =(HttpServletRequest) messageContext.get(MessageContext.SERVLET_REQUEST);
//==之前不是自定义标签的
/* String token =null;
for (Message message : messages) {
if(!deviceID.equals(message.getDeviceID())){
thrownew RuntimeException("只开设备与设备操作员ID不正确");
}
if(!signature.equals(message.getSignature())){
thrownew RuntimeException("哥们你知道我的算法么,我是带签名的");
}
//制造token返回,token有效期为1小时,登陆时间存入application中
ServletContext application =request.getSession().getServletContext();
long currenttime = new Date().getTime();
application.setAttribute(deviceID+"logintime", currenttime);
token = MakeToken.getToken(deviceID);
}
returntoken;*/
//=================================
CXFXmlMessage xml= new CXFXmlMessage();
for (Message message : messages) {
String token=null;
if(!deviceID.equals(message.getDeviceID())){
xml.setResult(-1);
xml.setErrorMessage("只开设备与设备操作员ID不正确");
}
if(deviceID.equals(message.getDeviceID())&&!signature.equals(message.getSignature())){
xml.setResult(-2);
xml.setErrorMessage("哥们你知道我的算法么,我是带签名的");
}
ServletContext application =request.getSession().getServletContext();
if(deviceID.equals(message.getDeviceID())&&signature.equals(message.getSignature())){
//制造token返回,token有效期为1小时,登陆时间存入application中
long currenttime = new Date().getTime();
application.setAttribute(deviceID+"logintime", currenttime);
token = MakeToken.getToken(deviceID);
}else{
token="无法制造token";
}
xml.setToken(token);
//============================
}
returnxml;
}
}
CXF获得httpservletrequest
import javax.xml.ws.WebServiceContext;
发布接口