【问题标题】:Basic authentication doesn't work in Spring-Boot WS Soap service基本身份验证在 Spring-Boot WS Soap 服务中不起作用
【发布时间】:2017-03-05 21:23:04
【问题描述】:

我有带有 Spring WS 的 SOAP 模拟服务。我正在尝试添加基本的http auth。我使用这种网络服务配置:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
    MessageDispatcherServlet servlet = new MessageDispatcherServlet();
    servlet.setApplicationContext(applicationContext);
    return new ServletRegistrationBean(servlet);
}

@Bean(name = "cards")
public Wsdl11Definition wsdlDefinition() {
    SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition();
    wsdl11Definition.setWsdl(new ClassPathResource("cards.wsdl"));
    return wsdl11Definition;
}
}

还有这个spring-security的配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
      .authorizeRequests().anyRequest().authenticated()
      .and().httpBasic()
      .and().authorizeRequests().antMatchers("*.wsdl").permitAll();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
            .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");
}

但是当我运行 spring-boot 并向服务发送请求时,即使没有身份验证,它也会返回响应。我配置错了什么?

更新: 此外,如果我运行 spring-boot 并对配置进行以下更改:

//@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

//@Bean
//public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
//    MessageDispatcherServlet servlet = new MessageDispatcherServlet();
//    servlet.setApplicationContext(applicationContext);
//    return new ServletRegistrationBean(servlet);
//}

@Bean(name = "cards")
public Wsdl11Definition wsdlDefinition() {
    SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition();
    wsdl11Definition.setWsdl(new ClassPathResource("cards.wsdl"));
    return wsdl11Definition;
}
}

它工作正常(需要对请求进行身份验证),但 url 映射更改为 [/services/*],这对我来说不是所需的映射。 抱歉,我是 Spring 新手。

【问题讨论】:

  • 模拟服务如何?你是如何使用这个的......你的安全设置也是错误的,因为你的*.wsdl 将被覆盖,因为之前的声明是你需要对所有东西都安全。 (订购很重要)。
  • 我的意思是像this example中的肥皂服务。
  • 还有什么不起作用,你在测试什么(你是如何测试的)以及加载的方式。您只是转储一些配置并期望得到帮助,但您的问题中的信息太少。
  • 我在 Soap UI 和 postman 中进行了测试。 Here is code 的项目。
  • 在soap ui中我用我的cards.wsdl创建了新的SOAP项目。我向没有身份验证标头的服务发送请求,而不是错误说我未通过身份验证,而是返回正确的响应。

标签: java spring web-services soap spring-boot


【解决方案1】:

试试,

正如@denium 指出的顺序很重要

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
      .antMatchers("*.wsdl").permitAll()
      .anyRequest().authenticated().hasRole("USER")
      .and().httpBasic();

}

【讨论】:

    【解决方案2】:

    我刚刚遇到了同样的问题,并且有我的解决方案。 您代码中的 permitAll() 应用于已通过身份验证的用户,您需要使用方法 configure(WebSecurity web) 将 URL 添加到忽略列表中。 而且我认为过滤“*.wsdl”是不够的,我使用了“/**/*.wsdl”

    有我的工作 WebSecurityConfig 类

    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
    //Enforce basic auth
    @Override
    protected void configure(HttpSecurity http) throws Exception {
         http.csrf().disable()
          .httpBasic()
          .and().authorizeRequests().anyRequest().authenticated();
    }
    
    //Ignore basic auth for WSDL URL
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/**/*.wsdl");
    }
    
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        //TODO - do parametru
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }
    

    【讨论】:

      【解决方案3】:

      只需将此代码复制粘贴到您的“WebServiceConfig”类中

      @Bean
         public SimplePasswordValidationCallbackHandler securityCallbackHandler() {
                      SimplePasswordValidationCallbackHandler callbackHandler = new SimplePasswordValidationCallbackHandler();
                      Properties users = new Properties();
                      users.setProperty("AAA", "BBB");
                      callbackHandler.setUsers(users);
                      return callbackHandler;
                  }
      
                  @Bean
                  public Wss4jSecurityInterceptor securityInterceptor() {
                      Wss4jSecurityInterceptor securityInterceptor = new Wss4jSecurityInterceptor();
                      securityInterceptor.setValidationActions("UsernameToken");
                      securityInterceptor.setSecurementMustUnderstand(true);
                      securityInterceptor.setSecurementUsername("setusername");
                      securityInterceptor.setSecurementPassword("setpassword");
                      securityInterceptor.setValidationCallbackHandler(securityCallbackHandler());
                      return securityInterceptor;
                  }
      
                  @Override
                  public void addInterceptors(List interceptors) {
                      interceptors.add(securityInterceptor());
                  }
      

      【讨论】:

        【解决方案4】:

        我遇到了一些问题。我将 ServletRegistrationBean 更改为自定义 url。

        new ServletRegistrationBean(servlet, "/cusom_url_mapping/*");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-26
          • 1970-01-01
          • 2018-04-27
          • 2019-04-25
          • 1970-01-01
          • 1970-01-01
          • 2020-12-03
          • 1970-01-01
          相关资源
          最近更新 更多