我的项目使用Spring Security + Spring MVC并处理websocket(over stomp + sockjs)。
当我测试sockjs回退时,XHR请求返回403。
如果我在安全上下文中关闭csrf,那就OK了
如果我像这样覆盖sockjs AbstractXHRObject构造函数:它没关系:
function AbstractXHRObject(method, url, payload, opts) {
debug(method, url);
var self = this;
EventEmitter.call(this);
// HACK : add csrf headers
opts["headers"][csrfHeader] = csrfToken;
setTimeout(function () {
self._start(method, url, payload, opts);
}, 0);
}
当然我必须检查opts是否为空...
我的问题是:在SockJS的XHR后备对象上添加标题的方法是什么?
由于
答案 0 :(得分:1)
最后,我在安全上下文中为sockjs URL禁用了CSRF。
public class CsrfSecurityRequestMatcher implements RequestMatcher {
private Pattern allowedMethods = Pattern.compile("^(GET|HEAD|TRACE|OPTIONS)$");
private AntPathRequestMatcher unprotectedMatcher = new AntPathRequestMatcher("/ws/**/xhr_send**");
@Override
public boolean matches(HttpServletRequest request) {
if (allowedMethods.matcher(request.getMethod()).matches()) {
return false;
}
return !unprotectedMatcher.matches(request);
}
}
和匹配者:
"...ERROR -- : private method `require' called for
#<ActiveSupport::HashWithIndifferentAccess:..."
注意:它遵循http://blogs.sourceallies.com/2014/04/customizing-csrf-protection-in-spring-security/