久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

AXIS學(xué)習(xí)筆記(三)(建立安全的AXIS服務(wù)上)

 WindySky 2009-02-20
建立安全的AXIS服務(wù)
在前面的文章中,我們實現(xiàn)了最簡單的AXIS服務(wù)?,F(xiàn)在我們一起來討論一下Web服務(wù)的安全問題。
根據(jù)應(yīng)用的對安全要求的級別不同,,可以采用不同的方式來實現(xiàn)安全性,以下是目前最常用的一些實現(xiàn)方式(從低到高排列):
    1,、J2EE Web應(yīng)用默認的訪問控制(數(shù)據(jù)是明文的); 
    2,、使用axis的Handler進行訪問控制(數(shù)據(jù)是明文的); 
    3,、使用Servlet過濾器(Filter)進行訪問控制(數(shù)據(jù)是明文的); 
    4,、使用SSL/HTTPS協(xié)議來傳輸(加密的數(shù)據(jù)傳輸協(xié)議),; 
    5、使用WS-Security規(guī)范對信息進行加密與身份認證(數(shù)據(jù)被加密傳輸),。
我們僅討論第2,、4,、5種實現(xiàn)方式。在此之前我們先來了解一下AXIS自帶的一個工具SOAPMonitor,。
一、SOAPMonitor的使用
  打開http://localhost:8080/axis/進入AXIS的主頁面,,你會看見:
   SOAPMonitor-[disabled by default for security reasons] ,默認狀態(tài)下其是不可用的,,現(xiàn)在我們就來激活它,。
   
1、到目錄%TOMCAT_HOME%\webapps\axis下,,你會找到SOAPMonitorApplet.java,,在命令行中編譯它:
        javac -classpath %AXIS_HOME%\lib\axis.jar SOAPMonitorApplet.java
   編譯完之后你會看見目錄下多了很多CLASS文件,它們的名字是SOAPMonitorApplet*.class

2,、在目錄%TOMCAT_HOME%\webapps\axis\WEB-INF下打開server-config.wsdd文件,,將下面的兩部分代碼直
   接加入其中相應(yīng)的位置
   第一部分:
      <handler name="soapmonitor"   type="java:org.apache.axis.handlers.SOAPMonitorHandler">
        <parameter name="wsdlURL"   value="/axis/SOAPMonitorService-impl.wsdl"/>
        <parameter name="namespace"   value="http:///wsdl/2001/12/SOAPMonitorService-impl.wsdl"/>
        <parameter name="serviceName" value="SOAPMonitorService"/>
        <parameter name="portName" value="Demo"/>
      </handler>
   第二部分:
      <service name="SOAPMonitorService" provider="java:RPC">
        <parameter name="allowedMethods" value="publishMessage"/>
        <parameter name="className"   value="org.apache.axis.monitor.SOAPMonitorService"/>
        <parameter name="scope" value="Application"/>
      </service>

3,、選擇你要監(jiān)控的服務(wù)
   以上次的HelloWorld服務(wù)為例,,在server-config.wsdd中你會找到這段代碼
    <service name="HelloWorld" provider="java:RPC">
       <parameter name="allowedMethods" value="sayHello"/>
       <parameter name="className" value="HelloWorld"/>
    </service>
   在這段代碼中加入以下的代碼:
    <requestFlow>
      <handler type="soapmonitor"/>
    </requestFlow>
    <responseFlow>
      <handler type="soapmonitor"/>
    </responseFlow>
   最后的樣子是:
    <service name="HelloWorld" provider="java:RPC">
    <requestFlow>
      <handler type="soapmonitor"/>
    </requestFlow>
    <responseFlow>
      <handler type="soapmonitor"/>
    </responseFlow>
    <parameter name="allowedMethods" value="sayHello"/>
    <parameter name="className" value="HelloWorld"/>
    </service>
   這樣HelloWorld服務(wù)就被監(jiān)控了
   
4,、啟動Tomcat,打開http://localhost:8080/axis/SOAPMonitor,你就會看到Applet界面,,在
   jbuilder2005中運行我們上次寫的客戶端程序 TestClient.java。OK,!你會在Applet界面看
   見客戶端與服務(wù)器端互發(fā)的XML內(nèi)容,注意這里是明文,!
   
二,、使用axis的Handler進行訪問控制(對安全要求不高時推薦)
   axis為Web服務(wù)的訪問控制提供了相關(guān)的配置描述符,并且提供了一個訪問控制的簡單  Handler,。默認情況下,,你只要在配置描述符中添加用戶,然后在Web服務(wù)器的部署描述符中自動允許的角色即可,。

1,、在axis的配置文件users.lst(位于WEB-INF目錄下)中添加一個用戶,如"ronghao1111",,表示
   用戶名為ronghao,,密碼為1111。
   
2,、把例HelloWorld的Web服務(wù)重新部署(新加的部分已標(biāo)出)
    <service name="HelloWorld" provider="java:RPC">
    <requestFlow>
      <handler type="soapmonitor"/>
      <handler type="Authenticate"/>  //新加的AXIS自帶的Handler
    </requestFlow>
    <responseFlow>
      <handler type="soapmonitor"/>
    </responseFlow>
    <parameter name="allowedMethods" value="sayHello"/>
    <parameter name="allowedRoles" value="ronghao"/>    //注意,,這里是新加的部分!
    <parameter name="className" value="HelloWorld"/>
   </service>
  在這個部署描述符中,,指定HelloWorld服務(wù)只能被ronghao訪問
   
3、修改客戶端程序 TestClient.java,,增加訪問用戶名,、密碼(新加的部分已標(biāo)出)
   TestClient.java

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.rpc.ParameterMode;

public class TestClient
{
   public static void main(String [] args) throws Exception {
   String endpoint = "http://localhost:" +"8080"+ "/axis/HelloWorld";

       Service  service = new Service();  
       Call     call    = (Call) service.createCall();
       call.getMessageContext().setUsername("ronghao");//  用戶名,。
       call.getMessageContext().setPassword("1111");//   密碼
     call.setTargetEndpointAddress( new java.net.URL(endpoint) );

     call.setOperationName( "sayHello" );
     String res = (String) call.invoke( new Object[] {} );

     System.out.println( res );
   }
}
  執(zhí)行TestClient,能夠順利訪問Web服務(wù),;如果修改用戶名或者密碼,,那么就不能訪問 。同樣,,
  你在http://localhost:8080/axis/SOAPMonitor中看到的請求和響應(yīng)的XML是明文,!
  
三、使用SSL/HTTPS協(xié)議來傳輸
    Web服務(wù)也可以使用SSL作為傳輸協(xié)議,。雖然JAX-RPC并沒有強制規(guī)定是否使用SSL協(xié)議,,但在tomcat
    下使用HTTPS協(xié)議,。
1、使用JDK自帶的工具創(chuàng)建密匙庫和信任庫,。

  1)通過使用以下的命令來創(chuàng)建服務(wù)器端的密匙庫:
   keytool -genkey -alias Server -keystore server.keystore -keyalg RSA
  輸入keystore密碼:  changeit
  您的名字與姓氏是什么?
  [Unknown]:  Server
  您的組織單位名稱是什么,?
  [Unknown]:  ec
  您的組織名稱是什么,?
  [Unknown]:  ec
  您所在的城市或區(qū)域名稱是什么,?
  [Unknown]:  beijing
  您所在的州或省份名稱是什么,?
  [Unknown]:  beijing
  該單位的兩字母國家代碼是什么
  [Unknown]:  CN
CN=Server, OU=ec, O=ec, L=beijing, ST=beijing, C=CN 正確嗎,?
  [否]:  y

輸入<Server>的主密碼
        (如果和 keystore 密碼相同,,按回車):
   以上命令執(zhí)行完成后,將獲得一個名為server.keystore的密匙庫,。
   
  2)生成客戶端的信任庫。首先輸出RSA證書:
  keytool -export -alias Server -file test_axis.cer -storepass changeit -keystore server.keystore
  然后把RSA證書輸入到一個新的信任庫文件中,。這個信任庫被客戶端使用,,被用來驗證服務(wù)器端的身份。
  keytool -import -file test_axis.cer -storepass changeit -keystore client.truststore -alias serverkey -noprompt
  以上命令執(zhí)行完成后,,將獲得一個名為client.truststore的信任庫,。
  
  3)同理生成客戶端的密匙庫client.keystore和服務(wù)器端的信任庫server.truststore.方便起見給出.bat文件
     gen-cer-store.bat內(nèi)容如下:
     set SERVER_DN="CN=Server, OU=ec, O=ec, L=BEIJINGC, S=BEIJING, C=CN"
     set CLIENT_DN="CN=Client, OU=ec, O=ec, L=BEIJING, S=BEIJING, C=CN"
     set KS_PASS=-storepass changeit
     set KEYINFO=-keyalg RSA

     keytool -genkey -alias Server -dname %SERVER_DN% %KS_PASS% -keystore server.keystore %KEYINFO% -keypass changeit
     keytool -export -alias Server -file test_axis.cer %KS_PASS% -keystore server.keystore
     keytool -import -file test_axis.cer %KS_PASS% -keystore client.truststore -alias serverkey -noprompt

     keytool -genkey -alias Client -dname %CLIENT_DN% %KS_PASS% -keystore client.keystore %KEYINFO% -keypass changeit
     keytool -export -alias Client -file test_axis.cer %KS_PASS% -keystore client.keystore
     keytool -import -file test_axis.cer %KS_PASS% -keystore server.truststore -alias clientkey -noprompt
     
  好的,,現(xiàn)在我們就有了四個文件:server.keystore,server.truststore,,client.keystore,client.truststore
  
2,、更改Tomcat的配置文件(server.xml),,增加以下部署描述符:(其實里面有,只是被注釋掉了)
      <Connector port="8440" 
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" disableUploadTimeout="true"
               acceptCount="100" scheme="https" secure="true"
               clientAuth="true" keystoreFile="f:\server.keystore" keystorePass="changeit"
                 truststoreFile="f:\server.truststore" truststorePass="changeit"
               sslProtocol="TLS" />

3,、把HelloWorld重新部署一次,,在server-config.wsdd中修改如下部署代碼。(還原了而已)
    <service name="HelloWorld" provider="java:RPC">
    <requestFlow>
      <handler type="soapmonitor"/>
    </requestFlow>
    <responseFlow>
      <handler type="soapmonitor"/>
    </responseFlow>
    <parameter name="allowedMethods" value="sayHello"/>
    <parameter name="className" value="HelloWorld"/>
    </service>
    
4,、修改客戶端程序 TestClient.java(修改的部分已標(biāo)出)

   public class TestClient
{
   public static void main(String [] args) throws Exception {
   String endpoint = "https://localhost:" +"8440"+ "/axis/HelloWorld";//注意區(qū)別在這里,!https!

       Service  service = new Service();  
       Call     call    = (Call) service.createCall();
     call.setTargetEndpointAddress( new java.net.URL(endpoint) );

     call.setOperationName( "sayHello" );
     String res = (String) call.invoke( new Object[] {} );

     System.out.println( res );
   }
}

5,、最后使用命令來執(zhí)行客戶端程序

java -cp %AXISCLASSPATH%
     -Djavax.net.ssl.keyStore=client.keystore 
     -Djavax.net.ssl.keyStorePassword=changeit 
     -Djavax.net.ssl.trustStore=client.truststore 
     TestClient

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點,。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,謹防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多