根据 Struts 中的用户设置 cookie 2
Set cookies depending on user in Struts 2
我想根据登录的用户设置 cookie。所以当用户 x 登录时,他可以看到他输入的名字,如果用户 y 登录,他会看到他之前输入的名字.我该怎么做?因为现在最新登录的用户就是cookies的值。
我这样验证我的用户:
public class LoginService {
public boolean verifyLogin(User user){
if(user.getUserId().equals("userId") && user.getPassword().equals("password")){
return true;
}
if(user.getUserId().equals("random") && user.getPassword().equals("a")){
return true;
}
return false;
}
}
这就是我在 LoginAction
中设置 cookie 的方式:
public Set<Cookie> getCookies(){
Set<Cookie> cookies = new HashSet<>();
Cookie userId = new Cookie("userId", user.getUserId() );
userId.setMaxAge(60*60*24*365); // Make the cookie last a year!
userId.setPath("/"); //Make it at root.
cookies.add(userId);}
这就是我在 FormAction
中设置 cookie 的方式:
public Set<Cookie> getCookies(){
Set<Cookie> cookies = new HashSet<>();
Cookie name = new Cookie("name", userInfo.getName() );
name.setMaxAge(60*60*24*365); // Make the cookie last a year!
name.setPath("/"); //Make it at root.
cookies.add(name); }
您应该为已经通过验证的用户保存 cookie。如果首先验证用户,则将其保存到会话中。然后使用会话用户提供cookie。像这样,
if (verifyLogin(User user))
session.put("user", user);
要保存 cookie,首先从会话中获取用户
public Set<Cookie> getCookies(){
Set<Cookie> cookies = new HashSet<>();
User user = (User) session.get("user");
if (user != null){
Cookie userId = new Cookie("userId", user.getUserId() );
userId.setMaxAge(60*60*24*365); // Make the cookie last a year!
userId.setPath("/"); //Make it at root.
cookies.add(userId);
}
return cookies;
}
要访问会话地图,您可以使用 SessionAware
界面。另一种方法是从操作上下文中获取会话。
请注意,cookie 由浏览器共享。如果您同时使用相同的浏览器以不同的用户身份进入,它将使用相同的 cookie,除非您提供每个会话 cookie 的实现。
我想根据登录的用户设置 cookie。所以当用户 x 登录时,他可以看到他输入的名字,如果用户 y 登录,他会看到他之前输入的名字.我该怎么做?因为现在最新登录的用户就是cookies的值。
我这样验证我的用户:
public class LoginService {
public boolean verifyLogin(User user){
if(user.getUserId().equals("userId") && user.getPassword().equals("password")){
return true;
}
if(user.getUserId().equals("random") && user.getPassword().equals("a")){
return true;
}
return false;
}
}
这就是我在 LoginAction
中设置 cookie 的方式:
public Set<Cookie> getCookies(){
Set<Cookie> cookies = new HashSet<>();
Cookie userId = new Cookie("userId", user.getUserId() );
userId.setMaxAge(60*60*24*365); // Make the cookie last a year!
userId.setPath("/"); //Make it at root.
cookies.add(userId);}
这就是我在 FormAction
中设置 cookie 的方式:
public Set<Cookie> getCookies(){
Set<Cookie> cookies = new HashSet<>();
Cookie name = new Cookie("name", userInfo.getName() );
name.setMaxAge(60*60*24*365); // Make the cookie last a year!
name.setPath("/"); //Make it at root.
cookies.add(name); }
您应该为已经通过验证的用户保存 cookie。如果首先验证用户,则将其保存到会话中。然后使用会话用户提供cookie。像这样,
if (verifyLogin(User user))
session.put("user", user);
要保存 cookie,首先从会话中获取用户
public Set<Cookie> getCookies(){
Set<Cookie> cookies = new HashSet<>();
User user = (User) session.get("user");
if (user != null){
Cookie userId = new Cookie("userId", user.getUserId() );
userId.setMaxAge(60*60*24*365); // Make the cookie last a year!
userId.setPath("/"); //Make it at root.
cookies.add(userId);
}
return cookies;
}
要访问会话地图,您可以使用 SessionAware
界面。另一种方法是从操作上下文中获取会话。
请注意,cookie 由浏览器共享。如果您同时使用相同的浏览器以不同的用户身份进入,它将使用相同的 cookie,除非您提供每个会话 cookie 的实现。