Okta 登录
# 1. 申请应用
# 1.1 创建第三方授权应用
- 注册并登录 Okta 后台管理控制台:okta.com (opens new window)。在 Okta 管理控制台内配置 OpenID Connect 应用程序:
在“创建新应用程序”页面上,选择“Web”。 填写应用程序设置,然后单击“完成”。登录重定向URI必须与用户可以重定向到的URI及其授权代码匹配。 2. 新建应用
- 从导航中,依次选择 Applications - Applications。
- 在“Applications”页面上,点击“Add Application”按钮。
- 在新页面中,点击“Create New App“按钮
- 选择”Web - OpenID Connect“
- 按照提示输入应用信息
注意
注意 Login redirect URIs
(登录回调) 和 Logout redirect URIs
(退出回调),两个参数不要配错了,登录回调必须配置。
- 配置应用
应用创建完成后,浏览器调整到应用配置页面。接下来修改 General Settings 信息
如上图,勾上”Refresh Token“复选框后保存,只有勾上该选项,才可使用刷新令牌的接口。
注意
JustAuth 1.16.0 后,AuthConfig
中新增了一个domainPrefix
参数,表示域名前缀。
以本测试应用为例,domainPrefix
就是 Okta domain
这个属性值去掉 .okta.com
后的字符串,即 justautha
- 配置用户访问权
注意
这一步为必须的操作,否则会抛出该用户不被允许登录的异常。
切换到 ”Assignments“ 选项卡页面,点击 ”Assign“ 下拉框
这儿可以选择将应用分配给用户或者分组,分组的权限更大。此处仅为测试,选择了”Assign to People“
记录以下几个信息:Client ID
、Client secret
、Login redirect URIs
和前面提到的domainPrefix
,后面我们会用到。
重要提示
“应用密钥”可保护你应用程序的安全,因此请确保其不会泄露!也不要与任何人共享你的“应用密钥”!!!
# 2. 集成JustAuth
# 2.1 引入依赖
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>${latest.version}</version>
</dependency>
2
3
4
5
${latest.version}
推荐使用最新版本,快照版:
,可以在这儿 (opens new window)获取最新的版本信息。
# 2.2 创建Request
AuthRequest authRequest = new AuthOktaRequest(AuthConfig.builder()
.clientId("Client ID")
.clientSecret("Client Secret")
.redirectUri("应用回调地址")
.domainPrefix("Okta domain Prefix")
.build());
2
3
4
5
6
# 2.3 生成授权地址
我们可以直接使用以下方式生成第三方平台的授权链接:
String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
这个链接我们可以直接后台重定向跳转,也可以返回到前端后,前端控制跳转。前端控制的好处就是,可以将第三方的授权页嵌入到iframe中,适配网站设计。
# 2.4 以上完整代码如下
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthOktaRequest;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
@RestController
@RequestMapping("/oauth")
public class RestAuthController {
@RequestMapping("/render")
public void renderAuth(HttpServletResponse response) throws IOException {
AuthRequest authRequest = getAuthRequest();
response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
}
@RequestMapping("/callback")
public Object login(AuthCallback callback) {
AuthRequest authRequest = getAuthRequest();
return authRequest.login(callback);
}
private AuthRequest getAuthRequest() {
return new AuthOktaRequest(AuthConfig.builder()
.clientId("Client ID")
.clientSecret("Client Secret")
.redirectUri("回调地址")
.domainPrefix("Okta domain Prefix")
.build());
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
授权链接访问成功后会看到以下页面内容:
点击“登录”即可完成 OAuth 登录。
# 3. 授权结果
注意
数据已脱敏
{
"code": 2000,
"data": {
"email": "xx",
"gender": "UNKNOWN",
"rawUserInfo": {
"sub": "xx",
"zoneinfo": "America/Los_Angeles",
"email_verified": true,
"updated_at": 1617076502,
"name": "yd z",
"preferred_username": "xx",
"locale": "en-US",
"given_name": "yd",
"family_name": "z",
"email": "xx"
},
"source": "OKTA",
"token": {
"accessToken": "xx",
"expireIn": 3600,
"idToken": "xx",
"refreshToken": "xx",
"refreshTokenExpireIn": 0,
"scope": "offline_access profile phone email address openid",
"tokenType": "Bearer"
},
"username": "yd z",
"uuid": "xx"
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 3. 推荐
官方推荐使用 JustAuth-demo (opens new window) 示例项目进行测试。
使用步骤:
- clone: https://github.com/justauth/JustAuth-demo.git (opens new window)
- 将上面申请的应用信息填入到
RestAuthController#getAuthRequest
方法的对应位置中:
- 启动项目,访问 http://localhost:8443 (opens new window)
- 选择对应的平台进行授权登录
- 登录完成后,可以访问http://localhost:8443/users (opens new window)查看已授权的用户
注意
- 如果直接使用 JustAuth-demo 项目进行测试,那么在配置测试应用的“回调地址”时要严格按照以下格式配置:
http://localhost:8443/oauth/callback/{平台名}
- 平台名参考
JustAuthPlatformInfo
枚举类names