当前位置:编程问答 > JS >

cxf获取客户端Ip

最近这几天在做一个用户同步,具体就是当权限系统中添加用户后,把该用户的信息同步到论坛系统中去,服务器端发布webservice采用的是cxf框架,这时候一个业务需求就是能获取到客户端的ip,并将这字段存到数据库中,用于论坛统计。


package com.bkbt.forum.service;

import java.io.Reader;
import java.io.StringReader;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import cn.jsprun.dao.UserDao;
import cn.jsprun.domain.Members;
import cn.jsprun.utils.BeanFactory;
import cn.jsprun.utils.Common;
import cn.jsprun.utils.Md5Token;

 

@WebService
public class UserServiceImpl implements UserService {
private Log log = LogFactory.getLog(UserServiceImpl.class);
private UserDao userDao = (UserDao) BeanFactory.getBean("userDao");
@Resource(name = "org.apache.cxf.jaxws.context.WebServiceContextImpl")
private WebServiceContext context;

public void addUser(String xml) {
try {
SAXReader saxReader = new SAXReader();
Reader reader = new StringReader(xml);
Document doc = saxReader.read(reader);
Element root = doc.getRootElement();
List<Element> list = root.elements();
Members member = new Members();
int salt = Common.rand(100000, 999999);
member.setUsername(list.get(0).getText());
member.setPassword(Md5Token.getInstance().getLongToken(Md5Token.getInstance().getLongToken(list.get(1).getText())+salt));
member.setEmail(list.get(2).getText());
member.setSalt(String.valueOf(salt));
int groupId = 10;
short id = (short)groupId;
Short Id = id;
member.setGroupid(Id);  www.zzzyk.com
member.setRegip(this.getClientIpCxf());
member.setRegdate(Integer.valueOf(String.valueOf(new Date().getTime()).substring(0, 10)));
member.setLastip(this.getClientIpCxf());
member.setLastvisit(Integer.valueOf(String.valueOf(new Date().getTime()).substring(0, 10)));
member.setPmsound(Byte.valueOf("1"));
member.setShowemail(Byte.valueOf("1"));
member.setNewsletter(Byte.valueOf("1"));
member.setTimeoffset("9999");
member.setLastactivity(Integer.valueOf(String.valueOf(new Date().getTime()).substring(0, 10)));
userDao.saveMember(member);
} catch (Exception e) {

}
}
public String getClientIpCxf() {
MessageContext ctx = context.getMessageContext();
HttpServletRequest request = (HttpServletRequest)
ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
String ip=request.getRemoteAddr();
return ip;
}
}

作者:jianhuili
补充:Web开发 , Jsp ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,