之前用戶(hù)注冊后是先跳到登錄頁(yè)面:
- @RequestMapping(method = RequestMethod.POST)
- public String register(@Valid User user, RedirectAttributes redirectAttributes) {
- accountService.registerUser(user);
- redirectAttributes.addFlashAttribute("username",
- user.getLoginName());
- return "redirect:/login";
- }
現在要求用戶(hù)注冊成功后自動(dòng)登錄,改寫(xiě)如下:
- @RequestMapping(method = RequestMethod.POST)
- public String register(@Valid User user, RedirectAttributes redirectAttributes) {
- accountService.registerUser(user);
- UsernamePasswordToken token = new UsernamePasswordToken();
- token.setUsername(user.getLoginName());
- token.setPassword(user.getPassword().toCharArray());
- SecurityUtils.getSubject().login(token);
- return "redirect:/";
- }
結果報錯:
- org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - user01, rememberMe=false] did not match the expected credentials.
問(wèn)題補充:問(wèn)題解決了,改寫(xiě)成token.setPassword(user.getPlainPassword().toCharArray()); 就可以了。因為我的user的plainPassword是明文,user的password是hash。