【发布时间】:2017-05-08 13:41:19
【问题描述】:
我们要找的场景如下:
- 客户端通过 REST 连接到 REST 登录 url
- Spring 微服务(使用 Spring Security)应该返回
200 OK和一个登录令牌 - 客户端保留令牌
- 客户端使用相同的令牌调用其他 REST 端点。
但是,我看到客户端正在获取 302 和 Location 标头以及令牌。所以它确实进行了身份验证,但带有不需要的 HTTP 响应状态代码和标头。
Spring Security 配置如下所示:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable() // Refactor login form
// See https://jira.springsource.org/browse/SPR-11496
.headers()
.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))
.and()
.formLogin()
.loginPage("/signin")
.permitAll()
.and()
.logout()
.logoutUrl("/signout")
.permitAll()
.and()
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated();
...
}
我尝试添加拦截器和过滤器,但看不到在 Spring 端设置和添加 302 和 Location 的位置。
但是,Location 标头确实显示在客户端收到的响应标头中(连同其余的 Spring Security 标头 LINK):
Server=Apache-Coyote/1.1
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, SAMEORIGIN
Set-Cookie=JSESSIONID=D1C1F1CE1FF4E1B3DDF6FA302D48A905; Path=/; HttpOnly
Location=http://ec2-35-166-130-246.us-west-2.compute.amazonaws.com:8108/ <---- ouch
Content-Length=0
Date=Thu, 22 Dec 2016 20:15:20 GMT
任何建议如何使其按预期工作(“200 OK”,没有 Location 标头和令牌)?
注意:使用 Spring Boot,Spring Security,没有 UI,只是调用 REST 端点的客户端代码。
【问题讨论】:
-
我们要做的是在应用程序使用 POST 和凭据登录时拥有一个 url,并接收一个登录令牌。我不清楚为什么应该有 302。我可能遗漏了一些东西......
-
//客户端调用:new RestTemplate().execute(SIGIN_URL, HttpMethod.POST, new RequestCallback() { ...}, new ResponseExtractor... { public Object extractData(... ) { //状态码现在是 302 headersToUpdate.add("Cookie", response.getHeaders().getFirst("Set-Cookie")); return null; } (没有 UI 和按钮)
-
它正在重定向到一个新的 url
标签: spring rest spring-mvc spring-boot spring-security