spring-security中自定义处理器的使用
故障代码:
//自定义处理器,继承SimpleUrlAuthenticationSuccessHandler重写onAuthenticationSuccess
@Component
public class AuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
@Autowired
private UserDao userDao;
/**
* 登录成功执行的方法
*
* @param request
* @param response
* @param authentication
* @throws IOException
* @throws ServletException
*/
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//通过authentication获取当前登录用户名,**故障点**
//User user = (User) authentication.getPrincipal();
//String username = user.getUsername();
//通过request获取当前登录用户名
String username = request.getParameter("username");
System.out.println(username);
//根据用户名查询上次一登录信息
UserLoginLog lastLoginLog = userDao.findLoginLogByUsername(username);
System.out.println(lastLoginLog);
//将用户的上次登录信息放入session中供前台获取
request.getSession().setAttribute("lastLogin", lastLoginLog);
//将本地登录的用户名,登录ip,登录时间封装,交给dao写入数据库
String dateToStr = DateUtil.formatDateToStr(new Date());
UserLoginLog userLoginLog = new UserLoginLog(username, request.getRemoteAddr(), dateToStr);
userDao.addUserLoginLog(userLoginLog);
super.onAuthenticationSuccess(request, response, authentication);
}
}
这是一个用户登陆成功的自定义处理器,业务逻辑为登录校验成功后,查询该用户的用户名、上次登录的时间、上次登录ip,然后将查询结果封装返回前台。 最后把本次登录信息写入数据库。
问题:使用