系统有两个grails应用程序:
因为我们在后台使用springsecuirty,所以我假设我们不能在Web上再次使用它(配置和数据库将发生冲突)。另外,网络只需要一个非常基本的身份验证(除注册和登录表单本身之外,所有页面都需要有效的会话)。
设置登录表单和拦截器很容易。
问题是,登录表单应在控制器中实际做什么?我可以检查用户名和密码是否与数据库中的内容匹配,然后大概需要创建一个会话,包括会话超时等。在哪里可以找到有关此操作的文档? http://docs.grails.org/3.1.1/ref/Servlet%20API/session.html告诉您如何注销,但不注销。我大概需要将会话存储在数据库中(以便用户可以访问任何服务器)等。
答案 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()
但是,我还没有找到可以设置会话超时的地方。