修改outlook-AuthController @Controller为@RestController

This commit is contained in:
APLS 2020-04-15 00:35:15 +08:00
parent 738bc0976a
commit 31efe5feff
2 changed files with 22 additions and 22 deletions

View File

@ -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;

View File

@ -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<Outlook> 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<Outlook> 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);
}
}