ITeye博客

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

zc_zhou_zhouchao · 2015-09-22 16:53 · webservice · 编程语言 · Markdown

说明:在网上找了一堆 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 plaincopy

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

2),  Test.java

 

 

[java] view plaincopy

  1. package com.Server;
  2. import javax.jws.WebService;
  3. @WebService(endpointInterface = "com.Server.ITest")
  4. public class Test implements ITest {
  5. @Override
  6. public String SayHello(String name) {
  7. return "Hello: " + name;
  8. }
  9. }

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 plaincopy

  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 plaincopy

  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 plaincopy

  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 plaincopy

  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 plaincopy

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

          RUN一遍,得到如下信息:

 

    error
request requires HTTP authentication: Unauthorized

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

[java] view plaincopy

  1. bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "111");

 

 

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

  

[java] view plaincopy

  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. URL url;
  4. try {
  5. url = new URL(WS_URL);
  6. QName qname = new QName("http://Server.com/","TestService");
  7. TestService service = new TestService(url, qname);
  8. TestDelegate test = service.getTestPort();
  9. BindingProvider bp = (BindingProvider) test;
  10. bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY,"TomcatWs");
  11. bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "你的密码");
  12. bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,S_URL);
  13. String rtnMessage = test.sayHello("JASON");
  14. System.out.println("Result=>" + rtnMessage);
  15. /*
  16. rtnMessage = test.sumCalc(23, 423);
  17. System.out.println("Result=>" + rtnMessage);
  18. rtnMessage = test.getCurrentServerTime();
  19. System.out.println("Result=>" + rtnMessage); */
  20. /* BindingProvider bp = (BindingProvider) service;
  21. Map<String,Object> context = bp.getRequestContext();
  22. context.put(BindingProvider.USERNAME_PROPERTY, "myusername");
  23. context.put(BindingProvider.PASSWORD_PROPERTY, "mypassword");*/
  24. } catch(Exception ex)
  25. {
  26. System.out.println(ex.getMessage());
  27. }
  28. System.out.println("the end.");

 

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

 

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。