2012-02-21 61 views
1

我的grails web應用程序有兩個部分:一個用於桌面/膝上型計算機瀏覽器,一個用於使用jquery-mobile的移動設備。移動部分位於子樹/移動/ *中。使用彈簧安全我想有不同的控制器/視圖登錄,註銷等我沒有發現任何有用的提示研究這個話題在網上。Grails項目中的不同登錄控制器與Spring安全

我現在想到的唯一選擇是將移動應用程序解壓到自己的Grails項目中,然後強制我將通用邏輯提取到grails插件中,然後將這些插件強制給完全不同的開發和部署設置等...我寧願保持移動和非移動部分在同一個應用程序,但無法弄清楚如何。

知道的任何建議...

回答

1

試試這一個又......

我一直在努力類似最近爲好。和你一樣有'想要'。這是我最終的結果。

要指向不同的登錄屏幕,我重寫了安全篩選器鏈(Authentication安全性)的AuthenticationEntryPoint步驟。我使用了Spring-Mobile插件使用的相同邏輯。 (事實上​​,你必須安裝spring-mobile插件才能工作)deviceResolver由該插件連接。

package com.myapp.security 

import org.codehaus.groovy.grails.plugins.springsecurity.AjaxAwareAuthenticationEntryPoint 
import javax.servlet.http.HttpServletRequestimport javax.servlet.http.HttpServletResponse 
import org.springframework.security.core.AuthenticationException 

class MyAppAuthenticationEntryPoint extends AjaxAwareAuthenticationEntryPoint { 
    def mobileLoginFormUrl 
    def deviceResolver 

    @Override 
    protected String determineUrlToUseForThisRequest(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { 
     if (deviceResolver.resolveDevice(request).isMobile()) 
     { 
      return mobileLoginFormUrl 
     } 
     return super.determineUrlToUseForThisRequest(request, response, e) 
    } 
} 

有線,像這樣在resources.groovy

authenticationEntryPoint(com.myapp.security.MyAppAuthenticationEntryPoint) { 
    loginFormUrl = conf.auth.loginFormUrl 
    forceHttps = conf.auth.forceHttps 
    ajaxLoginFormUrl = conf.auth.ajaxLoginFormUrl 
    useForward = conf.auth.useForward 
    portMapper = ref('portMapper') 
    portResolver = ref('portResolver') 
    deviceResolver = ref('deviceResolver') 
    mobileLoginFormUrl = conf.auth.mobileLoginFormUrl 
} 

Config.groovy中配置線

grails.plugins.springsecurity.auth.loginFormUrl = '/register' 
grails.plugins.springsecurity.auth.mobileLoginFormUrl = '/mobile/login' 

我也寫了我AuthenticationSuccessHandler一步,迫使移動用戶到移動登陸頁面登錄後。

package com.myapp.security 

import javax.servlet.http.HttpServletRequest 
import javax.servlet.http.HttpServletResponse 
import org.codehaus.groovy.grails.plugins.springsecurity.AjaxAwareAuthenticationSuccessHandler 
import org.springframework.security.web.savedrequest.RequestCache 

class MyAppAuthenticationSuccessHandler extends AjaxAwareAuthenticationSuccessHandler { 
    def mobileTargetUrl 
    def deviceResolver 
    RequestCache requestCache 

    @Override 
    protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) { 
     if (isMobile(request)) 
     { 
      return mobileTargetUrl 
     } 
     return super.determineTargetUrl(request, response) 
    } 

    @Override 
    void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, org.springframework.security.core.Authentication authentication) {   
     if (isMobile(request)) 
     { 
      // we always want to go to the mobile landing page here. 
      requestCache.removeRequest(request, response); 
     } 
     super.onAuthenticationSuccess(request, response, authentication) 
    } 

    private boolean isMobile(request) { 
     deviceResolver.resolveDevice(request).isMobile() 
    } 

    @Override 
    void setRequestCache(RequestCache requestCache) { 
     super.setRequestCache(requestCache) 
     this.requestCache = requestCache 
    } 

} 

這不從瀏覽到一個非移動網頁停止用戶,但它確實迫使他們登錄後的/移動/索引。形成我的手機頁面上的所有鏈接都指向其他手機頁面。

+0

感謝您的詳細示例。當我再次遇到問題時,我會嘗試它。這次我花了很長時間將項目分成一個插件(主要包含域類)和兩個grails項目(一個用於移動設備,另一個用於其他設備)。這花了一個下午,但它現在允許我去標準配置。 – johanneslink 2012-02-27 09:27:45

2

我會做這兩個選項之一:

  • 有一個默認的控制器,它會根據客戶端瀏覽器(或別的東西)重定向到mobileLoginlogin控制器
  • 使用一個登錄控制器但是CSS定製顯示器(然後你就可以在你的defaultTargetUrl做重定向到桌面/移動控制器,如果你要)
+0

試過你的第一個選項,但找不到正確的東西來發送。如果重定向網址位於移動/分支內,它應該重定向到移動設備。 'params.redirect'爲空,請求似乎也不包含重定向url。 – johanneslink 2012-02-22 08:27:17

1

你看過spring-mobile插件嗎?

允許您在需要在標準和移動設備之間切換的控制器中執行類似以下的操作。

def login() { 
    def view = 'login' 

    withMobileDevice { 
     view = 'mobile/login' 
    } 

    render view: view 
} 
+0

是的,我見過這個插件。同樣的點適用於:我不想調用一個不同的視圖,取決於設備,但對於網站的某個子部分。 – johanneslink 2012-02-25 17:37:43

相關問題