如何使用拦截器在不使用grails 3的情况下不使用spring security的情况下创建登录功能

时间:2019-05-03 10:36:08

标签: grails grails3

系统有两个grails应用程序:

  1. 使用springsecurity的私有后台,以及一个持有操作员用户名,密码,登录失败次数等的操作员域对象。
  2. 用户登录,登录和使用系统的公共Web前端。 User域对象包含用户的用户名,密码等。

因为我们在后台使用springsecuirty,所​​以我假设我们不能在Web上再次使用它(配置和数据库将发生冲突)。另外,网络只需要一个非常基本的身份验证(除注册和登录表单本身之外,所有页面都需要有效的会话)。

设置登录表单和拦截器很容易。

问题是,登录表单应在控制器中实际做什么?我可以检查用户名和密码是否与数据库中的内容匹配,然后大概需要创建一个会话,包括会话超时等。在哪里可以找到有关此操作的文档? http://docs.grails.org/3.1.1/ref/Servlet%20API/session.html告诉您如何注销,但不注销。我大概需要将会话存储在数据库中(以便用户可以访问任何服务器)等。

1 个答案:

答案 0 :(得分:0)

通过查看一些旧的Java代码,我了解了一些方法。

拦截器如下:

class AuthInterceptor {
    public AuthInterceptor() {
        // allow the login form and register form to work.
        matchAll().excludes(controller: 'auth')
    }
    boolean before() {
        if(session.getAttribute("user")== null ) {
            // jump to the login form if there is no user attribute.
            redirect controller: 'auth', action: 'login'
            return false
        }
        true
    }
    boolean after() { true }
    void afterView() {
        // no-op
    }

控制器看起来像这样:

class AuthController {
    def index() { }

    def login() {
        def email = params.email
        def password = params.password

        if (email != null) {
            // It would be better to invalidate the old session
            // but if we do, we cant get a new one...
            // session.invalidate()

            User user = User.findByEmail(email);
            if (user != null) {
                log.error("user.pass:" +  user.password +  " pass:" + password)
                // @TODO handle password encryption
                if (user.password == password) {
                    session.setAttribute("user", user)
                    redirect(controller:"dashboard")
                }
            }
            flash.message = "email or password incorrect"
        }
        render (view:"login")
    } // login()

但是,我还没有找到可以设置会话超时的地方。