带有安全策略的axis2实现方式(一)

阅读更多
服务端

1、所需文件

service.jks,放在src下面(与client.jks是一对,具体生成方式可上网查询)

services.xml


 
   server of  receiveMessage  ------服务描述信息
 

 
   com.test.ReceiveMessageServer  -------服务类,连带包结构
 

 
   org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
 

   -------这是axis2与spring的集成,所以将服务类配成了spring bean,这里直接引用spring bean 的名称
  ReceiveMsgBean 

  ----定义服务所需的输入输出参数
  
  
 

 

  -------安全策略的配置
 
     xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
   xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
  
   
           xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
     
      
       
                   sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
         
          
          
         

        

       

      

      
       
                   sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
         
          
          
         

        

       

      

      
       
        
       

      

      
       
        
       

      

      
     

    

    
     
      
      
     

    

           xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
     
    

    
      service

      -------判断密码的回调方法的路径,后面有具体代码
      com.test.MsgCallbackHandler

     
      
                 name="org.apache.ws.security.crypto.merlin.keystore.type">JKS
        service.jks
                 name="org.apache.ws.security.crypto.merlin.keystore.password">servicePW
      

     

    


   

  

 




3、代码

判断密码的回调方法的具体代码:

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.ws.security.WSPasswordCallback;


public class MsgCallbackHandler implements CallbackHandler {

private final static String SERVER_ALIAS = "service";

private final static String SERVER_ALIAS_PASSWORD = "servicePW";

public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
  for (int i = 0; i < callbacks.length; i++) {

   // To use the private key to sign messages, we need to provide  the private key password
   WSPasswordCallback pwcb = (WSPasswordCallback) callbacks[i];

   if (pwcb.getIdentifier().equals(SERVER_ALIAS)) {
    pwcb.setPassword(SERVER_ALIAS_PASSWORD);
    return;
   }
  }
}
}

服务方法的具体代码:


public class ReceiveMessageServer {

    public String receiveMessage(Message message) {
        if (message !=null && StringUtils.isEmpty(message.getMsgId())) {
           System.out.println("成功接收信息!");
           return "0";
        }else{

           System.out.println("接收信息失败!");
           return "1";
    }

}





你可能感兴趣的:(axis2,带有安全策略)