让我解释一下我的问题。我在AngularJS中实现了一个网站,访问方式如下:
http://localhost:8080/example/resources/#/
在这里,我们可以调用不同的页面,例如登录页面:
http://localhost:8080/example/resources/#/login
管理页面:
http://localhost:8080/example/resources/#/admin
用户页面:
http://localhost:8080/example/resources/#/user
现在,我在示例中实现了spring安全性,以便捕获每个调用并检查它是否具有ROLE_USER权限。到目前为止,我已经在Spring安全上下文文件中完成了这样的配置:
<security:http create-session="stateless" entry-point-ref="restAuthenticationEntryPoint"
authentication-manager-ref="authenticationManager">
<security:custom-filter ref="customRestFilter" position="BASIC_AUTH_FILTER" />
<security:intercept-url pattern="/**" access="ROLE_USER" />
</security:http>
如果用户具有正确的ROLES,并且运行良好,则此配置会检查调用的每个url,并抛出401未经授权的页面。
我遇到的问题是,当我把登录页面设置为每个人都可以访问时,我会这样做:
<security:http create-session="stateless" entry-point-ref="restAuthenticationEntryPoint"
authentication-manager-ref="authenticationManager">
<security:custom-filter ref="customRestFilter" position="BASIC_AUTH_FILTER" />
<security:intercept-url pattern="/login**" access="ROLE_ANONYMOUS" />
<security:intercept-url pattern="/**" access="ROLE_USER" />
</security:http>
但我不知道为什么springsecurity没有捕捉到这个URL。也许Angular对URL的管理方式不同。
最后,我尝试删除<security:intercept-url pattern="/**" access="ROLE_USER" />
,并只允许/login**访问ROLE_USER,但找不到此页面。有人知道这里会发生什么吗?
提前感谢!!!
我写了一个小示例应用程序,演示了如何通过将会话id公开为HTTP头(x-auth-token)来将AngularJS与Spring Security集成。该示例还提供了一些(简单的)授权(从服务器返回角色),以便客户端AngularJS应用程序可以对此做出反应。当然,这主要是为了用户体验(UX)的目的。始终确保REST端点具有属性安全性。
我的博客文章是这里。