Grails项目中的不同登录控制器具有Spring安全性

时间:2012-02-21 19:05:37

标签: grails spring-security

我的Grails网络应用程序有两个部分:一个用于桌面/笔记本电脑浏览器,一个用于使用jquery-mobile的移动设备。移动部分位于子树/移动/ *中。使用spring security我希望有不同的控制器/视图用于登录,注销等。我没有在网上找到任何有用的提示来研究这个主题。

我目前唯一能想到的选择是将移动应用程序解压缩到自己的grails项目中,这将迫使我将常用逻辑提取到grails插件中,这将迫使我进入一个完全不同的开发和部署集等等...我宁愿将移动和非移动部分保留在同一个应用程序中,但无法弄清楚如何。

任何赞赏的建议......

3 个答案:

答案 0 :(得分:2)

我会做以下两种选择之一:

  • 有一个默认控制器,它将根据客户端浏览器(或其他内容)重定向到mobileLoginlogin控制器
  • 使用一个登录控制器,但使用CSS自定义显示(如果必须在defaultTargetUrl中,则可以重定向到桌面/移动控制器)

答案 1 :(得分:1)

你看过spring-mobile插件吗?

允许您在需要在标准和移动设备之间切换的控制器中执行以下操作。

def login() {
    def view = 'login'

    withMobileDevice {
      view = 'mobile/login'
    }

    render view: view
}

答案 2 :(得分:1)

再试一次......

我最近也在做类似的事情。和你一样有“想要”。这就是我最终的目标。

为了指向不同的登录屏幕,我覆盖了安全过滤器链的AuthenticationEntryPoint步骤(spring security)。我使用了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。从那里形成我的移动页面上的所有链接都指向其他移动页面。

相关问题