修改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"), NO_ROBOT_FOUND_(-10007,"No QQ robot corresponding to this token was found"),
STATE_HAS_EXPIRED(-10008,"state has expired, please re-authorize."), STATE_HAS_EXPIRED(-10008,"state has expired, please re-authorize."),
INVALID_EMAIL(-10009,"Invalid Email!"), 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 Integer code;
private String msg; private String msg;

View File

@ -1,10 +1,13 @@
package io.qyi.e5.outlook.controller; package io.qyi.e5.outlook.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 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.config.security.UsernamePasswordAuthenticationToken;
import io.qyi.e5.outlook.entity.Outlook; import io.qyi.e5.outlook.entity.Outlook;
import io.qyi.e5.outlook.service.IOutlookService; import io.qyi.e5.outlook.service.IOutlookService;
import io.qyi.e5.util.EncryptUtil; import io.qyi.e5.util.EncryptUtil;
import io.qyi.e5.util.ResultUtil;
import io.qyi.e5.util.redis.RedisUtil; import io.qyi.e5.util.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -13,6 +16,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.UUID; import java.util.UUID;
@ -23,7 +27,7 @@ import java.util.UUID;
* @author: 落叶随风 * @author: 落叶随风
* @create: 2020-02-24 16:02 * @create: 2020-02-24 16:02
**/ **/
@Controller @RestController
@RequestMapping("/outlook/auth2") @RequestMapping("/outlook/auth2")
public class AuthController { public class AuthController {
@Autowired @Autowired
@ -39,36 +43,30 @@ public class AuthController {
String authorizeUrl; String authorizeUrl;
@RequestMapping("/receive") @RequestMapping("/receive")
public String Receive(Model model, String code, String state, String session_state, HttpServletResponse response) throws Exception { public Result Receive(String code, String state, String session_state) throws Exception {
model.addAttribute("result", false);
if (!redisUtil.hasKey(states + state)) { if (!redisUtil.hasKey(states + state)) {
model.addAttribute("msg", "state已过期重新点击授权!"); return ResultUtil.error(-1, "state已过期重新点击授权!");
return "/user/authorization_outlook";
} }
// 这里不应该查询在进行授权时因该把基础数据丢到redis /*这里不应该查询,在进行授权时因该把基础数据丢到redis*/
QueryWrapper<Outlook> outlookQueryWrapper = new QueryWrapper<>(); QueryWrapper<Outlook> outlookQueryWrapper = new QueryWrapper<>();
outlookQueryWrapper.eq("github_id", redisUtil.get(states + state)); outlookQueryWrapper.eq("github_id", redisUtil.get(states + state));
Outlook outlook = outlookService.getOne(outlookQueryWrapper); Outlook outlook = outlookService.getOne(outlookQueryWrapper);
// 删除redis中的此键 /*删除redis中的此键*/
redisUtil.del(states + state); redisUtil.del(states + state);
if (outlook == null) { if (outlook == null) {
model.addAttribute("msg", "没有查询到此用户,请检查是否在系统中注册!"); return ResultUtil.error(-2, "没有查询到此用户,请检查是否在系统中注册!");
return "/user/authorization_outlook";
} }
System.out.println(outlook); System.out.println(outlook);
boolean authorization_code = outlookService.getTokenAndSave(code, outlook.getClientId(), outlook.getClientSecret(), "https://e5.qyi.io/outlook/auth2/receive" boolean authorization_code = outlookService.getTokenAndSave(code, outlook.getClientId(), outlook.getClientSecret(), "https://e5.qyi.io/outlook/auth2/receive"
, "authorization_code"); , "authorization_code");
if (authorization_code) { if (!authorization_code) {
model.addAttribute("result", true); return ResultUtil.error(-3, "未知错误,请联系管理员~");
} else {
model.addAttribute("msg", "未知错误,请联系管理员~");
} }
return "/user/authorization_outlook"; return ResultUtil.success();
} }
@ResponseBody
@RequestMapping("/getAuthorizeUrl") @RequestMapping("/getAuthorizeUrl")
public void getAuthorizeUrl(HttpServletResponse response) { public Result getAuthorizeUrl() {
// 查询此用户的github_id与 // 查询此用户的github_id与
QueryWrapper<Outlook> outlookQueryWrapper = new QueryWrapper<>(); QueryWrapper<Outlook> outlookQueryWrapper = new QueryWrapper<>();
UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
@ -76,15 +74,16 @@ public class AuthController {
Outlook outlook = outlookService.getOne(outlookQueryWrapper); Outlook outlook = outlookService.getOne(outlookQueryWrapper);
if (outlook != null) { if (outlook != null) {
if (outlook.getClientId().length() < 1 || outlook.getClientSecret().length() < 1) {
return ResultUtil.error(ResultEnum.NO_DATA_FOUND);
}
// 生成随机uuid标识用户 // 生成随机uuid标识用户
String state = EncryptUtil.getInstance().SHA1Hex(UUID.randomUUID().toString()); String state = EncryptUtil.getInstance().SHA1Hex(UUID.randomUUID().toString());
redisUtil.set(states + state, outlook.getGithubId(), 600); 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); String url = String.format(authorizeUrl, outlook.getClientId(), "https://e5.qyi.io/outlook/auth2/receive", state);
System.out.println(url); return ResultUtil.success(url);
response.setStatus(302); } else {
response.setHeader("Location", url); return ResultUtil.error(ResultEnum.NO_DATA_FOUND);
} }
} }