【发布时间】:2015-02-21 19:09:06
【问题描述】:
我怎样才能使端点只对匿名用户可访问?我试过了
http
.authorizeRequests()
.regexMatchers(HttpMethod.POST, "^/users$")
.anonymous()
和
http
.authorizeRequests()
.regexMatchers(HttpMethod.POST, "^/users$")
.hasAnyAuthority("ROLE_ANONYMOUS");
但我总是收到以下消息:
An Authentication object was not found in the SecurityContext
如果这很重要,我的最后一条规则是
http
.authorizeRequests()
.anyRequest()
.denyAll();
以便默认禁止访问每个未配置的端点。
更新:按照 Dave Syer 的建议,我将每个规则合并到同一个链中,但我一直收到相同的消息。这些是我现在的规则:
http.authorizeRequests()
.regexMatchers(HttpMethod.POST, "^/users$")
.anonymous()
.regexMatchers(HttpMethod.GET, "^/me$")
.hasAnyAuthority("ROLE_ADMIN", "ROLE_USER")
.anyRequest()
.denyAll();
有什么建议吗?
【问题讨论】: