|
@@ -0,0 +1,78 @@
|
|
|
+package com.xjrsoft.module.system.controller;
|
|
|
+
|
|
|
+import cn.dev33.satoken.context.SaHolder;
|
|
|
+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.StpUtil;
|
|
|
+import cn.dev33.satoken.util.SaResult;
|
|
|
+import com.xjrsoft.common.annotation.XjrLog;
|
|
|
+import com.xjrsoft.common.constant.GlobalConstant;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Oauth2
|
|
|
+ */
|
|
|
+@Api(tags = "Oauth2")
|
|
|
+@RestController
|
|
|
+@AllArgsConstructor
|
|
|
+public class Oauth2Controller {
|
|
|
+
|
|
|
+ // 处理所有OAuth相关请求
|
|
|
+ @RequestMapping("/oauth2/*")
|
|
|
+ @ApiOperation(value = "oauth2", notes = "处理所有OAuth相关请求")
|
|
|
+ @XjrLog(value = "处理所有OAuth相关请求")
|
|
|
+ public Object request() {
|
|
|
+ System.out.println("------- 进入请求: " + SaHolder.getRequest().getUrl());
|
|
|
+
|
|
|
+ return SaOAuth2Handle.serverRequest();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Sa-OAuth2 定制化配置
|
|
|
+ @Autowired
|
|
|
+ public void setSaOAuth2Config(SaOAuth2Config cfg) {
|
|
|
+ // 配置:未登录时返回的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);
|
|
|
+ SaResponse res = SaHolder.getResponse();
|
|
|
+ res.redirect("http://127.0.0.1:9000/#/login");
|
|
|
+ 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) -> {
|
|
|
+ if ("sa".equals(name) && "123456".equals(pwd)) {
|
|
|
+ StpUtil.login(10001);
|
|
|
+ return SaResult.ok();
|
|
|
+ }
|
|
|
+ return SaResult.error("账号名或密码错误");
|
|
|
+ }).
|
|
|
+ // 配置:确认授权时返回的View
|
|
|
+ setConfirmView((clientId, scope) -> {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("clientId", clientId);
|
|
|
+ map.put("scope", scope);
|
|
|
+ return new ModelAndView("confirm.html", map);
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|