Reputation: 1601
I'm using play framework 2.8.x
and I trying to save data into the session and in the next request read this data but my data not saved in the session object. My controller class looks like the following:
public class UserController extends Controller {
public Result verifyToken(Http.Request request) {
final Http.Session session = request.session();
session.adding("key1", "my_value");
return ok(userInfo).withCookies(tokenCookie).withSession(session);
}
}
then I refactoring my code like the following:
public class UserController extends Controller {
public Result verifyToken(Http.Request request) {
return ok(userInfo).addingToSession(request, "sessionData", "test");
}
}
but I have the same result. Not working!
and then I trying to read my data from the session in the Filter
. My filter looks like the following:
public class PageAccessFilter extends Filter {
@Inject
public PageAccessFilter(Materializer mat) {
super(mat);
}
@Override
public CompletionStage<Result> apply(Function<Http.RequestHeader, CompletionStage<Result>> next, Http.RequestHeader rh) {
final Http.Session userSession = rh.session();
final String userToken = userSession.get("key1").orElse(null); // in this value was null.
return next(rh);
}
}
How can I save and read data from the session? What I'm doing wrong?
Upvotes: 0
Views: 461
Reputation: 6499
Use addingToSession
on the ok() response.
public class UserController extends Controller {
public Result verifyToken(Http.Request request) {
return ok(userInfo).addingToSession(request, "key1", "my_value");
}
}
Upvotes: 1