1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package com.boman.auth.controller;
- import cn.hutool.core.util.StrUtil;
- import com.alibaba.fastjson.JSON;
- import com.boman.common.core.utils.JSONArrayUtils;
- import com.boman.common.core.utils.StringUtils;
- import com.fasterxml.jackson.annotation.JsonFormat;
- import com.xkcoding.justauth.AuthRequestFactory;
- import io.micrometer.core.instrument.util.JsonUtils;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import me.zhyd.oauth.config.AuthSource;
- import me.zhyd.oauth.model.AuthCallback;
- import me.zhyd.oauth.model.AuthResponse;
- import me.zhyd.oauth.request.AuthRequest;
- import me.zhyd.oauth.utils.AuthStateUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- /**
- * @author tjf
- * @Date: 2021/05/24/11:41
- */
- @Slf4j
- @RestController
- @RequestMapping("/oauth")
- @RequiredArgsConstructor(onConstructor_ = @Autowired)
- public class OauthController {
- private final AuthRequestFactory factory;
- @GetMapping
- public List<String> list() {
- return factory.oauthList();
- }
- @GetMapping("/login/{type}")
- public void login(@PathVariable String type, HttpServletResponse response) throws IOException {
- AuthRequest authRequest = factory.get(type);
- //生成授权地址,并重定向
- String authorize = authRequest.authorize(type + "::" + AuthStateUtils.createState());
- response.sendRedirect(authorize);
- }
- /**
- * 回调登录
- * @param type
- * @param callback
- * @return
- */
- @RequestMapping("/{type}/callback")
- public AuthResponse login(@PathVariable String type, AuthCallback callback) {
- AuthRequest authRequest = factory.get(getAuthSource(type));
- AuthResponse response = authRequest.login(callback);
- log.info("【response】= {}", JSON.toJSONString(response));
- return response;
- }
- private String getAuthSource(String type) {
- if (StrUtil.isNotBlank(type)) {
- return type.toUpperCase();
- } else {
- throw new RuntimeException("类型为空");
- }
- }
- }
|