Skip to content
This repository was archived by the owner on Nov 21, 2020. It is now read-only.

Commit a0155f4

Browse files
committed
feat: prepare for adding sso
1 parent a6abc06 commit a0155f4

File tree

7 files changed

+237
-2
lines changed

7 files changed

+237
-2
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@
275275

276276
<developers>
277277
<developer>
278-
<name>zhazhapan</name>
279-
<email>tao@zhazhapan.com</email>
278+
<name>easepan</name>
279+
<email>easepan@qq.com</email>
280280
<url>https://blog.csdn.net/qq_26954773</url>
281281
</developer>
282282
</developers>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.code4everything.boot.base;
2+
3+
import java.lang.ref.Reference;
4+
import java.util.Objects;
5+
6+
/**
7+
* 引用工具类
8+
*
9+
* @author pantao
10+
* @since 2019/6/26
11+
**/
12+
public class ReferenceUtils {
13+
14+
private ReferenceUtils() {}
15+
16+
/**
17+
* 获取引用值
18+
*
19+
* @param reference 值的引用
20+
* @param <T> 值类型
21+
*
22+
* @return 值
23+
*
24+
* @since 1.1.5
25+
*/
26+
public static <T> T safeUnwrap(Reference<T> reference) {
27+
return Objects.isNull(reference) ? null : reference.get();
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.code4everything.boot.web.sso;
2+
3+
import org.code4everything.boot.base.bean.BaseBean;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* @author pantao
9+
* @since 2019/6/26
10+
**/
11+
public class LoginVO implements BaseBean, Serializable {
12+
13+
private static final long serialVersionUID = -3238074886033708538L;
14+
15+
private String token;
16+
17+
private String redirectUrl;
18+
19+
public LoginVO() {}
20+
21+
public LoginVO(String token, String redirectUrl) {
22+
this.token = token;
23+
this.redirectUrl = redirectUrl;
24+
}
25+
26+
public String getToken() {
27+
return token;
28+
}
29+
30+
public void setToken(String token) {
31+
this.token = token;
32+
}
33+
34+
public String getRedirectUrl() {
35+
return redirectUrl;
36+
}
37+
38+
public void setRedirectUrl(String redirectUrl) {
39+
this.redirectUrl = redirectUrl;
40+
}
41+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.code4everything.boot.web.sso;
2+
3+
import cn.hutool.core.util.IdUtil;
4+
import org.code4everything.boot.web.mvc.Response;
5+
6+
import javax.annotation.Nullable;
7+
8+
/**
9+
* @author pantao
10+
* @since 2019/6/26
11+
**/
12+
public interface SSOAware {
13+
14+
/**
15+
* 生成令牌
16+
*
17+
* @return
18+
*/
19+
default String generateToken() {
20+
return IdUtil.simpleUUID();
21+
}
22+
23+
/**
24+
* 检测用户是否已经登录
25+
*
26+
* @param sid 会话唯一标识符
27+
* @param addr
28+
* @param url 用户当前的页面路径
29+
*
30+
* @return 验证结果
31+
*
32+
* @since 1.1.5
33+
*/
34+
Response<String> validateSession(String sid, String addr, @Nullable String url);
35+
36+
/**
37+
* 用户登录成功后,注册子系统
38+
*
39+
* @param sid 会话唯一标识符
40+
* @param addr 子系统地址
41+
* @param user 用户
42+
*
43+
* @return 注册结果
44+
*
45+
* @since 1.1.5
46+
*/
47+
Response<LoginVO> registerSubSystem(String sid, String addr, Object user);
48+
49+
Response<Object> getUser(String sid);
50+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.code4everything.boot.web.sso;
2+
3+
/**
4+
* @author pantao
5+
* @since 2019/6/26
6+
**/
7+
public class SSOUtils {
8+
9+
private static final SSOAware aware = new SimpleSSO();
10+
11+
private SSOUtils() {}
12+
13+
public static String validateSession(SSOAware ssoAware, String sid) {
14+
}
15+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package org.code4everything.boot.web.sso;
2+
3+
import cn.hutool.core.util.StrUtil;
4+
import org.code4everything.boot.base.ReferenceUtils;
5+
import org.code4everything.boot.base.constant.IntegerConsts;
6+
import org.code4everything.boot.web.mvc.Response;
7+
8+
import javax.annotation.Nullable;
9+
import java.lang.ref.SoftReference;
10+
import java.lang.ref.WeakReference;
11+
import java.util.HashSet;
12+
import java.util.Map;
13+
import java.util.Objects;
14+
import java.util.Set;
15+
import java.util.concurrent.ConcurrentHashMap;
16+
17+
/**
18+
* @author pantao
19+
* @since 2019/6/26
20+
**/
21+
class SimpleSSO implements SSOAware {
22+
23+
private String loginUrl;
24+
25+
private int sessionTimeout;
26+
27+
/**
28+
* 缓存登录的用户
29+
*
30+
* @since 1.1.5
31+
*/
32+
private Map<String, SoftReference<Object>> userCache = new ConcurrentHashMap<>(1024);
33+
34+
/**
35+
* 缓存注册的子系统
36+
*
37+
* @since 1.1.5
38+
*/
39+
private Map<String, SoftReference<Set<String>>> subSysCache = new ConcurrentHashMap<>(1024);
40+
41+
/**
42+
* 缓存重定向路径
43+
*
44+
* @since 1.1.5
45+
*/
46+
private Map<String, WeakReference<String>> urlCache = new ConcurrentHashMap<>(1024);
47+
48+
public SimpleSSO() {
49+
this("/error", IntegerConsts.ONE_DAY_SECONDS);
50+
}
51+
52+
public SimpleSSO(String loginUrl, int sessionTimeout) {
53+
this.loginUrl = loginUrl;
54+
}
55+
56+
public SimpleSSO loginUrl(String loginUrl) {
57+
this.loginUrl = loginUrl;
58+
return this;
59+
}
60+
61+
@Override
62+
public Response<String> validateSession(String sid, String addr, @Nullable String url) {
63+
if (userCache.containsKey(sid)) {
64+
addSubSys(sid, addr);
65+
return new Response<>("用户已登录", generateToken());
66+
}
67+
if (StrUtil.isNotEmpty(url)) {
68+
urlCache.put(sid, new WeakReference<>(url));
69+
}
70+
return new Response<String>().error("请求用户登录").setData(loginUrl);
71+
}
72+
73+
@Override
74+
public Response<LoginVO> registerSubSystem(String sid, String addr, Object user) {
75+
userCache.put(sid, new SoftReference<>(user));
76+
addSubSys(sid, addr);
77+
String url = ReferenceUtils.safeUnwrap(urlCache.get(sid));
78+
return new Response<>("登录成功", new LoginVO(generateToken(), url));
79+
}
80+
81+
@Override
82+
public Response<Object> getUser(String sid) {
83+
return new Response<>(userCache.containsKey(sid) ? userCache.get(sid).get() : null);
84+
}
85+
86+
private void addSubSys(String sid, String addr) {
87+
Set<String> subs = ReferenceUtils.safeUnwrap(subSysCache.get(sid));
88+
if (Objects.isNull(subs)) {
89+
subs = new HashSet<>(8);
90+
subSysCache.put(sid, new SoftReference<>(subs));
91+
}
92+
subs.add(addr);
93+
}
94+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* 单点登录的简单封装
3+
*
4+
* @since 1.1.5
5+
*/
6+
package org.code4everything.boot.web.sso;

0 commit comments

Comments
 (0)