Dam*_*ien 9 java playframework
我正在使用play框架中的"Secure"模块,它的工作效果非常好.我可以通过在我的应用程序中发布/ login路由来登录用户.
然而,当我"创建/注册"的新用户,我想为用户用户创建后,将自动登录.这个,我似乎无法以编程方式工作,即不必发布表单登录.
我当前用户创建操作的代码如下:
public static void create(@Required @Email String email, @Required String password, @Required @Equals("password") String passwordConfirmation) {
User user = new User(email);
user.password = new Crypto().encryptAES(password);
user.save();
try {
Secure.authenticate(email, password, false); // <== this is the action in secure module mapped to /login
} catch(Throwable t) {
t.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
所以我手动调用Secure.authentication(username, password, remember)方法,但这只是将我重定向到登录页面而不是我的目标页面.
我有一种感觉,因为我直接调用Secure.authenticate而不是通过请求/响应,所以没有设置cookie或其他东西,但我只是不确定.
有任何想法吗?
一个想法可能是在会话中设置用户名(用户注册成功后)
session.put("username", username);
Run Code Online (Sandbox Code Playgroud)
这将确保Secure.connected()按预期返回.
如果你需要'记住我',
if(remember){
response.setCookie("rememberme", Crypto.sign(username) + "-" + username, "30d");
}
Run Code Online (Sandbox Code Playgroud)
实际上,这是Secure模块在成功验证后所执行的操作.检查安全模块