繁体   English   中英

带有Spring Security的AngularJS Web应用程序

[英]AngularJS Web Application with Spring Security

我正在AngularJS应用程序中实现Spring Security。 我对这两种技术都比较陌生,我找到了几个非常有用的网站,其中包含如何实现AngularJS和Spring Security的教程和示例。

我目前的问题在于限制某些用户的URL路径。 这听起来像一个简单的问题,但我已经淹没了自己的文档,试图弄清楚以前必须解决的问题。

在AngularJS中,导航到不同的URL时URL中有一个哈希标记,这似乎导致Spring出现问题。 没有抛出错误,但资源不受限制。 我的代码如下:

web.xml中

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/paperwebapp-servlet.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>webapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>*</param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>webapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Apply Spring Security Filter to all Requests -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

APP-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

<context:property-placeholder location="file:${catalina_home}/conf/application.properties" />

<mvc:view-controller path="/" view-name="/resources/index.html"/>
<mvc:resources mapping="/resources/**" location="/resources/" />

<import resource="spring-security.xml" />

</beans>

弹簧security.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:sec="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security.xsd">

<context:property-placeholder location="file:${catalina_home}/conf/application.properties" />

<sec:http auto-config='true'>
    <sec:intercept-url pattern="/access/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
    <sec:intercept-url pattern="/*" access="ROLE_USER" />
    <sec:intercept-url pattern="/#/inventory" access="ROLE_ADMIN" />

    <sec:form-login login-page="/access/login.jsp" default-target-url="/#/splash"
        always-use-default-target="true" />
</sec:http>

<sec:authentication-manager alias="authenticationManager">
    <sec:authentication-provider>
        <sec:user-service>
            <sec:user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
            <sec:user name="user" password="user" authorities="ROLE_USER" />
        </sec:user-service>
    </sec:authentication-provider>
</sec:authentication-manager>
</beans>

部署应用程序后,我可以访问URL(localhost:8080 / app),并按照预期欢迎使用登录页面。 一旦我通过身份验证,我也会按照预期进入启动画面(/#/ splash)。 但是,如果我使用“用户”凭据登录,我应该被限制在/ inventory路径中。 无论我尝试什么(/#/库存,/库存,#/库存等),我都无法限制资源。 我已经在一个直接从应用程序目录访问HTML页面的应用程序上测试了这个配置,它似乎运行正常,因此我确信它与AngularJS控制器路由请求和使用该哈希标记有关。

我在研究这个问题时发现的另一个兴趣点是,因为我们使用来自多个源的模板来编译每个页面,所以我们不能使用$ locationProvider来设置HTML5模式而不会破坏应用程序。

如果有人对此问题有任何见解,我们将不胜感激。 我确信这已经存在,但对于我的生活,我找不到任何东西。 谢谢!

您似乎将Angular的客户端URL处理(基于散列,如'/#/ splash')与Spring MVC(以及Spring Security的)服务器端URL处理混淆。

请记住,Spring Security可以保护对服务器端URL的访问,而Angular是一个单页的客户端库。

当您第一次访问Angular页面时,您将从服务器获取它,并且Spring Security可以根据登录限制访问。

如果您仍然在客户端上的单页Angular环境中,并通过Angular导航到“页面”并在URL中使用哈希(请参阅有关hashbang URLAngularJS $位置指南 ),那么您不会发出服务器请求。 您正在请求Angular呈现不同的模板或状态。 这是客户端行为,因此不涉及Spring Security。 当您访问模板HTML文件(Spring可能无需身份验证而静态返回)或者您已设置REST api以从服务器获取数据(通常返回要为您的应用程序使用的JSON格式数据)时,您会发出服务器请求。

我相信,为了使这项工作,受保护资源(“/ inventory”)的数据不必包含在主应用程序中,需要单独的服务器端资源。 这可以包括页面的HTML模板和/或数据。 您的AngularJS应用程序应该能够识别出该资源不可用,并向用户显示一些内容以传达缺少授权的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM