【发布时间】:2018-01-07 04:40:48
【问题描述】:
我无法解决这个问题。
我尝试验证登录注销示例here。登录正常,但是当我尝试注销时,浏览器给出 NetworkError : 403 disabled localhost:8080/logout is disabled.
在我看来,我应该在 ui 端的每个请求中添加令牌标头。但我不知道该怎么做?
这里是浏览器开发者工具消息:
发布 403 {"timestamp":1501570024381,"status":403,"error":"Forbidden","message":"Invalid CSRF Token 'null' 是 在请求参数 '_csrf' 或标头 'X-CSRF-TOKEN' 上找到。","path":"/helpdesk/logout"}
这是我的角度注销功能:
$scope.logout = function() {
$http.post('logout',{}).success(function() {
$rootScope.authenticated = false;
$location.path("/home");
}).error(function(data) {
$rootScope.authenticated = false;
});
}
这是我的 SpringSecurityConfig 配置方法:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic().and()
.authorizeRequests()
.antMatchers("/index.html","/pages/**","/","/webjars/**")
.permitAll()
.anyRequest()
.authenticated().and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll()
.logoutSuccessHandler(logoutSuccess)
.deleteCookies("JSESSIONID").invalidateHttpSession(false)
.and()
.addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class);
}
我该如何解决这个问题?如何将令牌标头添加到所有请求?你能帮帮我吗?
【问题讨论】:
-
您不需要明确地这样做
$rootScope.authenticated = false;或$location.path("/home");。您的配置应该足以注销用户,您应该能够执行简单的发布请求以注销。但是,了解您正在使用的 SpringSec 版本会很有帮助。您可能希望在您的发布请求中包含其他标头。 -
我必须设置 $rootscope.authenticated = false 因为某些视图隐藏或显示此参数的状态。我会尝试这个解决方案,如果它可以工作我会用不同的方式来解决这个问题。谢谢。
-
我尝试了您的解决方案,但没有奏效。我编辑了这个问题。
-
顺便说一句,我检查了您所关注教程的链接。乍一看,他们正在描述如何处理
CSRF令牌。我的回答可能没用 -
您需要在调用中将 csrf 令牌发送到 spring 控制器,因为这就是 Spring 安全性的工作方式。基于这些 csrf 令牌,只有调用得到验证和授权。
标签: java angularjs spring-boot spring-security