之前用户注册后是先跳到登录页面:
- @RequestMapping(method = RequestMethod.POST)
- public String register(@Valid User user, RedirectAttributes redirectAttributes) {
- accountService.registerUser(user);
- redirectAttributes.addFlashAttribute("username",
- user.getLoginName());
- return "redirect:/login";
- }
现在要求用户注册成功后自动登录,改写如下:
- @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.
问题补充:问题解决了,改写成token.setPassword(user.getPlainPassword().toCharArray()); 就可以了。因为我的user的plainPassword是明文,user的password是hash。