通过spring security登录后接收sessionid

通过spring security登录后接收sessionid,spring,spring-security,dart,dart-polymer,spring-session,Spring,Spring Security,Dart,Dart Polymer,Spring Session,我正在编写一个小应用程序,它有一个基于spring boot rest的后端和一个基于聚合物镖的前端 目前我正在努力从登录机制接收会话id 最初,我计划使用HeaderHttpSessionStrategy,并使用它对spring会话/redis会话存储进行身份验证,以实现水平扩展 问题在于,spring security总是在登录之后和dart中的HttpRequest类(来自html包)中执行重定向,并且在重定向之后,来自初始响应的头字段当然不再存在/可访问。 我试图为客户端禁用follow

我正在编写一个小应用程序,它有一个基于spring boot rest的后端和一个基于聚合物镖的前端

目前我正在努力从登录机制接收会话id

最初,我计划使用
HeaderHttpSessionStrategy
,并使用它对spring会话/redis会话存储进行身份验证,以实现水平扩展

问题在于,spring security总是在登录之后和dart中的
HttpRequest
类(来自html包)中执行重定向,并且在重定向之后,来自初始响应的头字段当然不再存在/可访问。 我试图为客户端禁用followRedirect,但它似乎仅适用于IO包
HttpRequest

然后我尝试切换到
CookieHttpSessionStrategy
,但dart httprequest似乎没有在浏览器cookie后端存储收到的cookie:-/

这应该是一个简单的问题,但我有点迷路了。这不会那么难的

例如,当我使用intellij rest客户端时,一切正常,我可以从cookie或header字段提取会话id


有什么想法吗-/

看起来dart总是在浏览器中遵循这些重定向,我没有机会在第一个请求的头中检索cookie或令牌

因此,作为替代解决方案,我下一步将尝试这样做:

在spring security中激活基本身份验证:

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/", "/logout").permitAll()
                .anyRequest().authenticated()
                .and()
                .httpBasic()
                .and()
                .sessionManagement().sessionFixation().changeSessionId()
                .and()
                .csrf().disable();
    }
...
}
在一个控制器中,我将这样的内容作为受保护的资源:

@RequestMapping(value = "login", method = RequestMethod.POST)
public String login() {
    return RequestContextHolder.currentRequestAttributes().getSessionId();
}
通过这种方式,您可以获得会话id作为常规结果。因此,我可以简单地从响应主体获取ID,并在提供正确的基本身份验证凭据一次后使用它

这应该只在受信任的环境中使用,或者通过https使坏人更难嗅探凭据或会话

所以基本上这就是我登录时要做的:

void login() {
  Map<String, String> headers = {};
  authorize(headers);
  HttpRequest.request(LOGIN_URL, method: "POST", requestHeaders: headers)
  .then((request) => processLogin(request))
  .catchError((e) => processLoginError(e));

}

void processLogin(HttpRequest request) {
  sessionController.sessionId=request.responseText;
  mainApp.showHome();
}

void processLoginError(var e) {
  print("total failure to login because of $e");
}
String authorization() {
  String auth = window.btoa("$username:$password");
  return "Basic $auth";
}

void authorize(Map<String, String> headers) {
  headers.putIfAbsent("Authorization", () => authorization());
}
您也可以让它与cookie一起工作,但我喜欢
HttpRequest.request()
,这样就更容易使用头字段。

这将教您如何在成功或失败的身份验证后阻止Spring Security重定向

身份验证应返回200而不是301
失败的身份验证应返回401而不是302

实际上,我认为教程中的代码过于复杂。对于
MySavedRequestAwareAuthenticationSuccessHandler
类,只需在
onAuthenticationSuccess
函数中插入一行即可:

clearAuthenticationAttributes(request);

最近有一次讨论可能与我看到的有关,但它似乎使用了浏览器中不可用的dart:io:-(看起来编写自己的登录筛选器更容易,只是不重定向:-/
clearAuthenticationAttributes(request);