【问题标题】:Spring-ws add interceptors not globallySpring-ws 不全局添加拦截器
【发布时间】:2021-03-16 20:53:06
【问题描述】:

我正在使用@Configuration 类来配置端点,并且需要一个具有安全性而另一个没有。

如果我添加拦截器,它们都适用于两者,我找不到只为其中一个分配拦截器的方法。

@Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
    try {
        interceptors.add(mySecurityIniterceptor());
    } catch (Exception e) {
        throw new RuntimeException("No se puede inicializar el Interceptor.");
    }
}

【问题讨论】:

    标签: java spring soap spring-ws


    【解决方案1】:

    像下面的例子一样声明你的拦截器

    public class MyInterceptor  implements SmartEndpointInterceptor {
    
        @Override
        public boolean shouldIntercept(MessageContext messageContext, Object endpoint) {
            if (endpoint instanceof MethodEndpoint) {
                MethodEndpoint methodEndpoint = (MethodEndpoint)endpoint;
                return methodEndpoint.getMethod().getDeclaringClass() == YourEndpoint.class; <--------Replace that with your endpoint that you wish to interpret
            }
            return false;
        }
    
        @Override
        public boolean handleRequest(MessageContext messageContext, Object o) throws Exception {
            return true;
        }
    
        @Override
        public boolean handleResponse(MessageContext messageContext, Object o) throws Exception {
            return true;
        }
    
        @Override
        public boolean handleFault(MessageContext messageContext, Object o) throws Exception {
            return true;
        }
    
        @Override
        public void afterCompletion(MessageContext messageContext, Object o, Exception e) throws Exception {
    
        }
    }
    

    不要忘记修改上面代码中的YourEndpoint.class; &lt;--------Replace that with your endpoint that you wish to interpret这一行。

    然后注册拦截器

    @Override
    public void addInterceptors(List<EndpointInterceptor> interceptors) {
        try {
            interceptors.add(new MyInterceptor());
        } catch (Exception e) {
            throw new RuntimeException("No se puede inicializar el Interceptor.");
        }
    }
    

    【讨论】:

    • 感谢@Boug的回复,但是这个方法是针对mvc框架的,不是针对webservice的,WsConfigurerAdapter,在我使用param的方法中是List&lt;EndpointInterceptor&gt;不是InterceptorRegistry .
    • mmm...我的拦截器是Wss4jSecurityInterceptor 我认为我不能这样做。我会尝试这样的事情。谢谢!
    • 在这种情况下,只需执行public class MyInterceptor extends Wss4jSecurityInterceptor implements SmartEndpointInterceptor 。如果您已经解决了问题,请不要忘记接受答案:D
    • 我再试一次,在我的实现中稍作改动,这行得通!我在扩展 WsConfigurationSupport 而不是 WsConfigurerAdapter 的类中添加拦截器。非常感谢。
    猜你喜欢
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2020-06-29
    • 2014-11-27
    • 2020-12-16
    • 2012-01-14
    相关资源
    最近更新 更多