【问题标题】:AngularJS and Spring Security. How to handle AngularJS Urls with Spring SecurityAngularJS 和 Spring Security。如何使用 Spring Security 处理 AngularJS 网址
【发布时间】:2013-07-16 08:34:47
【问题描述】:

让我解释一下我的问题。 我在 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 Security,以便捕获每个调用并检查它是否具有 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>

此配置检查每个调用的 url,如果用户具有正确的 ROLES,并且它工作正常,则抛出 401 Unauthorized page。

我遇到的问题是,当我让每个人都可以访问登录页面时,我会这样做:

<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>

但我不知道为什么 spring security 没有捕捉到这个 URL。也许 Angular 以不同的方式管理 URL。

最后,我尝试删除 &lt;security:intercept-url pattern="/**" access="ROLE_USER" /&gt; 并仅授予 /login** 对 ROLE_USER 的访问权限,但未找到此页面。有人知道这里会发生什么吗?

提前致谢!!!

【问题讨论】:

  • 所以这真的与 Angular 无关
  • 你知道#之后的所有URL部分都只能被浏览器看到吗? Angular 更改了地址栏中的 URL,但没有向服务器发出请求。向服务器发出的唯一请求是用于获取部分和调用 REST 服务的 AJAX 请求。这就是您必须保护的东西:REST 服务。

标签: spring url angularjs spring-security


【解决方案1】:

我写了一点sample application,说明了如何通过将会话 ID 公开为 HTTP 标头 (x-auth-token) 来将 AngularJS 与 Spring Security 集成。该示例还提供了一些(简单的)授权(从服务器返回角色),以便客户端 AngularJS 应用程序可以对此做出反应。这当然主要是出于用户体验 (UX) 的目的。始终确保您的 REST 端点具有属性安全性。

我的博文是 here

【讨论】:

    猜你喜欢
    • 2016-03-02
    • 2017-07-09
    • 2020-03-13
    • 2016-06-16
    • 2016-07-11
    • 2014-04-13
    • 2015-12-19
    • 2015-11-13
    • 1970-01-01
    相关资源
    最近更新 更多