我有一个登录 HTML 页面,它访问 Spring MVC 控制器页面上的 REST 功能,登录,以验证密码请求。控制器将根据数据库检查密码并返回用户名并重定向到默认的起始 HTML 页面。我正在使用 ModelAndView 类并重定向 flash 属性。我使用 flash 属性是因为我不希望用户名在浏览器的 URL 地址栏中可见。随后,此用户名将可在应用程序内的所有 HTML 页面中全局访问。
这是我的 login() 函数:
@Controller
public class UserController {
@ResponseBody
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView login(HttpServletRequest request, @RequestParam("txtUserName") String usrName, @RequestParam("txtPassword") String password, RedirectAttributes redirectAttrs) {
List<User> currentUsers = service.getUsersByUserName(usrName);
String strCurrentUserRole = currentUsers.get(0).getUserRole();
String strCurrentUserPassword = currentUsers.get(0).getUserPassword();
//get Base64 decoder
Base64.Decoder decoder = Base64.getDecoder();
String decodedPassword = new String(decoder.decode(strCurrentUserPassword));
if(decodedPassword.equals(password))
{
if(strCurrentUserRole.equals("admin"))
{
//redirect to the default starting page
ModelAndView modelAndView = new ModelAndView("redirect:/CSVFileList.html");
redirectAttrs.addFlashAttribute("r", "a");
redirectAttrs.addFlashAttribute("name", usrName);
return modelAndView;
}
}
}
地址栏中的结果:
https://10.164.60.168:8080/kilosteprest/CSVFileList.html
我的两个问题是(1)如何从我的接收页面 CSVFileList.html 访问两个 flash 属性 r 和 name -(注意:这是一个 HTML 页面而不是 JSP 页面)?我的第二个问题是(2)如何使检索到的用户名(名称 flash 属性)成为一个全局变量,我可以从我的所有 HTML 页面访问它(不使用隐藏文本字段)?先感谢您。