webservice 查询时校验用户名及其IP

该博客介绍了一个Webservice调用时的认证处理类,通过AbstractHandler扩展实现。内容涉及检查请求头中的AuthenticationToken,从中获取用户名和密码进行验证。使用ResourceBundle读取配置的合法用户名和密码,同时验证请求的IP是否在允许的范围内,防止非法访问。如果验证失败,会抛出XFireFault异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

public class AuthenticationHandler extends AbstractHandler {
	public static ResourceBundle resourceBundle;
	static {
	        resourceBundle = ResourceBundle.getBundle("sysParam");
	}
	public void invoke(MessageContext cfx) throws Exception {
		if (cfx.getInMessage().getHeader() == null) {
			throw new org.codehaus.xfire.fault.XFireFault("请求必须包含验证信息",
					org.codehaus.xfire.fault.XFireFault.SENDER);
		}
		Element token = cfx.getInMessage().getHeader().getChild(
				"AuthenticationToken");
		
		if (token == null) {
			throw new org.codehaus.xfire.fault.XFireFault("请求必须包含身份验证信息",
					org.codehaus.xfire.fault.XFireFault.SENDER);
		} 
		String check_usernames = resourceBundle.getString("username");
		HashMap currentUser = new HashMap();
 		String check_userName [] = check_usernames.split(",");
 		for(String _s : check_userName){
 			currentUser.put(_s, resourceBundle.getString(_s+"_psw"));
 		}
		
		String username = token.getChild("Username").getValue();
		String password = token.getChild("Password").getValue();
		
 
		try { 
			
			if(!clsStringTool.isEmpty(currentUser.get(username))){
				if(currentUser.get(username).equals(password)){
					//System.out.println("用户名密码验证通过");
					String ip = XFireServletController.getRequest().getHeader("remoteIp");
			        if( ip == null)
			        	ip = XFireServletController.getRequest().getRemoteAddr();
					
			        //System.out.println("ip==="+ip);
			        if(resourceBundle.getString("ip").contains(ip)){
						//System.out.println("用户IP验证通过");
					}else{
						throw new org.codehaus.xfire.fault.XFireFault("非法的IP来源",
								org.codehaus.xfire.fault.XFireFault.SENDER);  
					}
					
				}else{
					throw new Exception();
				}
			}else{
				throw new Exception();
			}
		} catch (Exception e) {
			throw new org.codehaus.xfire.fault.XFireFault("非法的用户名、密码或IP",
					org.codehaus.xfire.fault.XFireFault.SENDER);
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值