【发布时间】: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