2012-03-28 67 views
7

我有安裝彈簧的安全和運作良好的應用程序 - 它是目前運行的www.exampledomain.comJava spring security - 攔截不同登錄的子域名url?

我現在想擴大用完一個子域的應用。例如newapp.exampledomain.com

唯一的問題是,這個新的應用程序,用戶需要登錄。在春天它是很容易通過<intercept-url pattern="/Admin/*" access="ROLE_GENERAL"/>

攔截的URL,但是當你想攔截的登錄一個子你會怎麼做?例如以下不工作對我來說:

<intercept-url pattern="http://newapp.exampledomain.com/*" access="ROLE_GENERAL"/> 

任何想法如何解決這個問題?

+0

您使用的是相同的Web應用程序中的子域,而不是一個不同的web應用程序? – Qwerky 2012-03-28 15:19:53

+0

您可能會覆蓋'FilterSecurityInterceptor'來添加子域檢查,但它聞起來像一個黑客,而不是一個真正的解決方案。 Upvote您的問題開箱即用的解決方案。另外,你可以更詳細地瞭解一下web-app/web-container子域配置。 – alexkasko 2012-03-28 18:11:28

+0

葉使用不同的子域的相同的應用程序,不同的子域代表不同的功能給用戶,但在同一個服務器處理它(用於成本效益的原因)。至於web應用程序容器子域配置,過濾器用於查找調用的子域,然後將請求轉發到該域功能的相關操作。想象一下free.domain.com,它們都可以訪問,而pro.domain.com則是一個更好的外觀和感覺,增加了功能。 – 2012-04-17 10:32:40

回答

2

一個辦法是寫自己的AccessDecisionVoter延伸RoleVoter,並增加了基於主機名額外的檢查。事情是這樣的:

public class MyVoter extends RoleVoter { 
    public int vote(Authentication authentication, 
       java.lang.Object object, 
       java.util.Collection<ConfigAttribute> attributes) { 
    FilterInvocation filterInvocation = (FilterInvocation) object; 
    HttpRequest request = filterInvocation.getHttpRequest(); 
    // get subdomain from request 
    String subdomain = getSubdomain(request); 
    if ("free".equals(subdomain)) { 
     return ACCESS_GRANTED; 
    } 
    else { 
     super.vote(authentication, object, attributes); 
    } 
    } 
} 

再用鐵絲您的選民:

<security:http auto-config="true" 
       use-expressions="true" 
       access-decision-manager-ref="accessDecisionManager"> 
... 
</security:http> 

<bean id="accessDecisionManager" 
     class="org.springframework.security.access.vote.UnanimousBased"> 
    <property name="decisionVoters"> 
     <list> 
      <bean class="com.acme.MyVoter" /> 
     </list> 
    </property> 
</bean> 

如果你想採取這一步,你也可以寫自己的configuration attributes這樣可以讓你刪除硬編碼的主機名檢查中選民並做類似的事情:

<intercept-url pattern="/Admin/*" access="ROLE_GENERAL" domain="free.acme.com" /> 
+0

在spring-security版本中,intercept-url標記永遠不會獲取名爲「domain」的屬性。使用選民可能是事實,但這種解決方案永遠不會工作。 – tugcem 2015-04-29 08:49:33

1

在你的會話cookie中,域應明確設置爲exampledomain.com。

應用服務器負責會話cookie創建(JSESSIONID),但不是春季安全。

所有你需要做的就是告訴你要總是在cookie中的同一個域中的應用服務器。

添加到你的web.xml:

<session-config> 
     <cookie-config> 
      <domain>exampledomain.com</domain> 
     </cookie-config> 
    </session-config>