Search in sources :

Example 1 with Admin

use of com.tony.billing.entity.Admin in project BillingDubbo by TonyJiangWJ.

the class AdminServiceImpl method preResetPwd.

@Override
public Admin preResetPwd(String userName) {
    Admin user = mapper.queryByUserName(userName);
    if (user != null) {
        String email = user.getEmail();
        if (StringUtils.isNotEmpty(email)) {
            String token = sha256(UUID.randomUUID().toString());
            user = deleteSecret(user);
            redisUtils.set(token, deleteSecret(user), 3600);
            user.setTokenId(token);
            // TODO send reset email
            Map<String, Object> contents = new HashMap<>();
            contents.put("title", "重置密码");
            contents.put("typeDesc", "重置密码");
            contents.put("resetLink", resetPwdUrl + "?token=" + token);
            try {
                emailService.sendThymeleafMail(email, "用户重置密码", contents, EnumMailTemplateName.RESET_PWD_MAIL.getTemplateName());
            } catch (MessagingException e) {
                throw new BaseBusinessException("发送重置邮件失败");
            }
            return user;
        }
    }
    throw new BaseBusinessException("用户名不存在, 或者未绑定邮箱");
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) BaseBusinessException(com.tony.billing.exceptions.BaseBusinessException) Admin(com.tony.billing.entity.Admin) ModifyAdmin(com.tony.billing.entity.ModifyAdmin)

Example 2 with Admin

use of com.tony.billing.entity.Admin in project BillingDubbo by TonyJiangWJ.

the class AdminServiceImpl method modifyPwd.

@Override
public boolean modifyPwd(ModifyAdmin admin) {
    Preconditions.checkNotNull(admin.getId(), "用户id不能为空");
    Admin dbAdmin = mapper.getAdminById(admin.getId());
    if (dbAdmin == null) {
        return false;
    }
    boolean isOldPassword = dbAdmin.getPasswordVersion() == null || dbAdmin.getPasswordVersion().equals(0);
    // 现将密码进行加解密处理
    admin.setNewPassword(sha256(rsaUtil.decrypt(admin.getNewPassword()), dbAdmin.getUserName()));
    if (isOldPassword) {
        admin.setPassword(sha256(rsaUtil.decrypt(admin.getPassword())));
    } else {
        admin.setPassword(sha256(rsaUtil.decrypt(admin.getPassword()), dbAdmin.getUserName()));
    }
    if (admin.getNewPassword() == null) {
        return false;
    }
    Admin stored = mapper.getAdminById(admin.getId());
    if (stored != null && StringUtils.equals(stored.getPassword(), admin.getPassword())) {
        stored.setPassword(admin.getNewPassword());
        return mapper.modifyPwd(stored) > 0;
    }
    logger.error("用户:{} 修改密码,旧密码不正确", admin.getId());
    return false;
}
Also used : Admin(com.tony.billing.entity.Admin) ModifyAdmin(com.tony.billing.entity.ModifyAdmin)

Example 3 with Admin

use of com.tony.billing.entity.Admin in project BillingDubbo by TonyJiangWJ.

the class AdminServiceImpl method login.

@Override
public Admin login(Admin admin) {
    Admin dbAdmin = mapper.queryByUserName(admin.getUserName());
    boolean isOldPassword = dbAdmin.getPasswordVersion() == null || dbAdmin.getPasswordVersion().equals(0);
    String password = rsaUtil.decrypt(admin.getPassword());
    if (isOldPassword) {
        admin.setPassword(sha256(password));
    } else {
        admin.setPassword(sha256(password, admin.getUserName()));
    }
    if (admin.getPassword() == null) {
        logger.error("password error");
        return null;
    }
    Admin checkUser = mapper.preLogin(admin);
    if (checkUser != null) {
        redisUtils.del(checkUser.getTokenId());
        checkUser.setTokenId(TokenUtil.getToken(checkUser.getCode(), checkUser.getUserName(), checkUser.getPassword()));
        checkUser.setTokenVerify(VERIFY_TIME);
        checkUser.setLastLogin(new Date());
        if (mapper.doLogin(checkUser) > 0) {
            redisUtils.set(checkUser.getTokenId(), deleteSecret(checkUser), VERIFY_TIME / 1000);
            if (isOldPassword) {
                // 更新密码
                checkUser.setPasswordVersion(1);
                checkUser.setPassword(sha256(password, checkUser.getUserName()));
                mapper.update(checkUser);
            }
            return checkUser;
        }
    }
    return null;
}
Also used : Admin(com.tony.billing.entity.Admin) ModifyAdmin(com.tony.billing.entity.ModifyAdmin) Date(java.util.Date)

Example 4 with Admin

use of com.tony.billing.entity.Admin in project BillingDubbo by TonyJiangWJ.

the class AdminController method login.

@RequestMapping(value = "/user/login", method = RequestMethod.POST)
public BaseResponse login(@ModelAttribute("request") @Validated AdminLoginRequest request, // 用于AOP获取IP地址等信息
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
    BaseResponse response = new BaseResponse();
    try {
        Admin loginAdmin = new Admin();
        loginAdmin.setUserName(request.getUserName());
        loginAdmin.setPassword(request.getPassword());
        Admin admin = adminService.login(loginAdmin);
        if (admin != null) {
            authUtil.setCookieToken(admin.getTokenId(), httpServletResponse);
            ResponseUtil.success(response);
        } else {
            ResponseUtil.error(response);
        }
    } catch (Exception e) {
        logger.error("/user/login error", e);
        ResponseUtil.sysError(response);
    }
    return response;
}
Also used : BaseResponse(com.tony.billing.response.BaseResponse) ModifyAdmin(com.tony.billing.entity.ModifyAdmin) Admin(com.tony.billing.entity.Admin) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with Admin

use of com.tony.billing.entity.Admin in project BillingDubbo by TonyJiangWJ.

the class AdminController method register.

@RequestMapping(value = "/user/register/put", method = RequestMethod.POST)
public BaseResponse register(@ModelAttribute("request") @Validated AdminRegisterRequest registerRequest) {
    BaseResponse response = new BaseResponse();
    try {
        Admin admin = new Admin();
        admin.setUserName(registerRequest.getUserName());
        admin.setPassword(registerRequest.getPassword());
        Long flag = 0L;
        if ((flag = adminService.register(admin)) > 0) {
            ResponseUtil.success(response);
        } else {
            ResponseUtil.error(response);
            if (flag.equals(-2L)) {
                response.setMsg("账号已存在");
            }
        }
    } catch (Exception e) {
        logger.error("/user/register/put error", e);
        ResponseUtil.sysError(response);
    }
    return response;
}
Also used : BaseResponse(com.tony.billing.response.BaseResponse) ModifyAdmin(com.tony.billing.entity.ModifyAdmin) Admin(com.tony.billing.entity.Admin) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Admin (com.tony.billing.entity.Admin)8 ModifyAdmin (com.tony.billing.entity.ModifyAdmin)7 BaseBusinessException (com.tony.billing.exceptions.BaseBusinessException)2 BaseResponse (com.tony.billing.response.BaseResponse)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 TokenServletRequestWrapper (com.tony.billing.filters.wapper.TokenServletRequestWrapper)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 MessagingException (javax.mail.MessagingException)1 Cookie (javax.servlet.http.Cookie)1 StandardMultipartHttpServletRequest (org.springframework.web.multipart.support.StandardMultipartHttpServletRequest)1