Spring WS:如何将拦截器应用到特定端点

2024-04-01

我在 Spring 应用程序上有多个工作 SOAP Web 服务,使用 httpBasic 身份验证,并且我需要在其中之一上使用 WS-Security,以允许使用以下 Soap 标头进行身份验证。

<soap:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soap:mustUnderstand="1">
  <wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-1">
    <wsse:Username>username</wsse:Username>
    <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
  </wsse:UsernameToken>
</wsse:Security></soap:Header>

当前 WSConfiguration 是根据https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-ws/ https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-ws/给予类似的东西

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

    @Bean
    public ServletRegistrationBean dispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        return new ServletRegistrationBean(servlet, "/services/*");
    }

    @Bean(name = "SOAP1")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema soap1) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("Soap1");
        wsdl11Definition.setLocationUri("/soap1/");
        wsdl11Definition.setTargetNamespace("http://mycompany.com/hr/definitions");
        wsdl11Definition.setSchema(soap1);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema soap1() {
        return new SimpleXsdSchema(new ClassPathResource("META-INF/schemas/hr.xsd"));
    }

}

和网络安全根据http://spring.io/blog/2013/07/03/spring-security-java-config-preview-web-security/ http://spring.io/blog/2013/07/03/spring-security-java-config-preview-web-security/看起来像这样

@EnableWebSecurity
@Configuration
public class CustomWebSecurityConfigurerAdapter extends
   WebSecurityConfigurerAdapter {
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) {
    auth
      .inMemoryAuthentication()
        .withUser("user1")  
          .password("password")
          .roles("SOAP1")
          .and()
        .withUser("user2") 
          .password("password")
          .roles("SOAP2");
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeUrls()
        .antMatchers("/soap/soap1").hasRole("SOAP1") 
        .antMatchers("/soap/soap2").hasRole("SOAP2") 
        .anyRequest().authenticated() 
        .and().httpBasic();
  }
}

经过一番搜索,我发现 Wss4J 提供了 UsernameToken 身份验证,但不知道如何使用它。我想做的是以下内容https://sites.google.com/site/ddmwsst/ws-security-impl/ws-security-with-usernametoken https://sites.google.com/site/ddmwsst/ws-security-impl/ws-security-with-usernametoken但没有带有 bean 定义的 XML 文件。

我打算做什么:

  • 创建回调处理程序。
  • 创建一个 Wss4jSecurityInterceptor,设置“setValidationActions”到“用户名令牌”,“setValidationCallbackHandler” 到我的回调处理程序,然后添加它压倒性的 addInterceptors在我的 WebServiceConfig 上。

(我尝试了类似的方法,但我刚刚意识到我的回调使用的是已弃用的方法)

问题:即使它有效,它也会适用于“WebServiceConfig”上的所有网络服务。

Update :

该实现确实有效,但正如预期的那样,它应用于我的所有 Web 服务。如何将我的拦截器仅添加到 1 个 Web 服务?

下面是我在WebServiceConfig中添加的代码

 @Bean
    public Wss4jSecurityInterceptor wss4jSecurityInterceptor() throws IOException, Exception{
        Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
        interceptor.setValidationActions("UsernameToken");
        interceptor.setValidationCallbackHandler(new Wss4jSecurityCallbackImpl());

    return interceptor;
}

@Override
public void addInterceptors(List<EndpointInterceptor> interceptors)  {
    try {
        interceptors.add(wss4jSecurityInterceptor());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

抱歉,我完全忘记回答这个问题,但万一它对某人有帮助:

我们通过创建一个新的 SmartEndpointInterceptor 并将其仅应用于我们的端点来使其工作:

public class CustomSmartEndpointInterceptor extends Wss4jSecurityInterceptor implements SmartEndpointInterceptor {

    //CustomEndpoint is your @Endpoint class
    @Override
    public boolean shouldIntercept(MessageContext messageContext, Object endpoint) {
        if (endpoint instanceof MethodEndpoint) {
            MethodEndpoint methodEndpoint = (MethodEndpoint)endpoint;
            return methodEndpoint.getMethod().getDeclaringClass() == CustomEndpoint.class; 
        }
        return false;
    }
}

我们没有向 WebServiceConfig 添加 wss4j bean,而是添加了 SmartEndpointInterceptor :

@Configuration
public class SoapWebServiceConfig extends WsConfigurationSupport {

    //Wss4jSecurityCallbackImpl refers to an implementation of https://sites.google.com/site/ddmwsst/ws-security-impl/ws-security-with-usernametoken
    @Bean
    public CustomSmartEndpointInterceptor customSmartEndpointInterceptor() {
        CustomSmartEndpointInterceptor customSmartEndpointInterceptor = new CustomSmartEndpointInterceptor();
        customSmartEndpointInterceptor.setValidationActions("UsernameToken");
        customSmartEndpointInterceptor.setValidationCallbackHandler(new Wss4jSecurityCallbackImpl(login, pwd)); 
        return customSmartEndpointInterceptor;
    }

  [...]
}

希望这足够清楚:)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring WS:如何将拦截器应用到特定端点 的相关文章

随机推荐

  • python 中的线程锁未按预期工作

    我试图保护线程内的数据免受主线程的影响 我有以下代码 lock threading Lock def createstuff data t threading Thread target func args data t start def
  • 为什么 Collections.binarySearch 给出错误的结果?

    我创建了一个列表 其中保存了一些字符串 但是当我在做的时候二分查找在此列表中 它正在返回负值而该项目是在列表中 到目前为止我的知识正值当物品被退回时在列表中 但对于某些项目 它返回负值 而对于某些项目 它返回正值 Code Test pub
  • 结构体上溢出的整数加法[重复]

    这个问题在这里已经有答案了 有的是ULARGE INTEGER 联合 https msdn microsoft com en us library windows desktop aa383742 v vs 85 aspx对于不支持 64
  • 使用 AJAX 时页面不断刷新

    我正在创建一个包含表单的模式框 使用 ajax 和 php 提交后 表单将返回输入 然后模式框应该消失 问题是 结果在框消失和页面刷新之前显示了几秒钟
  • flutter中如何在某个时间执行一个方法?

    如何在固定时间执行一个方法 比如我想在下午 2 30 运行一个方法 我了解计时器功能 但是运行计时器功能这么长时间是个好主意吗 同样 该方法在一天内会被调用多次 Edited 我努力了android alarm manager https
  • R data.table 加速 SI/公制转换

    情况是这样的 我有一个 8500 万行 18 列的表 其中三列的值采用公制前缀 SI 表示法 请参阅公制前缀 http en wikipedia org wiki Metric prefix维基百科上 这意味着我有这样的号码 1M 而不是
  • 访问 <#list> 中对象的属性

    Solution 我之前曾尝试向 LineItem 类添加访问器 例如 public String getItemNo return itemNo 并将 FTL 从 lineItem itemNo to lineItem getItemNo
  • PushStreamContent 流在负载下不会刷新

    我正在使用 PushStreamContent 来保持与每个客户端的持久连接 每 20 秒向每个客户端流推送短心跳消息对于 100 个客户端来说效果很好 但在大约 200 个客户端时 客户端首先开始延迟几秒钟接收 然后根本不显示 我的控制器
  • HighCharts图像导出

    我在我的应用程序中使用 HighChart 我想通过单击按钮导出图表图像http jsfiddle net hfrntt fXHB5 1896 http jsfiddle net hfrntt fXHB5 1896 但我想将图像保存在预定义
  • 无法从 Scrapy 脚本访问 request.response.meta['redirect_urls']

    我无法访问request response meta redirect urls 来自我的 Scrapy 脚本 但在 Scrapy shell 中访问同一网页的此信息没有问题 当我打印钥匙时request response meta我只看到
  • 如何使用 Firebase Cloud Messaging 自动增加 iOS 通知徽章?

    如何使用 Firebase Cloud Messaging 自动增加 iOS 通知徽章 是否可以做类似的事情 1 or 您可以在 通知负载 https firebase google com docs cloud messaging htt
  • Onclick 或 href 最适合在按钮中打开链接

    这是最好的方法 使用按钮打开链接
  • 使用 webdriver python 的触摸事件示例?

    我见过大约100个Java Webdriver 的触摸事件示例 http android developers blogspot com 2011 10 introducing android webdriver html在线 但没有一个P
  • 删除 pandas 数据框中的所有特殊字符

    我无法从 pandas 数据框中删除所有特殊字符 你能帮我吗 我尝试过这样的事情 df df replace r W regex True 因为我在最近的一篇文章中发现了它 但是当我执行时 特殊字符 不会消失 我知道在 PostgresQL
  • Firebase 删除不应该的值

    我正在使用 firebase 编写 Android 应用程序 我有一个部分 用户发送取件请求 该请求显示在司机的请求片段中 为了处理接受 拒绝 我已经设置了它 因此当您单击 接受 时 它会创建另一个包含已接受请求的 Firebase 子项
  • express - Angular2错误:ENOENT:刷新时没有这样的文件或目录

    我有一个公共文件夹 其中放置了 angular2 应用程序 现在我正在尝试设置一个带有始终返回index html 的包罗万象的路由的快速服务器 需要明确的是 根据这个问题 https stackoverflow com questions
  • OCaml 数据类型定义中的方括号“[”和“]”是什么意思?

    I saw 下列 https coq github io doc v8 11 api coq Genarg index html type rlevel type rlevel rlevel 但我以前从未见过这种情况 并且 ADT 代数数据
  • 是否有最近键映射数据结构?

    我遇到一种情况 我需要找到与我请求的键最接近的值 它有点像定义键之间距离的最近地图 例如 如果我在映射中有键 A C M Z 则对 D 的请求将返回 C 的值 任何想法 大多数树数据结构使用某种排序算法来存储和查找键 许多这样的实现可以找到
  • Lollipop中导航栏的默认Alpha值

    如果你设置android windowTranslucentNavigation to true在 Lollipop 主题中 导航栏不会像 KitKat 那样完全半透明 相反 它是一个黑暗的半透明背景 这里有人知道该背景的 alpha 值是
  • Spring WS:如何将拦截器应用到特定端点

    我在 Spring 应用程序上有多个工作 SOAP Web 服务 使用 httpBasic 身份验证 并且我需要在其中之一上使用 WS Security 以允许使用以下 Soap 标头进行身份验证