【发布时间】:2017-04-16 01:44:56
【问题描述】:
我正在使用 Spring Boot 开发一个 Spring REST 应用程序。我正在使用 Angular JS 客户端使用 Spring REST API。
我正在使用的堆栈是:
Spring Boot 1.4.2 , Spring Security , Angular JS , Tomcat 8
我的问题是:
1) 登录时,用户成功通过身份验证。自定义身份验证成功处理程序返回 200 状态码。
2)认证成功后,当clint再次发送请求访问受保护的资源时,HttpSession为NULL
3)因此当SecurityContextPersistenceFilter 尝试从HttpSession 检索SecurityContext 时,它会返回为null 并且
然后服务器再次向/login资源发送请求
4)所以我的问题是:
1) 为什么第二个请求的 httpsesion 为空?
2)我观察到 Spring security 在第一次成功验证后返回 JSESSIONID 作为 cookie。
3)但是在那之后,客户端不会在请求中发送这个JSESSIONID,因此第二个请求不会在同一个会话中。
4) 如果是这样的话,如果SESSION 不成立,如何检索SecurityContext?
请帮忙,因为我无法在这里继续
编辑 1:
我正在使用 spring security 提供的默认内存用户。我的安全配置如下:
@Configuration
@EnableWebSecurity
@ComponentScan({"com.atul.security"})
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private RESTAuthenticationSuccessHandler authenticationSuccessHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().successHandler(authenticationSuccessHandler)
.and()
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS,"/*").permitAll()
.antMatchers("/index.html", "/home.html", "/login.html", "/").permitAll()
.anyRequest().authenticated()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
我作为经过身份验证的用户低于用户:
org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc:
Principal: anonymousUser; Credentials: [PROTECTED];
Authenticated: true;
Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364:
RemoteIpAddress: 0:0:0:0:0:0:0:1;
SessionId: null;
Granted Authorities: ROLE_ANONYMOUS
请求现在失败:AbstractSecurityInterceptor:AccessDecisionVoter 为 authenticated 表达式返回 false 并抛出拒绝访问异常
【问题讨论】:
-
您是否禁用了 csrf 令牌安全性?
-
是的,我在配置 Spring Security 时这样做了
-
@Atul : 你知道这个意思吗 :and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
-
是的。这意味着spring security永远不会创建会话,这意味着基于表单的登录将不起作用。我将删除 STATELESS 并尝试