Grails - 在不更改URL的情况下检索注册页面或主页

时间:2015-02-22 23:35:44

标签: grails spring-security

我正在使用带有Spring Security的Grails,并希望当用户输入我的站点根URL时,它将收到一个不同的页面取决于他是否登录。 1.登录 - 主页包含不同的数据 2.未登录 - 注册页面

我不想使用Spring UI插件,我不想使用重定向,因为我希望URL保留在根URL,最好的方法是什么?

1 个答案:

答案 0 :(得分:1)

映射根" /"您的应用程序对您定义的Controller(在UrlMappings.groovy中)。在该Controller的索引方法中,检查用户是否已登录(通过SpringSecurity&#API;)。如果用户已登录,则渲染已登录的视图,否则渲染未登录的视图。这样,URL保持不变。

以下是插件documentation的摘录:

class SomeController {
    def springSecurityService

    def index() {
        if (springSecurityService.isLoggedIn() && SpringSecurityUtils.ifAnyGranted("USER_ROLE")) { // Example here checks if we have the "USER_ROLE"
            render view: "A"
        }
        else {
            render view: "B"
        }
    }
}

小心匿名角色。它将成功传递文档中指定的 isLoggedIn

相关问题