效果展示
版本说明
- pig 2.10
- guns vip 1.0
pig 客户端表
INSERT INTO `pig`.`sys_oauth_client_details`(`client_id`, `resource_ids`, `client_secret`, `scope`, `authorized_grant_types`, `web_server_redirect_uri`, `authorities`, `access_token_validity`, `refresh_token_validity`, `additional_information`, `autoapprove`) VALUES ('guns', NULL, 'guns', 'server', 'refresh_token,authorization_code', 'http://localhost:8087/sso/code', NULL, 43200, 2592001, NULL, 'true');
调整 WebSecurityConfig

新增SSO 登录接口
- LoginController 新增如下端点, 代码中的URL 根据实际地址修改即可
注意:授权码模式下回调地址不能使用localhost,可以使用127.0.0.1
@SneakyThrows@RequestMapping(value = "/sso/login", method = RequestMethod.GET)public RedirectView loginSso(HttpServletRequest request, HttpServletResponse response) {String url = String.format("%s?response_type=code&scope=%s&client_id=%s&state=%s&redirect_uri=%s","http://localhost:3000/oauth/authorize","server","guns","guns",URLEncoder.encode("http://127.0.0.1:8087/sso/code", "UTF-8"));return new RedirectView(url);}@SneakyThrows@ResponseBody@RequestMapping(value = "/sso/code", method = RequestMethod.GET)public RedirectView loginCode(HttpServletRequest request, HttpServletResponse response) {String code = super.getPara("code");String template = "http://localhost:3000/oauth/token?grant_type=authorization_code&scope=%s&code=%s&redirect_uri=%s";final String url = String.format(template, "server", code, URLEncoder.encode(request.getRequestURL().toString(), "UTF-8"));String body = HttpRequest.get(url).basicAuth("guns", "guns").execute().body();JSONObject parse = JSONUtil.parseObj(body);String username = parse.getStr("username");//登录并创建tokenString token = authService.login(username);return new RedirectView("/");}
前端使用
http://localhost:8087/sso/login


