Grails控制器的beforeInterceptor

时间:2014-09-17 19:04:01

标签: grails

我正在尝试这样做,以便无论我的控制器的哪个功能被访问,如果会话已被清空,它会在弹出一个弹出说有会话的模态上的单击确定后返回到登录页面已过期但我无法完成它的工作。以下是该控制器的前几种方法:

def beforeInterceptor = {
        [action:this.&checkSession()]
    }

    def checkSession() {
        if (!session.user) {
            render text: """<script type="text/javascript"> alert('Session expired. Please log in again.'); window.location.href = data.url;</script>""",
                    contentType: 'js'

        }
    }

    def index() {
        redirect (action: customerLogin)
    }

    def customerLogin = {
        selectedBatch = null
    }

    def authenticate = {

        def user = null

        def possibleUsersList = User.findAllWhere(user_name: params.username)

        possibleUsersList.each { 
            if (bcryptService.checkPassword(params.password, it.user_password))
                user = it
        }

        if (user) {
                session.user = user
                greetingName = user.user_name
                render(contentType: 'text/json') {
                        [success: true, url: createLink(controller: 'customer', action: 'custMainPage')]
                }
        }
        else {
            //def messages = ResourceBundle.getBundle('messages')
            //def errorstring = bundle.getString("fatcaone.login.error")
            //println "Login error: " + ${errorstring} 
            render (contentType: 'text/json') {
                //["message": '<p>code=fatcaone.login.error</p>']
                ["message": '<p>Login or Password incorrect.</p>']
            }
        }
    }

    def logout = {
        if (session.user != null) {
            flash.message = "Goodbye ${session.user.fullName}"
            session.user = null

        }
        redirect(action: customerLogin)
    }

3 个答案:

答案 0 :(得分:0)

鉴于您希望保持代码原样:

def beforeInterceptor = {
    [action:this.&checkSession()]
}

private def checkSession() {
    if (!session.user) {
        flash.error = 'Session expired. Please log in again.'

        redirect(action: customerLogin)
        return false

    }
}

def index() {
    redirect (action: customerLogin)
}

def customerLogin = {
    selectedBatch = null
}

def authenticate = {

    def user = null

    def possibleUsersList = User.findAllWhere(user_name: params.username)

    possibleUsersList.each { 
        if (bcryptService.checkPassword(params.password, it.user_password))
            user = it
    }

    if (user) {
            session.user = user
            greetingName = user.user_name
            render(contentType: 'text/json') {
                    [success: true, url: createLink(controller: 'customer', action: 'custMainPage')]
            }
    }
    else {
        //def messages = ResourceBundle.getBundle('messages')
        //def errorstring = bundle.getString("fatcaone.login.error")
        //println "Login error: " + ${errorstring} 
        render (contentType: 'text/json') {
            //["message": '<p>code=fatcaone.login.error</p>']
            ["message": '<p>Login or Password incorrect.</p>']
        }
    }
}

def logout = {
    if (session.user != null) {
        flash.message = "Goodbye ${session.user.fullName}"
        session.user = null

    }
    redirect(action: customerLogin)
}

在您的登录页面中添加:

<g:if test="${flash.error}"> 
    <script type="text/javascript"> 
        alert('${flash.error}');
    </script>
</g:if>

答案 1 :(得分:0)

从Grails 3.0中删除了beforeInterceptor。 有关此问题的讨论,请参阅https://github.com/grails/grails-core/issues/635

答案 2 :(得分:0)

检查

def checkSession() {
   if (session == null || session["loginId"] == null) {
        render text: """<script type="text/javascript"> alert('Session expired. Please log in again.'); window.location.href = "${createLink(controller: 'test',action: 'logout')}";</script>""",
                contentType: 'js';
        return false;
    } 
}