服务器token
// User.java
public class User{
private String email;
private String userName;
private Integer userId;
private String mobile;
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
= email;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
}
// Constant.java
public final class Constant {
/**
* 用户SESSION标识
*/
public static final String USER_SESSION_NAME = "user";
}
// UserInfoController.java
@Controller
@SessionAttributes(Constant.USER_SESSION_NAME)
public class UserInfoController{
@RequestMapping(value = Path.USER_LOGIN, method = RequestMethod.POST)
public String login(final Model model){
User user = new User();
...
model.addAttribute(Constant.USER_SESSION_NAME, user);
// 设置token
}
@RequestMapping(value = Path.USER_RECOMMENDATION_DATA, method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> recommendationNeedsDatas(@ModelAttribute(Constant.USER_SESSION_NAME)final User user){
...
}
// 使用token
}
核心:
1、@SessionAttributes(Constant.USER_SESSION_NAME) 标记表示支持token
2、设置token
model.addAttribute(Constant.USER_SESSION_NAME, user);
3、使用token
// 在请求参数中传入
@ModelAttribute(Constant.USER_SESSION_NAME)