2016-08-13 96 views
7

我對Spring安全性有最基本的想法。我想爲應用程序開發多個微服務,我希望使用可以在所有微服務中使用的一點身份驗證和授權。爲了使它更具體,我想創建一個通用的過濾器,只能在每個微服務應用程序的web.xml中使用。我不想在所有微服務中使用以下內容。如何使用Spring Security爲微服務創建自定義安全篩選器

彈簧security.xml文件

<http> 
     <intercept-url pattern="/index*" access="ROLE_USER" /> 
     <access-denied-handler error-page="/customaccessdenied.jsp" /> 
     <intercept-url pattern="/manage*" access="ROLE_ADMIN" /> 
     <form-login default-target-url="/index" always-use-default-target="true"/> 
     <logout/> 
     <!-- <remember-me/> --> 
    </http> 

    <authentication-manager> 
     <authentication-provider> 
      <user-service> 
       <user name="admin" password="admin" authorities="ROLE_ADMIN,ROLE_USER" /> 
       <user name="deb" password="deb" authorities="ROLE_USER" /> 
      </user-service> 
     </authentication-provider> 
    </authentication-manager> 



web.xml 
======= 
<!-- Spring Security --> 
    <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> 

我不想使用上述所有微服務。我的應用程序應該看起來像圖像下面。

enter image description here

請幫助我如何去實現它,如果您有任何示例應用程序,請給我提供的鏈接。整個問題的重點是將所有安全相關的東西放在一個地方,並使用該庫和自定義篩選所有微服務的web.xml。

我想要實現這樣的

<filter> 
     <filter-name>mySecurityFilterChain</filter-name> 
     <filter-class>com.world.india.DelegatingFilterProxy</filter-class> 
    </filter> 

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

請我需要你在這方面的幫助。希望這會對其他人有所幫助。

+2

對我**「我想創造一個只能在每一個微服務應用程序的web.xml中使用的通用篩選我做的。不想在所有微服務中使用以下內容「**聽起來像是一個矛盾。你能否詳細說明一下。 – jah

+0

您需要一個單獨的服務進行身份驗證,以便可以將所有未經授權的請求定向到可以暴露REST API或LogIn頁面的請求。所有其他微服務和Web應用程序只應只允許授權請求。這可以在配置文件中設置。這樣,所有與授權相關的代碼都將駐留在單個模塊中。 –

回答

0

您可以簡單地將所有視圖放置在文件夾中,並將訪問權限設置爲某個角色。 因此,與/服務的任何請求/ *可以重定向到一個登錄界面,而其他可能不需要身份驗證(/ *)

<http pattern="/resources/**" security="none" /> 
    <http> 
    <intercept-url pattern="/service/*" access="ROLE_ADMIN" /> 
    <access-denied-handler error-page="/customaccessdenied.jsp" /> 
    <intercept-url pattern="/manage*" access="ROLE_ADMIN" /> 
    <form-login default-target-url="/index" always-use-default-  target="true"/> 
    <logout/> 
    <!-- <remember-me/> --> 
</http> 

您可以設置自定義的安全配置以下這tutorial

0

這是一個很少有人不清楚你在問什麼:從你的圖像看來,所有的微服務都會駐留在同一個web應用中,由相同的web.xml配置,但是你的代碼片斷表明你的微服務將駐留在單獨的web應用中,這確實會使總體感覺。

無論哪種方式:只需添加一個根應用上下文在web.xml,其中指定的安全配置:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:/common-security-config.xml</param-value> 
</context-param> 

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

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

common-security-config.xml將是在同一個網絡的所有子應用程序上下文的基本應用程序上下文應用程序(在你的情況下:所有的微服務等)。把你的Spring Security配置放在那裏,你應該沒問題。

如果你所有的微服務都駐留在同一個web應用程序中,這就是你所需要做的;如果沒有,只需創建一個通用模塊,其中包含安全配置(在src/main/resources中)以及安全配置可能需要的任何支持類。添加對該模塊的引用並在所有微服務模塊的web.xml中添加公共配置。

請注意,無論哪種方式,如果您想要使用方法級安全性(例如使用@Secured),則需要將此配置包括在每個子微服務的配置中。

0

從本質上講,你的問題聽起來像你想要一個可重用的spring安全java配置(而不是spring-security.xml)。如果是這樣,您可以創建自定義的安全依賴關係,或者轉向安全即服務。

依賴關係:將您的安全實現置於jar(例如com.world.india.DelegatingFilterProxy)中,並將此jar作爲依賴添加到您的每個服務中。通過這種方式,所有安全更改都會隔離到不同的模塊,並且每次升級安全性時,對其他微服務的唯一更改就是依賴版本。

服務:也將您的安全應用程序部署爲服務。現在,問題被簡化爲在您自己的兩個服務(即安全微服務和其他微服務)之間建立安全握手。使用這種方法,對您的安全服務的升級是無縫和透明的(如果您使用服務發現機制)到其他服務(例如A-MicroService)使用它。

有關非基於XML的配置提示,檢查這個答案:Configure Spring Security without XML in Spring 4

相關問題