zoukankan      html  css  js  c++  java
  • wse

    由于项目集成需要使用Web Service来提供接口。而Web Service又是面向整个Internat的。理所当然的安全性问题被搬上了台面。    WSE顾名思义是对Web Service的补充与加强,大家都知道Web Service是一个公开的标准,所以对Web Service的扩展也是需要加入Web Service标准的。(听说为了与BEA,SUN等大公司统一,Microsoft花了不少时间与精力)WS-Security、WS-Policy、WS-Addressing就是现有统一的扩展(WS-Transaction还在统一中...)。
        下面分别来介绍一下WS-SecurityWS-Policy:
        WS-Security想必大家从字面上就能猜出她的作用了吧,对!它是对基本的Web Service在安全性方面的增强。他提供了UsernameTokenKerberosX509三种验证方式。
        WS-Policy根据我的理解就是对访问Web Service或Web Service返回的Soap信息的格式的要求或者限制。打个比方:访问的Soap必须有X509加密或者必须有<Username>等XML段等

    wse2.0发布了,大家都来一起研究吧,我先来抛砖引玉:
    调用webservice时有的时候安全性要求比较高,wse提供了客户端证书来调用webservice,好我们就来看看怎么弄。
    调用webservice当然有客户端和webservice端了,我们先来看看
    客户端:

    using Microsoft.Web.Services2.Security;
    using Microsoft.Web.Services2.Security.Tokens;
    using Microsoft.Web.Services2.Security.X509;
    1// 在个人证书存储区获取证书
    X509CertificateStore store = X509CertificateStore.CurrentUserStore( X509CertificateStore.MyStore );
    store.OpenRead() 
    //读取证书的keyid
    X509CertificateCollection certs = store.FindCertificateByKeyIdentifier( Convert.FromBase64String( keyIdentifier ) );
    X509SecurityToken token 
    = null;
    if (certs.Count > 0)
    {
         
    // 得到证书存储区的第一个个人证书
         token = new X509SecurityToken( ((X509Certificate) certs[0]) );
    }
      


    2。//把token加入到soap中
    ServiceWse serviceProxy = new ServiceWse(); //远程webservice代理
    serviceProxy.RequestSoapContext.Security.Tokens.Add( token );
     serviceProxy.RequestSoapContext.Security.Elements.Add( new MessageSignature( token ) );

    3。调用webservice的方法:
    。。。和普通调用webservice的方法一样

    WebService端:
    1。配置web.config
    在configuration节点下加:<configSections>
       

    <section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      
    </configSections>表示引用的是wse2.0
    <system.web>下加:<webServices>
          
    <soapExtensionTypes>
            
    <add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0" />
          
    </soapExtensionTypes>
        
    </webServices>
    在configuration节点下加:
    <microsoft.web.services2>
    <security>
          
    <x509 allowTestRoot="true" allowRevocationUrlRetrieval="false" verifyTrust="true" />
        
    </security>
      
    </microsoft.web.services2>


    这个wse2.0中规定的xml节点。

    2。验证客户端提交上来的证书

    //获取客户端提交上来的证书
    X509SecurityToken x509Token = GetSigningToken(RequestSoapContext.Current) as X509SecurityToken;
    public SecurityToken GetSigningToken(SoapContext context)
            
    {
                
    foreach ( ISecurityElement element in context.Security.Elements )
                
    {
                    
    if ( element is MessageSignature )
                    
    {
                        
    // The given context contains a Signature element.
                        MessageSignature sig = element as MessageSignature;

                        
    if (CheckSignature(context, sig))
                        
    {
                            
    // The SOAP Body is signed.
                            return sig.SigningToken;
                        }

                    }

                }
               
    return null;
      }



    //判断证书是否合法
    //根据证书的keyid来判断
    //这个就是证书的keyid,
    x509Token.KeyIdentifier.Value
    。。。
    如果和你颁发的证书keyid不一致的话,你可以抛给他一个错误:
    throw new SecurityFault(SecurityFault.FailedAuthenticationMessage, SecurityFault.FailedAuthenticationCode);
    如果正确,执行webservice中的代码。


  • 相关阅读:
    Shell学习笔记 ——第一天
    Myclipse 安装 Maven遇见的N个异常
    Myeclipse 创建 Web Maven项目
    Guava API
    String 转Map(基于Guava类库)
    Mybatis——helloWorld级程序
    redis
    listener、context、filter、servlet及其加载顺序
    junit 单元测试
    hibernate —— 树状存储
  • 原文地址:https://www.cnblogs.com/zjbtony/p/527323.html
Copyright © 2011-2022 走看看