PIG 整合 Guns 单点登录
后端开发
- 基础概念
- 功能使用
- 认证授权
前端开发
PIG 整合 Guns 单点登录
版本说明
- 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");
//登录并创建 token
String token = authService.login(username);
return new RedirectView("/");
}
前端使用
http://localhost:8087/sso/login
退出修改
LoginController.java
@RequestMapping(value = "/logout")
@ResponseBody
public void logOut(HttpServletResponse response) {
authService.logout();
response.sendRedirect("http://localhost:3000/logout?redirect_url="+ URLEncoder.encode("http://localhost:8087"));
}
♥️ 获取支持
遇到问题?
如果您在使用过程中遇到任何问题、有功能建议或需求,请点击此卡片前往 Gitee 仓库提交 Issue。