From 31efe5feff5a1bda88acbe68d8a3c93b468e13df Mon Sep 17 00:00:00 2001 From: APLS Date: Wed, 15 Apr 2020 00:35:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9outlook-AuthController=20@Con?= =?UTF-8?q?troller=E4=B8=BA@RestController?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/qyi/e5/bean/result/ResultEnum.java | 3 +- .../e5/outlook/controller/AuthController.java | 41 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/java/io/qyi/e5/bean/result/ResultEnum.java b/src/main/java/io/qyi/e5/bean/result/ResultEnum.java index 9455970..0d018dd 100644 --- a/src/main/java/io/qyi/e5/bean/result/ResultEnum.java +++ b/src/main/java/io/qyi/e5/bean/result/ResultEnum.java @@ -19,7 +19,8 @@ public enum ResultEnum { NO_ROBOT_FOUND_(-10007,"No QQ robot corresponding to this token was found"), STATE_HAS_EXPIRED(-10008,"state has expired, please re-authorize."), INVALID_EMAIL(-10009,"Invalid Email!"), - INVALID_format(-10010, "Invalid format"); + INVALID_format(-10010, "Invalid format"), + NO_DATA_FOUND(-10011,"No data found"); private Integer code; private String msg; diff --git a/src/main/java/io/qyi/e5/outlook/controller/AuthController.java b/src/main/java/io/qyi/e5/outlook/controller/AuthController.java index 0d7f5c3..6bd2f39 100644 --- a/src/main/java/io/qyi/e5/outlook/controller/AuthController.java +++ b/src/main/java/io/qyi/e5/outlook/controller/AuthController.java @@ -1,10 +1,13 @@ package io.qyi.e5.outlook.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import io.qyi.e5.bean.result.Result; +import io.qyi.e5.bean.result.ResultEnum; import io.qyi.e5.config.security.UsernamePasswordAuthenticationToken; import io.qyi.e5.outlook.entity.Outlook; import io.qyi.e5.outlook.service.IOutlookService; import io.qyi.e5.util.EncryptUtil; +import io.qyi.e5.util.ResultUtil; import io.qyi.e5.util.redis.RedisUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -13,6 +16,7 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.util.UUID; @@ -23,7 +27,7 @@ import java.util.UUID; * @author: 落叶随风 * @create: 2020-02-24 16:02 **/ -@Controller +@RestController @RequestMapping("/outlook/auth2") public class AuthController { @Autowired @@ -39,36 +43,30 @@ public class AuthController { String authorizeUrl; @RequestMapping("/receive") - public String Receive(Model model, String code, String state, String session_state, HttpServletResponse response) throws Exception { - model.addAttribute("result", false); + public Result Receive(String code, String state, String session_state) throws Exception { if (!redisUtil.hasKey(states + state)) { - model.addAttribute("msg", "state已过期,重新点击授权!"); - return "/user/authorization_outlook"; + return ResultUtil.error(-1, "state已过期,重新点击授权!"); } -// 这里不应该查询,在进行授权时因该把基础数据丢到redis + /*这里不应该查询,在进行授权时因该把基础数据丢到redis*/ QueryWrapper outlookQueryWrapper = new QueryWrapper<>(); outlookQueryWrapper.eq("github_id", redisUtil.get(states + state)); Outlook outlook = outlookService.getOne(outlookQueryWrapper); -// 删除redis中的此键 + /*删除redis中的此键*/ redisUtil.del(states + state); if (outlook == null) { - model.addAttribute("msg", "没有查询到此用户,请检查是否在系统中注册!"); - return "/user/authorization_outlook"; + return ResultUtil.error(-2, "没有查询到此用户,请检查是否在系统中注册!"); } System.out.println(outlook); boolean authorization_code = outlookService.getTokenAndSave(code, outlook.getClientId(), outlook.getClientSecret(), "https://e5.qyi.io/outlook/auth2/receive" , "authorization_code"); - if (authorization_code) { - model.addAttribute("result", true); - } else { - model.addAttribute("msg", "未知错误,请联系管理员~"); + if (!authorization_code) { + return ResultUtil.error(-3, "未知错误,请联系管理员~"); } - return "/user/authorization_outlook"; + return ResultUtil.success(); } - @ResponseBody @RequestMapping("/getAuthorizeUrl") - public void getAuthorizeUrl(HttpServletResponse response) { + public Result getAuthorizeUrl() { // 查询此用户的github_id与 QueryWrapper outlookQueryWrapper = new QueryWrapper<>(); UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); @@ -76,15 +74,16 @@ public class AuthController { Outlook outlook = outlookService.getOne(outlookQueryWrapper); if (outlook != null) { + if (outlook.getClientId().length() < 1 || outlook.getClientSecret().length() < 1) { + return ResultUtil.error(ResultEnum.NO_DATA_FOUND); + } // 生成随机uuid标识用户 String state = EncryptUtil.getInstance().SHA1Hex(UUID.randomUUID().toString()); redisUtil.set(states + state, outlook.getGithubId(), 600); - - System.out.println(outlook); String url = String.format(authorizeUrl, outlook.getClientId(), "https://e5.qyi.io/outlook/auth2/receive", state); - System.out.println(url); - response.setStatus(302); - response.setHeader("Location", url); + return ResultUtil.success(url); + } else { + return ResultUtil.error(ResultEnum.NO_DATA_FOUND); } }