本文参考自:J2EE Web服务开发系列之十二: 实现安全的AXIS Web服务,第1部分
?
package com.hellking.study.webservice; public class PersonalTaxService { final double base = 3500;// 所得税上缴基数。 public double getTax(double salary) { double tax_salary = salary - base; double tax = 0.0d;// 计算后的所得税。 if (0 > tax_salary) tax = 0; else if (0 < tax_salary && tax_salary <= 1500) tax = tax_salary * 0.03 - 0; else if (1500 < tax_salary && tax_salary <= 4500) tax = tax_salary * 0.10 - 25; return tax; } }
?
布署web 服务
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="PersonalTaxService" provider="java:RPC"> <parameter name="allowedMethods" value="*"/> <parameter name="className" value="com.hellking.study.webservice.PersonalTaxService"/> </service> </deployment>
?
执行命令:
java -classpath %CLASSPATH% org.apache.axis.client.AdminClient -l http://localhost:8080/axis/services/PersonalTaxService D:/workspace-java/axis1/web/WEB-INF/deploy_auth_basic.wsdd
?
编辑%TOMCAT_HOME%/conf/tomcat-users.xml ,添加用户角色和用户:
<role rolename="department-manager"/> <user username="hellking" password="simplewebservices" roles="department-manager"/>
?编辑/axis1/web/WEB-INF/web.xml,在最后添加上:
<security-constraint> <web-resource-collection> <web-resource-name>Tax Web service</web-resource-name> <url-pattern>/services/PersonalTaxService</url-pattern> </web-resource-collection> <auth-constraint> <role-name>department-manager</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name>Axis Basic Authentication Area</realm-name> </login-config> <security-role> <role-name>department-manager</role-name> </security-role>
?
最后编写客户端代码:
package com.hellking.study.webservice; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; /** * 调用需要验证的Web服务 */ public class AuthClient { static final double salary = 50000; public static void main(String[] args) { try { // 服务端的url,需要根据情况更改。 String endpointURL = "http://localhost:8080/axis/services/PersonalTaxService";// Web服务端点地址 Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpointURL)); call.setOperationName(new QName("PersonalTaxService", "getTax"));// 设置操作的名称。 // 由于需要认证,故需要设置调用的用户名和密码。 call.getMessageContext().setUsername("hellking");// 设置用户名。 call.getMessageContext().setPassword("simplewebservices");// 设置密码 call.addParameter("op1", XMLType.XSD_DOUBLE, ParameterMode.IN);// 参数的类型 call.setReturnType(XMLType.XSD_DOUBLE);// 返回的数据类型 Double ret = (Double) call.invoke(new Object[] { new Double(salary) });// 执行调用 System.out.println("使用HTTP协议来作为Web服务的传输协议!"); System.out.println("已经成功调用。请参看服务端的输出!"); System.out.println("输入工资" + salary + "元,应交个人所得税:" + ret); } catch (Exception e) { e.printStackTrace(); } } }
?
测试:
???? 1、访问:http://localhost:8080/axis/services,将列出web服务列表
点击PersonalTaxService 的wsdl链接地址,验证一下会不会提示要输入用户名和密码。
?
?