webservice连接验证用户名密码

本文介绍了如何在服务器端通过代码实现WebService接口,用于验证用户名和密码。详细步骤包括配置web.xml,添加tomcat-users.xml用户信息,以及进行客户端测试。通过对这些步骤的实施,可以实现基于WebService的安全身份验证。
摘要由CSDN通过智能技术生成

1.服务器端代码

@WebService
public class CalculateService {

	
	public float plus(float x,float y) {
		return x + y ;
	}
	
	public float minus(float x,float y) {
		return x - y;
	}
	
	public float multiply(float x,float y) {
		return x * y;
	}
	
	public float divide(float x,float y) {
		return x / y;
	}
}

2.服务器web.xml添加

<security-role>
  <description>Normal operator user</description>
  <role-name>WsOperator</role-name>
</security-role>
<security-constraint>
  <web-resource-collection>
    <web-resource-name>Operator Roles Security</web-resource-name>
    <url-pattern>/service/CalculateService</url-pattern>
  </web-resource-collection>
  <auth-constraint>
    <role-name>WsOperator</role-name>
  </auth-constraint>
  <user-data-constraint>
    <transport-guarantee>NONE</transport-guarantee>
  </user-data-constraint>
</security-constraint>
<login-config>
  <auth-method>BASIC</auth-method>
</login-config>

3.tomcat-users.xml添加

<role rolename="WsOperator"/>
	<user username="tomcat" password="123456" roles="WsOperator"/>

4.客户端测试

 

public class Test {

	
	public static void main(String[] args) {
	
		System.out.println("webservice¿ªÊ¼");
		Service service = new Service();
		try {
			Call call=(Call)service.createCall();
			call.setTargetEndpointAddress("http://localhost:8080/helloService/services/CalculateService?wsdl");
			call.setOperationName(new QName("plus"));
			call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
			call.addParameter("a",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.INOUT);
			call.addParameter("b",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
			call.setProperty(call.USERNAME_PROPERTY, "tomcat");
			call.setProperty(call.PASSWORD_PROPERTY, "123456");
			float x = 4;
			float y = 5;
			try {
				String result=(String)call.invoke(new Object[] {x,y});
				System.out.println(result);
				System.out.println(" this is " + call.getOutputParams()+call.getProperty("x")+ call.USERNAME_PROPERTY);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值