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