Search in sources :

Example 1 with BusinessException

use of com.tansci.common.exception.BusinessException in project tansci by typ1805.

the class MailServiceImpl method sendSimple.

/**
 * @MonthName: sendSimple
 * @Description: 普通邮件发送
 * @Author: tanyp
 * @Date: 2021/6/7 9:30
 * @Param: [dto]
 * @return: void
 */
@Override
public MessageVo sendSimple(MessageDto dto) {
    try {
        log.info("=======普通邮件发送开始,请求参数:{}", JSON.toJSON(dto));
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject(dto.getSubject());
        // 设置邮件发送者,这个跟application.yml中设置的要一致
        message.setFrom(sender);
        // 设置邮件接收者,可以有多个接收者,中间用逗号隔开,以下类似
        // message.setTo("10*****16@qq.com","12****32*qq.com");
        message.setTo(dto.getRecipient());
        // 设置邮件抄送人,可以有多个抄送人
        if (Objects.nonNull(dto.getCc())) {
            message.setCc(dto.getCc());
        }
        // 设置隐秘抄送人,可以有多个
        if (Objects.nonNull(dto.getBcc())) {
            message.setBcc(dto.getBcc());
        }
        // 设置邮件发送日期
        message.setSentDate(new Date());
        // 设置邮件的正文
        Template template = templateService.getOne(Wrappers.<Template>lambdaQuery().eq(Template::getCode, dto.getCode()));
        if (Objects.isNull(template)) {
            throw new BusinessException("邮件模板编码不存在,请核实!");
        }
        dto.setText(template.getCode());
        String text = MessageUtils.assembly(dto.getText(), dto.getParam());
        if (Objects.isNull(text)) {
            throw new BusinessException("邮件模板参数有误,请核查!");
        }
        message.setText(text);
        // 发送邮件
        javaMailSender.send(message);
        log.info("=======普通邮件发送结束");
        TemplateDetails details = new TemplateDetails();
        details.setCode(dto.getCode());
        details.setState(0);
        details.setContent(JSON.toJSONString(message));
        details.setSendTime(LocalDateTime.now());
        templateDetailsService.save(details);
        return MessageVo.builder().code(Constants.NEWS_SUCCESS_CODE).message(Constants.NEWS_SUCCESS_MESSAGE).build();
    } catch (MailException e) {
        log.error("====邮件====sendSimple=====异常:{}", e);
        return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
    }
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) TemplateDetails(com.tansci.domain.message.TemplateDetails) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailException(org.springframework.mail.MailException) Date(java.util.Date) Template(com.tansci.domain.message.Template)

Example 2 with BusinessException

use of com.tansci.common.exception.BusinessException in project tansci by typ1805.

the class MailServiceImpl method sendImgRes.

/**
 * @MonthName: sendImgRes
 * @Description: 带图片资源的邮件
 * @Author: tanyp
 * @Date: 2021/6/7 9:30
 * @Param: [dto]
 * @return: void
 */
@Override
public MessageVo sendImgRes(MessageDto dto) {
    try {
        log.info("=======带图片资源的邮件开始,请求参数:{}", JSON.toJSON(dto));
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
        message.setSubject(dto.getSubject());
        message.setFrom(sender);
        message.setTo(dto.getRecipient());
        if (Objects.nonNull(dto.getCc())) {
            message.setCc(dto.getCc());
        }
        if (Objects.nonNull(dto.getBcc())) {
            message.setBcc(dto.getBcc());
        }
        message.setSentDate(new Date());
        Template template = templateService.getOne(Wrappers.<Template>lambdaQuery().eq(Template::getCode, dto.getCode()));
        if (Objects.isNull(template)) {
            throw new BusinessException("邮件模板编码不存在,请核实!");
        }
        dto.setText(template.getContent());
        String text = MessageUtils.assembly(dto.getText(), dto.getParam());
        if (Objects.isNull(text)) {
            throw new BusinessException("邮件模板参数有误,请核查!");
        }
        message.setText(text);
        // 第一个参数指的是html中占位符的名字,第二个参数就是文件的位置
        dto.getAttachments().forEach(file -> {
            try {
                message.addInline(file.getName(), new FileSystemResource(file));
            } catch (MessagingException e) {
                log.error("=========邮件图片解析异常:{}", e);
            }
        });
        javaMailSender.send(mimeMessage);
        log.info("=======带图片资源的邮件结束");
        TemplateDetails details = new TemplateDetails();
        details.setCode(dto.getCode());
        details.setState(0);
        details.setContent(JSON.toJSONString(message));
        details.setSendTime(LocalDateTime.now());
        templateDetailsService.save(details);
        return MessageVo.builder().code(Constants.NEWS_SUCCESS_CODE).message(Constants.NEWS_SUCCESS_MESSAGE).build();
    } catch (MessagingException e) {
        log.error("====邮件====sendImgRes=====异常:{}", e);
        return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
    }
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) TemplateDetails(com.tansci.domain.message.TemplateDetails) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) FileSystemResource(org.springframework.core.io.FileSystemResource) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date) Template(com.tansci.domain.message.Template)

Example 3 with BusinessException

use of com.tansci.common.exception.BusinessException in project tansci by typ1805.

the class JWTAuthorizationFilter method getAuthentication.

/**
 * @MonthName: getAuthentication
 * @Description: 从token中获取用户信息并新建一个token
 * @Author: tanyp
 * @Date: 2021/10/22 17:55
 * @Param: [tokenHeader]
 * @return: org.springframework.security.authentication.UsernamePasswordAuthenticationToken
 */
private UsernamePasswordAuthenticationToken getAuthentication(String tokenHeader) {
    String token = tokenHeader.replace(JwtTokenUtils.TOKEN_PREFIX, "");
    boolean expiration = JwtTokenUtils.isExpiration(token);
    if (expiration) {
        throw new BusinessException(Enums.AUTH_NO_TOKEN.getValue());
    } else {
        String username = JwtTokenUtils.getUsername(token);
        String role = JwtTokenUtils.getUserRole(token);
        SysUser user = JwtTokenUtils.getUser(token);
        if (username != null) {
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, null, Collections.singleton(new SimpleGrantedAuthority(role)));
            authenticationToken.setDetails(user);
            return authenticationToken;
        }
    }
    return null;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) BusinessException(com.tansci.common.exception.BusinessException) SysUser(com.tansci.domain.system.SysUser) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 4 with BusinessException

use of com.tansci.common.exception.BusinessException in project tansci by typ1805.

the class SysUserServiceImpl method save.

@Transactional
@Override
public boolean save(SysUser user) {
    Integer count = this.baseMapper.selectCount(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, user.getUsername()));
    if (Objects.nonNull(count) && count > 0) {
        throw new BusinessException("用户名称已存在!");
    }
    user.setDelFlag(Constants.NOT_DEL_FALG);
    user.setCreateTime(LocalDateTime.now());
    // 密码加密
    PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    user.setPassword(passwordEncoder.encode(user.getPassword()));
    int row = this.baseMapper.insert(user);
    if (row > 0) {
        if (Objects.isNull(user.getOrgId())) {
            user.setOrgId(1);
        }
        return sysUserOrgService.save(SysUserOrg.builder().userId(user.getId()).orgId(user.getOrgId()).build());
    }
    return false;
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with BusinessException

use of com.tansci.common.exception.BusinessException in project tansci by typ1805.

the class SysUserServiceImpl method modifyPass.

/**
 * @MonthName: modifyPass
 * @Description: 修改密码
 * @Author: tanyp
 * @Date: 2021/7/8 16:37
 * @Param: [dto]
 * @return: java.lang.Integer
 */
@Override
public Integer modifyPass(SysUserDto dto) {
    SysUser user = this.baseMapper.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, dto.getUsername()));
    // 验证原始密码
    PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    Boolean flag = passwordEncoder.matches(dto.getOldPassword(), user.getPassword());
    if (Objects.isNull(user) || !flag) {
        throw new BusinessException("原始密码错误,请重新输入!");
    }
    user.setPassword(passwordEncoder.encode(dto.getPassword()));
    user.setUpdateTime(LocalDateTime.now());
    return this.baseMapper.updateById(user);
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)

Aggregations

BusinessException (com.tansci.common.exception.BusinessException)10 Template (com.tansci.domain.message.Template)5 TemplateDetails (com.tansci.domain.message.TemplateDetails)3 MessageVo (com.tansci.domain.message.vo.MessageVo)3 Date (java.util.Date)3 Transient (java.beans.Transient)2 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)2 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)2 PasswordEncoder (org.springframework.security.crypto.password.PasswordEncoder)2 Transactional (org.springframework.transaction.annotation.Transactional)2 SysUser (com.tansci.domain.system.SysUser)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1 MailException (org.springframework.mail.MailException)1 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1