Search in sources :

Example 1 with CustomException

use of com.fanxb.bookmark.common.exception.CustomException in project bookmark by FleyX.

the class HashUtil method hash.

/**
 * Description: 根据type签名str
 *
 * @param str  str
 * @param type 签名类别
 * @return java.lang.String
 * @author fanxb
 * @date 2019/7/6 14:40
 */
public static String hash(String str, String type) {
    try {
        MessageDigest md = MessageDigest.getInstance(type);
        md.update(str.getBytes());
        return bytesToHexString(md.digest());
    } catch (Exception e) {
        throw new CustomException(e);
    }
}
Also used : CustomException(com.fanxb.bookmark.common.exception.CustomException) MessageDigest(java.security.MessageDigest) CustomException(com.fanxb.bookmark.common.exception.CustomException)

Example 2 with CustomException

use of com.fanxb.bookmark.common.exception.CustomException in project bookmark by FleyX.

the class BaseInfoServiceImpl method updateEmail.

@Override
@Transactional(rollbackFor = Exception.class)
public void updateEmail(EmailUpdateBody body) {
    int userId = UserContextHolder.get().getUserId();
    String oldPassword = userDao.selectByUserIdOrGithubId(userId, null).getPassword();
    if (!StrUtil.equals(oldPassword, HashUtil.getPassword(body.getOldPassword()))) {
        throw new CustomException("密码校验失败,无法更新email");
    }
    String secret = UUID.randomUUID().toString().replaceAll("-", "");
    String url = VERIFY_EMAIL.replaceAll("XXXX", CommonConstant.serviceAddress + VERIFY_EMAIL_PATH + secret);
    log.debug(url);
    MailInfo info = new MailInfo(body.getEmail(), "验证邮箱", url);
    MailUtil.sendMail(info, true);
    RedisUtil.set(RedisConstant.getUpdateEmailKey(secret), String.valueOf(userId), TimeUtil.DAY_MS);
    userDao.updateNewEmailByUserId(userId, body.getEmail());
}
Also used : CustomException(com.fanxb.bookmark.common.exception.CustomException) MailInfo(com.fanxb.bookmark.common.entity.MailInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with CustomException

use of com.fanxb.bookmark.common.exception.CustomException in project bookmark by FleyX.

the class EsUtil method init.

@PostConstruct
public void init() {
    if (!status) {
        return;
    }
    try {
        if (client != null) {
            client.close();
        }
        client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, scheme)));
        if (this.indexExist(EsConstant.BOOKMARK_INDEX)) {
            return;
        }
        CreateIndexRequest request = new CreateIndexRequest(EsConstant.BOOKMARK_INDEX);
        request.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2));
        request.mapping(EsConstant.CREATE_BOOKMARK_INDEX, XContentType.JSON);
        CreateIndexResponse res = client.indices().create(request, RequestOptions.DEFAULT);
        if (!res.isAcknowledged()) {
            throw new CustomException("初始化失败");
        }
    } catch (Exception e) {
        log.error("注意初始化es失败:{}:{}", host, port, e);
        System.exit(0);
    }
}
Also used : HttpHost(org.apache.http.HttpHost) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) CustomException(com.fanxb.bookmark.common.exception.CustomException) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.client.indices.CreateIndexResponse) CustomException(com.fanxb.bookmark.common.exception.CustomException) EsException(com.fanxb.bookmark.common.exception.EsException) PostConstruct(javax.annotation.PostConstruct)

Example 4 with CustomException

use of com.fanxb.bookmark.common.exception.CustomException in project bookmark by FleyX.

the class JwtUtil method decode.

/**
 * Description: 解密jwt
 *
 * @param token  token
 * @param secret secret
 * @return java.util.Map<java.lang.String, com.auth0.jwt.interfaces.Claim>
 * @author fanxb
 * @date 2019/3/4 18:14
 */
public static Map<String, Claim> decode(String token, String secret) {
    if (token == null || token.length() == 0) {
        throw new CustomException("token为空:" + token);
    }
    Algorithm algorithm = Algorithm.HMAC256(secret);
    JWTVerifier jwtVerifier = JWT.require(algorithm).build();
    DecodedJWT decodedJWT = jwtVerifier.verify(token);
    return decodedJWT.getClaims();
}
Also used : CustomException(com.fanxb.bookmark.common.exception.CustomException) Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 5 with CustomException

use of com.fanxb.bookmark.common.exception.CustomException in project bookmark by FleyX.

the class MailUtil method sendMail.

/**
 * 功能描述: 发送邮件
 *
 * @param info   邮件体
 * @param isHtml 是否为html邮件
 * @author fanxb
 * @date 2019/9/26 16:30
 */
public static void sendMail(MailInfo info, boolean isHtml) {
    try {
        MimeMessageHelper helper = new MimeMessageHelper(mailSender.createMimeMessage());
        helper.setFrom(from);
        helper.setTo(info.getReceiver());
        helper.setSubject(info.getSubject());
        helper.setText(info.getContent(), isHtml);
        mailSender.send(helper.getMimeMessage());
    } catch (Exception e) {
        throw new CustomException("发送邮件失败:" + e.getMessage(), e);
    }
}
Also used : CustomException(com.fanxb.bookmark.common.exception.CustomException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) CustomException(com.fanxb.bookmark.common.exception.CustomException)

Aggregations

CustomException (com.fanxb.bookmark.common.exception.CustomException)6 Transactional (org.springframework.transaction.annotation.Transactional)2 JSONObject (com.alibaba.fastjson.JSONObject)1 JWTVerifier (com.auth0.jwt.JWTVerifier)1 Algorithm (com.auth0.jwt.algorithms.Algorithm)1 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 MailInfo (com.fanxb.bookmark.common.entity.MailInfo)1 User (com.fanxb.bookmark.common.entity.po.User)1 EsException (com.fanxb.bookmark.common.exception.EsException)1 MessageDigest (java.security.MessageDigest)1 HashMap (java.util.HashMap)1 PostConstruct (javax.annotation.PostConstruct)1 HttpHost (org.apache.http.HttpHost)1 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)1 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)1 CreateIndexResponse (org.elasticsearch.client.indices.CreateIndexResponse)1 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)1