【问题标题】:Spring WS not accepting basic auth credentialsSpring WS 不接受基本身份验证凭据
【发布时间】:2020-12-03 09:58:26
【问题描述】:

我已经为此苦苦挣扎了一段时间,无法弄清楚我做错了什么。

我应该在 Spring 中为我的 SOAP Web 服务添加基本身份验证。我使安全配置非常简单(可能太简单了),所以它只专注于基本身份验证(见下文)。

当我从浏览器访问基本 URL 时,身份验证似乎正在工作,它要求提供凭据,如果我正确提供它们,它就会接受它们。

但是,当我想将包含基本身份验证标头的 SOAP 请求发送到我的 Web 服务端点时,Spring Security 会向我发回 401。我尝试使用 SOAPUI、Postman 和通过 Invoke-WebRequest 从 Windows Powershell 发送请求,结果是相同的,而如果我使用 Wireshark 捕获请求,则正确的标头就在那里。

我在这个项目中使用 Spring Boot 2.1.8(与 Spring Web 服务和安全版本相同)。

安全配置类:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("foo")
            .password(passwordEncoder().encode("bar"))
            .roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .anyRequest().authenticated()
            .and().httpBasic();
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

据我了解,我不需要向 Web 服务配置 本身添加任何特定内容,因此所有相关的基本身份验证设置都可以在 security config 类中完成。还是我错了?

感谢您的帮助。

更新

这是请求/响应对:

请求:

POST /foo/endpoint/ HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "http://foo.bar"
Authorization: Basic Zm9vOmJhcg==
Content-Length: 9688
Host: localhost:1502
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:foo="http://foobar.com/">
   <soapenv:Header/>
   <soapenv:Body>
   // body omitted
   </soapenv:Body>
</soapenv:Envelope>

回应

HTTP/1.1 401 
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
WWW-Authenticate: Basic realm="Realm"
Content-Length: 0
Date: Thu, 13 Aug 2020 14:22:29 GMT

【问题讨论】:

  • 在浏览器控制台中是否只有 401 错误?还是其他类似 CORS 政策的东西?并且您可以添加您尝试通过邮递员发送的带有 xml 和标题的屏幕吗?
  • 我已经更新了我的帖子

标签: spring-boot basic-authentication spring-ws


【解决方案1】:

configure(HttpSecurity http) 需要进行一些修改才能在您的代码中启用 http 基本身份验证。

authorizeRequests() 用于授权目的。用户成功登录后,登录用户应该可以访问的所有资源(端点)由 authorizeRequests() 定义。我还建议,在授权端点时,您最好使用 antMatchers。例如:我将上面的代码修改为

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter 
{

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
            .withUser("foo")
            .password(passwordEncoder().encode("bar"))
            .roles("USER");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic().and()
            .authorizeRequests()
            .antMatchers("/simple/**").hasRole("USER");
}

@Bean
PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
} 
}

我希望这对你有用。尝试并告诉我。

注意:在 Spring boot 中,只需添加 spring-boot-starter-security 依赖项,即可启用安全性,无需任何配置。所以现在,您尝试重新配置基本的自动配置。通过扩展,WebSecurityConfigurerAdapter 类。没错。

POSTMAN:使用授权标头

【讨论】:

  • 感谢您的回复。如果我相应地修改我的配置(用我的端点路径替换“/simple/**”),我会从服务器获得 403。 :)
  • 也就是说,您已成功通过身份验证。只有授权失败。如果可能的话,你能发布控制器代码吗?
猜你喜欢
  • 2018-03-25
  • 2021-04-22
  • 2017-11-11
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 2012-09-13
相关资源
最近更新 更多