环境说明
注意:授权码模式下回调地址不能使用localhost
,可以使用127.0.0.1
@Clear(PermissionInterceptor.class)
public void sso(String code) {
if (StrUtil.isBlank(code)){
String url = String.format("%s?response_type=code&scope=%s&client_id=%s&state=%s&redirect_uri=%s",
"http://192.168.0.33:3000/oauth/authorize",
"server",
"jfinal",
"jfinal",
URLEncoder.encode("http://127.0.0.1:8080/admin/sso"));
redirect(url);
return;
}
String template = "http://192.168.0.33:3000/oauth/token?grant_type=authorization_code&scope=%s&code=%s&redirect_uri=%s";
final String url = String.format(template, "server", code, URLEncoder.encode("http://127.0.0.1:8080/admin/sso"));
String body = HttpRequest.get(url)
.basicAuth("jfinal", "jfinal")
.execute()
.body();
JSONObject parse = JSONUtil.parseObj(body);
String username = parse.getStr("username");
//登录并创建token
User user = userService.findFistByUsername(username);
Ret ret = Ret.ok().set("user_id", user.getId());
if (ret.isOk()) {
SessionUtils.record(user.getId());
CookieUtil.put(this, JPressConsts.COOKIE_UID, user.getId());
}
redirect("/admin");
}
- AdminInterceptor 暴露此端点
前端使用
http://127.0.0.1:8080/admin/sso