Search in sources :

Example 6 with CustomException

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

the class OauthServiceImpl method oAuthCheck.

@Override
@Transactional(rollbackFor = Exception.class)
public String oAuthCheck(OauthBody body) {
    User current, other = new User();
    if (StrUtil.equals(body.getType(), OauthBody.GITHUB)) {
        Map<String, String> header = new HashMap<>(2);
        header.put("accept", "application/json");
        String url = "https://github.com/login/oauth/access_token?client_id=" + githubClientId + "&client_secret=" + githubSecret + "&code=" + body.getCode();
        JSONObject obj = HttpUtil.getObj(url, header, true);
        String accessToken = obj.getString("access_token");
        if (StrUtil.isEmpty(accessToken)) {
            throw new CustomException("github登陆失败,请稍后重试");
        }
        header.put("Authorization", "token " + accessToken);
        JSONObject userInfo = HttpUtil.getObj("https://api.github.com/user", header, true);
        other.setGithubId(userInfo.getLong("id"));
        if (other.getGithubId() == null) {
            log.error("github返回异常:{}", userInfo);
            throw new CustomException("登陆异常,请稍后重试");
        }
        other.setEmail(userInfo.getString("email"));
        other.setIcon(userInfo.getString("avatar_url"));
        other.setUsername(userInfo.getString("login"));
        current = userDao.selectByUserIdOrGithubId(null, other.getGithubId());
        if (current == null) {
            current = userDao.selectByUsernameOrEmail(null, other.getEmail());
        }
    } else {
        throw new CustomException("不支持的登陆方式" + body.getType());
    }
    User newest = dealOauth(current, other);
    return JwtUtil.encode(Collections.singletonMap("userId", String.valueOf(newest.getUserId())), CommonConstant.jwtSecret, body.isRememberMe() ? LONG_EXPIRE_TIME : SHORT_EXPIRE_TIME);
}
Also used : User(com.fanxb.bookmark.common.entity.po.User) JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) CustomException(com.fanxb.bookmark.common.exception.CustomException) Transactional(org.springframework.transaction.annotation.Transactional)

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