2014-05-19 24 views
7

我的web應用程序有一堆「正常」資源(html頁面等)以及一些由前面提到的html頁面從JavaScript調用的REST資源。配置Spring Security爲REST URL返回403並重定向到其他URL的登錄

如果有會話超時,用戶將被重定向到登錄表單。對於「普通」資源來說這很好,但對於REST資源則不是。我只需要一個403響應,以便JavaScript可以接管並要求用戶重新進行身份驗證。

在網絡上有無數的例子如何配置每一個,但我找不到如何組合這些方法的例子。我所有的API網址都以「/ api /」開頭,因此我需要所有這些URL的403以及所有其他URL的重定向。我如何設置?

回答

6

我花了一點時間研究Spring源代碼以使其發揮作用。您可以設置一個身份驗證入口點,如下所示:

<bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint"> 
    <!-- this is the configuration for /api/ URLs --> 
    <constructor-arg> 
     <map> 
      <entry> 
       <key> 
        <bean class="org.springframework.security.web.util.matcher.RegexRequestMatcher"> 
         <constructor-arg value="^/api/.*" /><!-- match URLs starting with "/api/" --> 
         <constructor-arg><null /></constructor-arg><!-- no matter what the HTTP method is --> 
        </bean> 
       </key> 
       <!-- if the key above has matched, send 403 response --> 
       <bean class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint" /> 
      </entry> 
     </map> 
    </constructor-arg> 

    <!-- and in the default case just redirect to login form --> 
    <property name="defaultEntryPoint"> 
     <bean class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> 
      <constructor-arg value="/spring_security_login" /> 
     </bean> 
    </property> 
</bean> 

這可隨後在自旋微觀安全配置中使用:

<http ... entry-point-ref="authenticationEntryPoint"> 
0

我認爲你應該有兩個不同的<http pattern="{...}" ...>實體,因爲,好的,你解決了重定向的問題,但是關於csrf保護呢?還有其他問題,我想不起我的頭頂。

相關問題