private ShopMember getMemberFromCookie( HttpServletRequest request, HttpServletResponse response, Website web) { Member coreMember; try { coreMember = (Member) rememberMeService.autoLogin(request, response); if (coreMember == null) { return null; } } catch (CookieTheftException e) { log.warn("remember me cookie theft: {}", e.getMessage()); return null; } // 无法自动登录 if (coreMember == null) { return null; } Long webId = web.getId(); Long userId = coreMember.getUser().getId(); ShopMember member = null; // 改变站点 boolean change = false; // 其他站点会员 if (!coreMember.getWebsite().getId().equals(webId)) { coreMember = memberMng.getByUserId(webId, userId); change = true; } if (coreMember == null) { // 考虑自动注册 ShopConfig config = shopConfigMng.findById(webId); if (config.getRegisterAuto()) { member = shopMemberMng.join(userId, webId, config.getRegisterGroup()); log.debug("shop member auto login. username= {}", member.getUsername()); } else { log.debug("shop member not allow auto login."); } } else { member = shopMemberMng.findById(coreMember.getId()); // 不应该为null,可能在某些地方有错误。 if (member == null) { throw new IllegalStateException( "This is JspGou's BUG, ShopMember here should not be null."); } } if (member != null) { userMng.updateLoginInfo(userId, request.getRemoteAddr()); session.setAttribute( request, response, SESSION_USER_ID_KEY, member.getMember().getUser().getId()); session.setAttribute(request, response, SESSION_MEMBER_ID_KEY, member.getMember().getId()); // addUsernameCookie(member.getUsername(), member.getFirstname(), // member.getLastname(), request, response); addUsernameCookie(member.getUsername(), null, null, request, response); if (change) { // 不用实现的这么复杂 // rememberMeService.loginChange(request, response, member // .getMember()); } } return member; }
public ShopMember memberLogin( HttpServletRequest request, HttpServletResponse response, String username) throws UsernameNotFoundException, BadCredentialsException, UserNotInWebsiteException, UserNotAcitveException { // Long webId = web.getId(); // 先退出登录 logout(request, response); User user = userMng.getByUsername(username); ShopMember member = shopMemberMng.getByUsername(username); if (member == null) { throw new UserNotInWebsiteException("user '" + user.getUsername()); } else { if (!member.getMember().getActive()) { throw new UserNotAcitveException("user '" + user.getUsername()); } } userMng.updateLoginInfo(user.getId(), request.getRemoteAddr()); rememberMeService.loginSuccess(request, response, member.getMember()); session.setAttribute(request, response, SESSION_USER_ID_KEY, user.getId()); session.setAttribute(request, response, SESSION_MEMBER_ID_KEY, member.getId()); addUsernameCookie(member.getUsername(), null, null, request, response); shoppingSvc.addCookie(member, request, response); return member; }
public ShopMember memberLogin( HttpServletRequest request, HttpServletResponse response, Website web, String username, String password) throws UsernameNotFoundException, BadCredentialsException, UserNotInWebsiteException, UserNotAcitveException { Long webId = web.getId(); // 先退出登录 logout(request, response); User user = login(username, password); ShopMember member = shopMemberMng.getByUserId(webId, user.getId()); if (member == null) { ShopConfig config = shopConfigMng.findById(webId); if (config.getRegisterAuto()) { // 自动注册 member = shopMemberMng.join(user, webId, config.getRegisterGroup()); } else { throw new UserNotInWebsiteException( "user '" + user.getUsername() + "' not in Website '" + webId + "'"); } } else { if (!member.getMember().getActive()) { throw new UserNotAcitveException( "user '" + user.getUsername() + "' not Active '" + webId + "'"); } } userMng.updateLoginInfo(user.getId(), request.getRemoteAddr()); rememberMeService.loginSuccess(request, response, member.getMember()); session.setAttribute(request, response, SESSION_USER_ID_KEY, user.getId()); session.setAttribute(request, response, SESSION_MEMBER_ID_KEY, member.getId()); addUsernameCookie(member.getUsername(), null, null, request, response); shoppingSvc.addCookie(member, request, response); return member; }