試試這一個又......
我一直在努力類似最近爲好。和你一樣有'想要'。這是我最終的結果。
要指向不同的登錄屏幕,我重寫了安全篩選器鏈(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
}
}
這不從瀏覽到一個非移動網頁停止用戶,但它確實迫使他們登錄後的/移動/索引。形成我的手機頁面上的所有鏈接都指向其他手機頁面。
感謝您的詳細示例。當我再次遇到問題時,我會嘗試它。這次我花了很長時間將項目分成一個插件(主要包含域類)和兩個grails項目(一個用於移動設備,另一個用於其他設備)。這花了一個下午,但它現在允許我去標準配置。 – johanneslink 2012-02-27 09:27:45