Pat*_*sen 6 spring spring-security dart dart-polymer spring-session
我正在写一个小应用程序,它有一个基于弹簧支架的后端和一个基于聚合物飞镖的前端.
目前我正在努力从登录机制接收会话ID ...
最初我计划使用它HeaderHttpSessionStrategy并使用它来对spring-session/redis会话存储进行身份验证以启用水平扩展.
问题是Spring安全性总是在登录后和dart HttpRequest类(来自html包)中执行重定向,并且在重定向之后,来自初始响应的头字段当然不再存在/可访问.我试图为客户端禁用followRedirect,但看起来这只适用于IO包HttpRequest.
然后我尝试切换到CookieHttpSessionStrategy但看起来dart httprequest不会在浏览器cookie后端存储收到的cookie: - /
这应该是一个简单的问题,但我在这里有点迷失.这不是那么难......
当我使用例如intellij rest客户端时,一切正常,我可以从cookie或头字段中提取会话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();
}
...
}
Run Code Online (Sandbox Code Playgroud)
在一个控制器中,我有这样的东西作为受保护的资源:
@RequestMapping(value = "login", method = RequestMethod.POST)
public String login() {
return RequestContextHolder.currentRequestAttributes().getSessionId();
}
Run Code Online (Sandbox Code Playgroud)
这样你就可以得到你的会话 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());
}
Run Code Online (Sandbox Code Playgroud)
要发送请求,HeaderHttpSessionStrategy我可以这样做:
void updateUserData(){
_logger.info("Updating user data");
Map<String, String> headers = {"Accept": "application/json", 'x-auth-token':sessionId};
HttpRequest.request(USER_URL, method: "GET", requestHeaders: headers)
.then((request) => processUserData(request))
.catchError(ErrorHandler.handleHttpErrorGeneric);
}
Run Code Online (Sandbox Code Playgroud)
您还可以让它与 cookie 一起使用,但我喜欢这样,因此HttpRequest.request()使用标头字段更容易。
| 归档时间: |
|
| 查看次数: |
3101 次 |
| 最近记录: |