java webservice 用户验证 (服务端 + 客户端)

说明:在网上找了一堆 handler验证的东东,试验了一下,没成功。

现在换了一种方式:在 tomcat的配置文件中添加用户角色和用户信息

             然后在 webservice的项目配置文件中增加对应的角色。从而达到 用户验证的目的。

开发环境: MyEclipse 10, Tomcat 7

参考:http://www.fengfly.com/plus/view-210099-1.html

先晒一下效果图:

以下是具体操作过程:

一, Server端代码

二, Client端的代码 

三, Tomcat的配置文件 及 Server端的配置

四,修改Client端调用webservice的方法

================================================

一, Server端代码

1, 在 MyEclipse 10中 新建一WebService项目:WsAuthServer

1),  ITest.java

[java]  view plain copy
  1. package com.Server;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebService;  
  5. import javax.jws.soap.*;  
  6. import javax.jws.soap.SOAPBinding.Style;  
  7.   
  8. @WebService  
  9. @SOAPBinding(style = Style.RPC)  
  10. public interface ITest {  
  11.          @WebMethod  
  12.          public String SayHello(String name);  
  13. }  

2),  Test.java

[java]  view plain copy
  1. package com.Server;  
  2.   
  3. import javax.jws.WebService;  
  4.    
  5. @WebService(endpointInterface = "com.Server.ITest")  
  6. public class Test implements ITest {  
  7.       
  8.     @Override  
  9.     public String SayHello(String name) {  
  10.               return "Hello: " + name;  
  11.     }  
  12.       
  13. }  


3), 让MyEclipse 自动生成WebService

          注意:如果 是jax-ws方式创建的webservice, 必须 加入JAX-WS 相关的2个包。

    加入方法:项目名 右键 --> Build Path --> Add Libraies --> MyEclipse Libraries --> 拉到最下面 ,选中 JAX-WS的2个jar包

        

           如果 发布失败,检查 WebRoot\WEB-INF目录下的sun-jaxws.xml文件:

[java]  view plain copy
  1. <?xml version = "1.0"?>  
  2. <endpoints version="2.0"  
  3.     xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">  
  4.     <endpoint name="TestPort"  
  5.         implementation="com.Server.TestDelegate"  
  6.         url-pattern="/TestPort">  
  7.     </endpoint></endpoints>  

 

         如果发布成功,我们可以通过这个URL在浏览器中访问:http://localhost:8080/WsAuthServer/TestPort?wsdl

二, Client端的代码 

1,在MyEclipse 10 中 新建一个project: WsAuthClient

1), 新建一个 WebServiceClient, 把 刚才的webservice 通过url 方式引入 其中,按MyEclipse提示会自动生成一堆代码。

                  

2), 新建一个 test.java

             在 main方法中粘进如下代码:

[java]  view plain copy
  1. TestService service = new TestService();  
  2. ITest portType = service.getTestPort();  
  3. portType.sayHello("JASON");  

         运行,会得到:Result=>Hello: JASON

         至此,不需要用户验证的server端和client端已完成。 但我们的主题是 需要加入用户验证,所以我们还得继续下面的步骤。

三, Tomcat的配置文件 及 Server端的配置

1,tomcat配置用户角色及用户信息。

我这里是 tomcat7, 找到 Tomcat 7.0\conf 目录下的 tomcat-users.xml 文件.

[html]  view plain copy
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <tomcat-users>  
  3.   <role rolename="WsOperator"/>  
  4.   <user username="TomcatWs" password="你的密码(自己设定)" roles="WsOperator"/>  
  5. </tomcat-users>  

     2,server端配置web.xml

      WsAuthServer 项目 的 WebRoot\WEB-INFO目录下的web.xml 

     加入如下内容:

[html]  view plain copy
  1. <security-role>  
  2.   <description>Normal operator user</description>  
  3.   <role-name>WsOperator</role-name>  
  4. </security-role>  
  5. <security-constraint>  
  6.   <web-resource-collection>  
  7.     <web-resource-name>Operator Roles Security</web-resource-name>  
  8.     <url-pattern>/TestPort</url-pattern>  
  9.   </web-resource-collection>  
  10.   <auth-constraint>  
  11.     <role-name>WsOperator</role-name>  
  12.   </auth-constraint>  
  13.   <user-data-constraint>  
  14.     <transport-guarantee>NONE</transport-guarantee>  
  15.   </user-data-constraint>  
  16. </security-constraint>  
  17. <login-config>  
  18.   <auth-method>BASIC</auth-method>  
  19. </login-config>  

       将server端的webservice重新发布一次,并重启 tomcat。

       再通过浏览器访问:http://localhost:8080/WsAuthServer/TestPort?wsdl,会让你验证。说明刚才的配置生效了。

四,修改Client端调用webservice的方法

        回到client端,重新run一下main中的方法,发现会报错,这时,client端调用webservice的方法需要重新写:

[java]  view plain copy
  1. package com.TEST;  
  2.   
  3. import java.net.URL;  
  4. import javax.xml.namespace.QName;  
  5. import javax.xml.ws.BindingProvider;  
  6. import javax.xml.ws.Service;  
  7.     
  8. import com.server.*;  
  9.    
  10. public class test {  
  11.   
  12.     /** 
  13.      * @param args 
  14.      */  
  15.     public static void main(String[] args) {  
  16.        
  17.         final String WS_URL = "file:C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/WsAuthServer/WEB-INF/wsdl/TestService.wsdl";  
  18.         final String S_URL = "http://localhost:8080/WsAuthServer/TestPort?wsdl";  
  19.    
  20.         URL url;  
  21.         try {  
  22.              url = new URL(WS_URL);  
  23.           
  24.              QName qname = new QName("http://Server.com/","TestService");  
  25.              Service service = Service.create(url, qname);  
  26.              ITest port=service.getPort(ITest.class);  
  27.              BindingProvider bp = (BindingProvider) port;  
  28.              bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY,"TomcatWs");  
  29.              bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "111");  
  30.              bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,S_URL);  
  31.              String rtnMessage = port.sayHello("JASON");  
  32.              System.out.println("Result=>" + rtnMessage);  
  33.            
  34.         } catch (Exception e) {  
  35.             System.out.println("error");  
  36.             System.out.println(e.getMessage());  
  37.         }  
  38.            
  39.     }  
  40.   
  41. }  

          RUN一遍,得到如下信息:

    error
request requires HTTP authentication: Unauthorized

         如果把 下面的111 换成你自己的密码,又会得到 Result=>Hello: JASON

[java]  view plain copy
  1. bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "111");  

补充:PC的client端还有另外一种写法:

  

[java]  view plain copy
  1. final String WS_URL = "file:C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/WsAuthServer/WEB-INF/wsdl/TestService.wsdl";  
  2.     final String S_URL = "http://localhost:8080/WsAuthServer/TestPort?wsdl";  
  3.        
  4.     URL url;  
  5.     try {  
  6.          url = new URL(WS_URL);  
  7.   
  8.          QName qname = new QName("http://Server.com/","TestService");  
  9.          TestService service = new TestService(url, qname);  
  10.          TestDelegate test = service.getTestPort();  
  11.            
  12.          BindingProvider bp = (BindingProvider) test;  
  13.          bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY,"TomcatWs");  
  14.          bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "你的密码");  
  15.          bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,S_URL);  
  16.              
  17.          String rtnMessage = test.sayHello("JASON");  
  18.          System.out.println("Result=>" + rtnMessage);  
  19.           
  20.          /*  
  21.          rtnMessage = test.sumCalc(23, 423); 
  22.          System.out.println("Result=>" + rtnMessage);  
  23.           
  24.          rtnMessage = test.getCurrentServerTime(); 
  25.          System.out.println("Result=>" + rtnMessage); */  
  26.             
  27.         /* BindingProvider bp = (BindingProvider) service; 
  28.             Map<String,Object> context = bp.getRequestContext(); 
  29.  
  30.             context.put(BindingProvider.USERNAME_PROPERTY, "myusername"); 
  31.             context.put(BindingProvider.PASSWORD_PROPERTY, "mypassword");*/  
  32.    
  33.     } catch(Exception ex)  
  34.     {  
  35.         System.out.println(ex.getMessage());  
  36.     }  
  37.       
  38.     System.out.println("the end.");  

 

至此, 一个完整的用户验证结束。非常感谢。

内容来源于 小红提技术博客,http://www.xiaohongti.com/ 转载请保留地址,尊重版权。

猜你喜欢

转载自java-ahz.iteye.com/blog/2245323