欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

webservice连接验证用户名密码

程序员文章站 2022-03-04 13:33:57
...

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();
		}
	}
}
相关标签: 接口

上一篇: 对接小程序接口

下一篇: 接头