|
| 1 | +package com.cym.controller.adminPage; |
| 2 | + |
| 3 | +import cn.hutool.core.lang.UUID; |
| 4 | +import cn.hutool.core.util.StrUtil; |
| 5 | +import cn.hutool.http.HttpUtil; |
| 6 | +import cn.hutool.json.JSONObject; |
| 7 | +import cn.hutool.json.JSONUtil; |
| 8 | +import com.cym.ext.AdminExt; |
| 9 | +import com.cym.ext.Tree; |
| 10 | +import com.cym.model.Admin; |
| 11 | +import com.cym.model.Group; |
| 12 | +import com.cym.service.AdminService; |
| 13 | +import com.cym.service.GroupService; |
| 14 | +import com.cym.service.SettingService; |
| 15 | +import com.cym.sqlhelper.bean.Page; |
| 16 | +import com.cym.sqlhelper.utils.ConditionAndWrapper; |
| 17 | +import com.cym.utils.*; |
| 18 | +import com.google.zxing.BarcodeFormat; |
| 19 | +import com.google.zxing.EncodeHintType; |
| 20 | +import com.google.zxing.MultiFormatWriter; |
| 21 | +import com.google.zxing.WriterException; |
| 22 | +import com.google.zxing.client.j2se.MatrixToImageWriter; |
| 23 | +import com.google.zxing.common.BitMatrix; |
| 24 | +import com.jayway.jsonpath.JsonPath; |
| 25 | +import org.noear.solon.annotation.Controller; |
| 26 | +import org.noear.solon.annotation.Inject; |
| 27 | +import org.noear.solon.annotation.Mapping; |
| 28 | +import org.noear.solon.core.handle.Context; |
| 29 | +import org.noear.solon.core.handle.ModelAndView; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.*; |
| 35 | + |
| 36 | +@Controller |
| 37 | +@Mapping("/adminPage/sso") |
| 38 | +public class SSOController extends BaseController { |
| 39 | + Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 40 | + @Inject |
| 41 | + AdminService adminService; |
| 42 | + @Inject |
| 43 | + SettingService settingService; |
| 44 | + @Inject |
| 45 | + SendMailUtils sendCloudUtils; |
| 46 | + @Inject |
| 47 | + AuthUtils authUtils; |
| 48 | + @Inject |
| 49 | + GroupService groupService; |
| 50 | + @Inject |
| 51 | + RemoteController remoteController; |
| 52 | + |
| 53 | + @Mapping("") |
| 54 | + public ModelAndView index(ModelAndView modelAndView) { |
| 55 | + |
| 56 | + modelAndView.put("codeUrl", settingService.get("sso_codeUrl")); |
| 57 | + modelAndView.put("tokenUrl", settingService.get("sso_tokenUrl")); |
| 58 | + modelAndView.put("userinfoUrl", settingService.get("sso_userinfoUrl")); |
| 59 | + modelAndView.put("jsonpath", settingService.get("sso_jsonpath")); |
| 60 | + modelAndView.put("clientID", settingService.get("sso_clientID")); |
| 61 | + modelAndView.put("clientSecret", settingService.get("sso_clientSecret")); |
| 62 | + modelAndView.view("/adminPage/sso/index.html"); |
| 63 | + return modelAndView; |
| 64 | + } |
| 65 | + |
| 66 | + @Mapping("save") |
| 67 | + public JsonResult save(String codeUrl, String tokenUrl, String userinfoUrl, String jsonpath, String clientID, String clientSecret, String callbackUrl) { |
| 68 | + |
| 69 | + settingService.set("sso_codeUrl", codeUrl); |
| 70 | + settingService.set("sso_tokenUrl", tokenUrl); |
| 71 | + settingService.set("sso_userinfoUrl", userinfoUrl); |
| 72 | + settingService.set("sso_jsonpath", jsonpath); |
| 73 | + settingService.set("sso_clientID", clientID); |
| 74 | + settingService.set("sso_clientSecret", clientSecret); |
| 75 | + settingService.set("sso_callbackUrl", callbackUrl); |
| 76 | + |
| 77 | + return renderSuccess(); |
| 78 | + } |
| 79 | + |
| 80 | + @Mapping("redirect") |
| 81 | + public void redirect(Context ctx) { |
| 82 | + |
| 83 | + String codeUrl = settingService.get("sso_codeUrl"); |
| 84 | + String clientID = settingService.get("sso_clientID"); |
| 85 | + String callbackUrl = settingService.get("sso_callbackUrl"); |
| 86 | + |
| 87 | + String url = codeUrl + "?client_id=" + clientID + "&response_type=code&redirect_uri=" + callbackUrl + "&oauth_timestamp=" + System.currentTimeMillis() + "&state="; |
| 88 | + |
| 89 | + ctx.redirect(url); |
| 90 | + } |
| 91 | + |
| 92 | + @Mapping("code") |
| 93 | + public void code(String code, Context ctx) { |
| 94 | + |
| 95 | + String tokenUrl = settingService.get("sso_tokenUrl"); |
| 96 | + String userinfoUrl = settingService.get("sso_userinfoUrl"); |
| 97 | + String jsonpath = settingService.get("sso_jsonpath"); |
| 98 | + String clientID = settingService.get("sso_clientID"); |
| 99 | + String clientSecret = settingService.get("sso_clientSecret"); |
| 100 | + String callbackUrl = settingService.get("sso_callbackUrl"); |
| 101 | + |
| 102 | + String getTokenUrl = tokenUrl + "?grant_type=authorization_code&oauth_timestamp=" + System.currentTimeMillis() + "&client_id=" + clientID + "&client_secret=" + clientSecret + "&code=" + code |
| 103 | + + "&redirect_uri=" + callbackUrl; |
| 104 | + |
| 105 | + String post = HttpUtil.post(getTokenUrl, ""); |
| 106 | + |
| 107 | + JSONObject entries = JSONUtil.parseObj(post); |
| 108 | + String accessToken = entries.getStr("access_token"); |
| 109 | + |
| 110 | + String userInfoUrl = userinfoUrl + "?access_token=" + accessToken; |
| 111 | + |
| 112 | + String userinfoStr = HttpUtil.get(userInfoUrl); |
| 113 | + |
| 114 | + String read = JsonPath.read(userinfoStr, jsonpath); |
| 115 | + |
| 116 | + Admin admin = sqlHelper.findOneByQuery(new ConditionAndWrapper().eq(Admin::getName, read), Admin.class); |
| 117 | + |
| 118 | + admin.setAutoKey(UUID.randomUUID().toString()); // 生成自动登录code |
| 119 | + sqlHelper.updateById(admin); |
| 120 | + |
| 121 | + Context.current().sessionSet("localType", "local"); |
| 122 | + Context.current().sessionSet("isLogin", true); |
| 123 | + Context.current().sessionSet("admin", admin); |
| 124 | + Context.current().sessionRemove("imgCode"); // 立刻销毁验证码 |
| 125 | + |
| 126 | + ctx.redirect("/adminPage/monitor"); |
| 127 | + } |
| 128 | +} |
0 commit comments