|
@@ -1,14 +1,20 @@
|
|
|
package com.xjrsoft.module.system.controller;
|
|
|
|
|
|
+import cn.dev33.satoken.config.SaCookieConfig;
|
|
|
import cn.dev33.satoken.context.SaHolder;
|
|
|
+import cn.dev33.satoken.context.model.SaCookie;
|
|
|
import cn.dev33.satoken.context.model.SaRequest;
|
|
|
import cn.dev33.satoken.context.model.SaResponse;
|
|
|
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
|
|
|
import cn.dev33.satoken.oauth2.logic.SaOAuth2Handle;
|
|
|
+import cn.dev33.satoken.stp.StpLogic;
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
import cn.dev33.satoken.util.SaResult;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
import com.xjrsoft.common.annotation.XjrLog;
|
|
|
import com.xjrsoft.common.constant.GlobalConstant;
|
|
|
+import com.xjrsoft.common.utils.RedisUtil;
|
|
|
+import com.xjrsoft.config.CommonPropertiesConfig;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.AllArgsConstructor;
|
|
@@ -17,6 +23,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -28,13 +36,16 @@ import java.util.Map;
|
|
|
@AllArgsConstructor
|
|
|
public class Oauth2Controller {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private CommonPropertiesConfig commonPropertiesConfig;
|
|
|
+
|
|
|
+ private final RedisUtil redisUtil;
|
|
|
+
|
|
|
// 处理所有OAuth相关请求
|
|
|
@RequestMapping("/oauth2/*")
|
|
|
@ApiOperation(value = "oauth2", notes = "处理所有OAuth相关请求")
|
|
|
@XjrLog(value = "处理所有OAuth相关请求")
|
|
|
public Object request() {
|
|
|
- System.out.println("------- 进入请求: " + SaHolder.getRequest().getUrl());
|
|
|
-
|
|
|
return SaOAuth2Handle.serverRequest();
|
|
|
}
|
|
|
|
|
@@ -44,20 +55,26 @@ public class Oauth2Controller {
|
|
|
// 配置:未登录时返回的View
|
|
|
cfg.setNotLoginView(() -> {
|
|
|
SaRequest req = SaHolder.getRequest();
|
|
|
-// Map<String, String> paramMap = req.getp
|
|
|
-// StringBuilder param = new StringBuilder();
|
|
|
-// paramMap.forEach((k, v) -> {
|
|
|
-// param.append("&").append(k).append("=").append(v);
|
|
|
-// });
|
|
|
-// param.deleteCharAt(0);
|
|
|
+ Map<String, String> paramMap = req.getParamMap();
|
|
|
+ StringBuilder param = new StringBuilder();
|
|
|
+ paramMap.forEach((k, v) -> {
|
|
|
+ param.append("&").append(k).append("=").append(v);
|
|
|
+ });
|
|
|
+ param.deleteCharAt(0);
|
|
|
+ String callBackUrl = String.format("%s?%s", req.getUrl(), param);
|
|
|
+
|
|
|
+ String key = GlobalConstant.OAUTH2 + IdUtil.simpleUUID();
|
|
|
+ redisUtil.set(key, callBackUrl, 86400);
|
|
|
+
|
|
|
SaResponse res = SaHolder.getResponse();
|
|
|
- res.redirect("http://127.0.0.1:9000/#/login");
|
|
|
+ SaCookie cookie = new SaCookie()
|
|
|
+ .setName("Oauth2Info")
|
|
|
+ .setValue(key);
|
|
|
+
|
|
|
+ res.addCookie(cookie);
|
|
|
+
|
|
|
+ res.redirect(String.format("%s/#/login", commonPropertiesConfig.getDomainWeb()));
|
|
|
return null;
|
|
|
-// return new ModelAndView("login.html");
|
|
|
-// String msg = "当前会话在OAuth-Server端尚未登录,请先访问"
|
|
|
-// + "<a href='/oauth2/doLogin?name=sa&pwd=123456' target='_blank'> doLogin登录 </a>"
|
|
|
-// + "进行登录之后,刷新页面开始授权";
|
|
|
-// return msg;
|
|
|
}).
|
|
|
// 配置:登录处理函数
|
|
|
setDoLoginHandle((name, pwd) -> {
|
|
@@ -68,7 +85,7 @@ public class Oauth2Controller {
|
|
|
return SaResult.error("账号名或密码错误");
|
|
|
}).
|
|
|
// 配置:确认授权时返回的View
|
|
|
- setConfirmView((clientId, scope) -> {
|
|
|
+ setConfirmView((clientId, scope) -> {
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
map.put("clientId", clientId);
|
|
|
map.put("scope", scope);
|