我的grails网络应用包含两个部分:一个用于台式机/笔记本电脑浏览器,一个用于使用jquery-mobile的移动设备。移动部分位于子树/ mobile / *中。使用Spring Security,我希望具有用于登录,注销等的不同 Controller / View 。在网络上研究此主题时,我还没有发现任何有用的提示。
我目前能想到的唯一选择是将移动应用程序提取到其自己的grails项目中,然后将迫使我将通用逻辑提取到grails插件中,然后将迫使我进行完全不同的开发和部署设置,因此在...上,我宁愿将移动和非移动部分保留在同一应用程序中,但无法弄清楚该怎么做。
任何建议表示赞赏...
最佳答案
再试一次...
我最近也一直在做类似的事情。并拥有与您相同的“渴望”。这就是我最后得到的。
为了指向不同的登录屏幕,我改写了安全过滤器链(spring security)的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
}
}
这不会阻止用户浏览到非移动页面,但是会在登录后强制他们进入/ mobile / index。从那里,我在移动页面上的所有链接都引用了其他移动页面。
关于grails - 具有Spring安全性的Grails项目中的不同登录 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9383541/