2016-05-25 8 views
0

ハンドルこんにちは私はすべての春のセキュリティ例外を処理するために、このクラスを作成しました:春のセキュリティ例外CXF

   import org.apache.cxf.binding.soap.SoapFault; 
       import org.apache.cxf.binding.soap.SoapMessage; 
       import org.apache.cxf.interceptor.Fault; 
       import org.apache.cxf.phase.AbstractPhaseInterceptor; 
       import org.apache.cxf.phase.Phase; 
       import org.springframework.beans.factory.InitializingBean; 
       import org.springframework.security.authentication.AuthenticationManager; 
       import org.springframework.security.core.Authentication; 
       import org.springframework.security.core.AuthenticationException; 

       /** 
       * The Class SoapAuthenticationInterceptor. 
       */ 
       public class SoapAuthenticationInterceptor extends AbstractPhaseInterceptor<SoapMessage> implements InitializingBean { 

        /** The authentication manager. */ 
        private AuthenticationManager authenticationManager; 

        /** The authentication required. */ 
        private boolean authenticationRequired = true; 

        /** 
        * Instantiates a new soap authentication interceptor. 
        */ 
        public SoapAuthenticationInterceptor() { 
         super(Phase.RECEIVE); 
        } 

        /** 
        * Sets the authentication manager. 
        * 
        * @param authenticationManager 
        *   the new authentication manager 
        */ 
        public void setAuthenticationManager(AuthenticationManager authenticationManager) { 
         this.authenticationManager = authenticationManager; 
        } 

        /** 
        * Sets the authentication required. 
        * 
        * @param authenticationRequired 
        *   the new authentication required 
        */ 
        public void setAuthenticationRequired(boolean authenticationRequired) { 
         this.authenticationRequired = authenticationRequired; 
        } 

        /* 
        * (non-Javadoc) 
        * 
        * @see 
        * org.springframework.beans.factory.InitializingBean#afterPropertiesSet() 
        */ 
        public void afterPropertiesSet() throws Exception { 
         if (authenticationManager == null) { 
          throw new IllegalStateException("No authentication manager has been configured"); 
         } 
        } 

        /* 
        * (non-Javadoc) 
        * 
        * @see org.apache.cxf.interceptor.Interceptor#handleMessage(org.apache.cxf. 
        * message.Message) 
        */ 
        public void handleMessage(SoapMessage message) throws Fault { 
         Authentication authentication = message.getExchange().get(Authentication.class); 
         if (authentication != null) { 
          try { 
           authentication = authenticationManager.authenticate(authentication); 
           message.getExchange().put(Authentication.class, authentication); 
          } catch (AuthenticationException ex) { 
           throw new SoapFault("Bad credentials", message.getVersion().getSender()); 
          } 
         } else if (authenticationRequired) { 
          throw new SoapFault("Authentication required", message.getVersion().getSender()); 
         } 
        } 
       } 

はその後、私のapplicationContext.xmlをして私はこのように、このintercepterを設定している:

  <bean id="soapAuthenticationInterceptor" class="com.test.cxf.interceptors.SoapAuthenticationInterceptor"> 
       <property name="authenticationManager" ref="authenticationManager" /> 
      </bean> 

      <cxf:bus> 
       <cxf:features> 
        <cxf:logging /> 
       </cxf:features> 
       <cxf:inInterceptors> 
        <ref bean="soapAuthenticationInterceptor" /> 
       </cxf:inInterceptors> 
       <cxf:outFaultInterceptors> 
        <ref bean="soapAuthenticationInterceptor" /> 
       </cxf:outFaultInterceptors> 
      </cxf:bus> 

私問題は、私がログイン/パスワードが間違っているsoapui enveloppeを送信すると、インターセプタが呼び出されないということですか?

お願いします。

+0

であるあなたが「にref'erringされているクラスauthenticationManagerを定義していますか? – Sampada

+0

はいコードは正常に動作し、例外はありません –

+0

okです。あなたは悪いlogin/pwdを送信したときにインターセプタが呼び出されないと言った。そうでなければ呼び出されますか? – Sampada

答えて

0

カスタムCallbackHandlerを作成し、そのhandleメソッドを実装する必要があります。 authenticationManagerクラスでこのメソッドを呼び出し、ユーザー名とパスワードを渡します。

詳細はここで見つけることができます:http://cxf.apache.org/docs/ws-security.html

+0

:http://docs.spring.io/spring-ws/site/reference/html/security.html –

+0

チェックセクション7.2を使用するので、私はのCallbackHandlerを使用することができます – Sampada

関連する問題