优化单点登录步骤

This commit is contained in:
click33 2021-07-09 02:31:34 +08:00
parent 0a5c5da4b4
commit 936dfe333d
34 changed files with 206 additions and 264 deletions

View File

@ -14,22 +14,22 @@ public class SaSsoConsts {
public static final class Api {
/** SSO-Server端授权地址 */
public static String ssoAuth = "/ssoAuth";
public static String ssoAuth = "/sso/auth";
/** SSO-Server端RestAPI 登录接口 */
public static String ssoDoLogin = "/ssoDoLogin";
public static String ssoDoLogin = "/sso/doLogin";
/** SSO-Server端校验ticket 获取账号id */
public static String ssoCheckTicket = "/ssoCheckTicket";
public static String ssoCheckTicket = "/sso/checkTicket";
/** SSO-Server端 (and Client端):单点注销 */
public static String ssoLogout = "/ssoLogout";
public static String ssoLogout = "/sso/logout";
/** SSO-Client端登录地址 */
public static String ssoLogin = "/ssoLogin";
public static String ssoLogin = "/sso/login";
/** SSO-Client端单点注销的回调 */
public static String ssoLogoutCall = "/ssoLogoutCall";
public static String ssoLogoutCall = "/sso/logoutCall";
}

View File

@ -8,7 +8,7 @@ import cn.dev33.satoken.context.model.SaResponse;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.sso.SaSsoConsts.Api;
import cn.dev33.satoken.sso.SaSsoConsts.ParamName;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;
@ -29,16 +29,17 @@ public class SaSsoHandle {
SaRequest req = SaHolder.getRequest();
SaResponse res = SaHolder.getResponse();
SaSsoConfig sso = SaManager.getConfig().getSso();
StpLogic stpLogic = SaSsoUtil.saSsoTemplate.stpLogic;
// ---------- SSO-Server端单点登录授权地址
if(match(Api.ssoAuth)) {
// ---------- 此处两种情况分开处理
// 情况1在SSO认证中心尚未登录则先去登登录
if(StpUtil.isLogin() == false) {
if(stpLogic.isLogin() == false) {
return sso.notLoginView.get();
}
// 情况2在SSO认证中心已经登录开始构建授权重定向地址下放ticket
String redirectUrl = SaSsoUtil.buildRedirectUrl(StpUtil.getLoginId(), req.getParameter(ParamName.redirect));
String redirectUrl = SaSsoUtil.buildRedirectUrl(stpLogic.getLoginId(), req.getParameter(ParamName.redirect));
return res.redirect(redirectUrl);
}
@ -88,6 +89,7 @@ public class SaSsoHandle {
SaRequest req = SaHolder.getRequest();
SaResponse res = SaHolder.getResponse();
SaSsoConfig sso = SaManager.getConfig().getSso();
StpLogic stpLogic = SaSsoUtil.saSsoTemplate.stpLogic;
// ---------- SSO-Client端登录地址
if(match(Api.ssoLogin)) {
@ -95,7 +97,7 @@ public class SaSsoHandle {
String ticket = req.getParameter(ParamName.ticket);
// 如果当前Client端已经登录则无需访问SSO认证中心可以直接返回
if(StpUtil.isLogin()) {
if(stpLogic.isLogin()) {
return res.redirect(back);
}
/*
@ -124,7 +126,7 @@ public class SaSsoHandle {
}
// ------- 2如果loginId有值说明ticket有效进行登录并重定向至back地址
if(loginId != null ) {
StpUtil.login(loginId);
stpLogic.login(loginId);
return res.redirect(back);
} else {
// 如果ticket无效:
@ -135,7 +137,7 @@ public class SaSsoHandle {
// ---------- SSO-Client端单点注销 [模式二]
if(match(Api.ssoLogout) && sso.isSlo && sso.isHttp == false) {
StpUtil.logout();
stpLogic.logout();
if(req.getParameter(ParamName.back) == null) {
return SaResult.ok("单点注销成功");
} else {
@ -146,11 +148,11 @@ public class SaSsoHandle {
// ---------- SSO-Client端单点注销 [模式三]
if(match(Api.ssoLogout) && sso.isSlo && sso.isHttp) {
// 如果未登录则无需注销
if(StpUtil.isLogin() == false) {
if(stpLogic.isLogin() == false) {
return SaResult.ok();
}
// 调用SSO-Server认证中心API
String url = SaSsoUtil.buildSloUrl(StpUtil.getLoginId());
String url = SaSsoUtil.buildSloUrl(stpLogic.getLoginId());
String body = String.valueOf(sso.sendHttp.apply(url));
if(SaSsoConsts.OK.equals(body)) {
if(req.getParameter(ParamName.back) == null) {
@ -168,7 +170,7 @@ public class SaSsoHandle {
String secretkey = req.getParameter(ParamName.secretkey);
SaSsoUtil.checkSecretkey(secretkey);
StpUtil.logoutByTokenValue(StpUtil.getTokenValueByLoginId(loginId));
stpLogic.logoutByTokenValue(stpLogic.getTokenValueByLoginId(loginId));
return SaSsoConsts.OK;
}

View File

@ -9,7 +9,7 @@ import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.config.SaSsoConfig;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.sso.SaSsoConsts.ParamName;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.util.SaFoxUtil;
/**
@ -17,14 +17,19 @@ import cn.dev33.satoken.util.SaFoxUtil;
* @author kong
*
*/
public interface SaSsoInterface {
public class SaSsoTemplate {
public StpLogic stpLogic;
public SaSsoTemplate(StpLogic stpLogic) {
this.stpLogic = stpLogic;
}
/**
* 创建一个 Ticket码
* @param loginId 账号id
* @return 票据
*/
public default String createTicket(Object loginId) {
public String createTicket(Object loginId) {
// 随机一个ticket
String ticket = randomTicket(loginId);
@ -41,7 +46,7 @@ public interface SaSsoInterface {
* 删除一个 Ticket码
* @param ticket Ticket码
*/
public default void deleteTicket(String ticket) {
public void deleteTicket(String ticket) {
Object loginId = getLoginId(ticket);
if(loginId != null) {
SaManager.getSaTokenDao().delete(splicingKeyTicketToId(ticket));
@ -55,7 +60,7 @@ public interface SaSsoInterface {
* @param redirect Client端提供的重定向地址
* @return see note
*/
public default String buildRedirectUrl(Object loginId, String redirect) {
public String buildRedirectUrl(Object loginId, String redirect) {
// 校验重定向地址
checkRedirectUrl(redirect);
@ -79,7 +84,7 @@ public interface SaSsoInterface {
* @param ticket Ticket码
* @return 账号id
*/
public default Object getLoginId(String ticket) {
public Object getLoginId(String ticket) {
if(SaFoxUtil.isEmpty(ticket)) {
return null;
}
@ -93,7 +98,7 @@ public interface SaSsoInterface {
* @param cs 要转换的类型
* @return 账号id
*/
public default <T> T getLoginId(String ticket, Class<T> cs) {
public <T> T getLoginId(String ticket, Class<T> cs) {
return SaFoxUtil.getValueByType(getLoginId(ticket), cs);
}
@ -102,7 +107,7 @@ public interface SaSsoInterface {
* @param ticket Ticket码
* @return 账号id
*/
public default Object checkTicket(String ticket) {
public Object checkTicket(String ticket) {
Object loginId = getLoginId(ticket);
if(loginId != null) {
deleteTicket(ticket);
@ -114,7 +119,7 @@ public interface SaSsoInterface {
* 校验重定向url合法性
* @param url 下放ticket的url地址
*/
public default void checkRedirectUrl(String url) {
public void checkRedirectUrl(String url) {
// 1是否是一个有效的url
if(SaFoxUtil.isUrl(url) == false) {
@ -144,7 +149,7 @@ public interface SaSsoInterface {
* @param back 回调路径
* @return [SSO-Server端-认证地址 ]
*/
public default String buildServerAuthUrl(String clientLoginUrl, String back) {
public String buildServerAuthUrl(String clientLoginUrl, String back) {
// 服务端认证地址
String serverUrl = SaManager.getConfig().getSso().getAuthUrl();
@ -165,7 +170,7 @@ public interface SaSsoInterface {
* @param url url
* @return 编码过后的url
*/
public default String encodeBackParam(String url) {
public String encodeBackParam(String url) {
// 获取back参数所在位置
int index = url.indexOf("?" + ParamName.back + "=");
@ -191,7 +196,7 @@ public interface SaSsoInterface {
* @param loginId 账号id
* @return 票据
*/
public default String randomTicket(Object loginId) {
public String randomTicket(Object loginId) {
return SaFoxUtil.getRandomString(64);
}
@ -202,7 +207,7 @@ public interface SaSsoInterface {
* 校验secretkey秘钥是否有效
* @param secretkey 秘钥
*/
public default void checkSecretkey(String secretkey) {
public void checkSecretkey(String secretkey) {
if(secretkey == null || secretkey.isEmpty() || secretkey.equals(SaManager.getConfig().getSso().getSecretkey()) == false) {
throw new SaTokenException("无效秘钥:" + secretkey);
}
@ -214,7 +219,7 @@ public interface SaSsoInterface {
* @param ssoLogoutCallUrl 单点注销时的回调URL
* @return 构建完毕的URL
*/
public default String buildCheckTicketUrl(String ticket, String ssoLogoutCallUrl) {
public String buildCheckTicketUrl(String ticket, String ssoLogoutCallUrl) {
String url = SaManager.getConfig().getSso().getCheckTicketUrl();
// 拼接ticket参数
url = SaFoxUtil.joinParam(url, ParamName.ticket, ticket);
@ -231,13 +236,13 @@ public interface SaSsoInterface {
* @param loginId 账号id
* @param sloCallbackUrl 单点注销时的回调URL
*/
public default void registerSloCallbackUrl(Object loginId, String sloCallbackUrl) {
public void registerSloCallbackUrl(Object loginId, String sloCallbackUrl) {
if(loginId == null || sloCallbackUrl == null || sloCallbackUrl.isEmpty()) {
return;
}
Set<String> urlSet = StpUtil.getSessionByLoginId(loginId).get(SaSsoConsts.SLO_CALLBACK_SET_KEY, ()-> new HashSet<String>());
Set<String> urlSet = stpLogic.getSessionByLoginId(loginId).get(SaSsoConsts.SLO_CALLBACK_SET_KEY, ()-> new HashSet<String>());
urlSet.add(sloCallbackUrl);
StpUtil.getSessionByLoginId(loginId).set(SaSsoConsts.SLO_CALLBACK_SET_KEY, urlSet);
stpLogic.getSessionByLoginId(loginId).set(SaSsoConsts.SLO_CALLBACK_SET_KEY, urlSet);
}
/**
@ -245,9 +250,9 @@ public interface SaSsoInterface {
* @param loginId 账号id
* @param fun 调用方法
*/
public default void forEachSloUrl(Object loginId, CallSloUrlFunction fun) {
public void forEachSloUrl(Object loginId, CallSloUrlFunction fun) {
String secretkey = SaManager.getConfig().getSso().getSecretkey();
Set<String> urlSet = StpUtil.getSessionByLoginId(loginId).get(SaSsoConsts.SLO_CALLBACK_SET_KEY,
Set<String> urlSet = stpLogic.getSessionByLoginId(loginId).get(SaSsoConsts.SLO_CALLBACK_SET_KEY,
() -> new HashSet<String>());
for (String url : urlSet) {
@ -264,7 +269,7 @@ public interface SaSsoInterface {
* @param loginId 要注销的账号id
* @return 单点注销URL
*/
public default String buildSloUrl(Object loginId) {
public String buildSloUrl(Object loginId) {
SaSsoConfig ssoConfig = SaManager.getConfig().getSso();
String url = ssoConfig.getSloUrl();
url = SaFoxUtil.joinParam(url, ParamName.loginId, loginId);
@ -278,7 +283,7 @@ public interface SaSsoInterface {
* @param loginId 指定账号
* @param fun 调用方法
*/
public default void singleLogout(String secretkey, Object loginId, CallSloUrlFunction fun) {
public void singleLogout(String secretkey, Object loginId, CallSloUrlFunction fun) {
// step.1 校验秘钥
checkSecretkey(secretkey);
@ -287,7 +292,7 @@ public interface SaSsoInterface {
// step.3 Server端注销
// StpUtil.logoutByLoginId(loginId);
StpUtil.logoutByTokenValue(StpUtil.getTokenValueByLoginId(loginId));
stpLogic.logoutByTokenValue(stpLogic.getTokenValueByLoginId(loginId));
}
@ -299,7 +304,7 @@ public interface SaSsoInterface {
* @param ticket
* @return key
*/
public default String splicingKeyTicketToId(String ticket) {
public String splicingKeyTicketToId(String ticket) {
return SaManager.getConfig().getTokenName() + ":ticket:" + ticket;
}
@ -308,7 +313,7 @@ public interface SaSsoInterface {
* @param id 账号id
* @return key
*/
public default String splicingKeyIdToTicket(Object id) {
public String splicingKeyIdToTicket(Object id) {
return SaManager.getConfig().getTokenName() + ":id-ticket:" + id;
}

View File

@ -1,6 +1,7 @@
package cn.dev33.satoken.sso;
import cn.dev33.satoken.sso.SaSsoInterface.CallSloUrlFunction;
import cn.dev33.satoken.sso.SaSsoTemplate.CallSloUrlFunction;
import cn.dev33.satoken.stp.StpUtil;
/**
* Sa-Token-SSO 单点登录工具类
@ -10,9 +11,9 @@ import cn.dev33.satoken.sso.SaSsoInterface.CallSloUrlFunction;
public class SaSsoUtil {
/**
* 底层 SaSsoServerInterface 对象
* 底层 SaSsoTemplate 对象
*/
public static SaSsoInterface saSsoInterface = new SaSsoInterface() {};
public static SaSsoTemplate saSsoTemplate = new SaSsoTemplate(StpUtil.stpLogic);
/**
* 创建一个 Ticket票据
@ -20,7 +21,7 @@ public class SaSsoUtil {
* @return 票据
*/
public static String createTicket(Object loginId) {
return saSsoInterface.createTicket(loginId);
return saSsoTemplate.createTicket(loginId);
}
/**
@ -28,7 +29,7 @@ public class SaSsoUtil {
* @param ticket Ticket码
*/
public static void deleteTicket(String ticket) {
saSsoInterface.deleteTicket(ticket);
saSsoTemplate.deleteTicket(ticket);
}
/**
@ -38,7 +39,7 @@ public class SaSsoUtil {
* @return see note
*/
public static String buildRedirectUrl(Object loginId, String redirect) {
return saSsoInterface.buildRedirectUrl(loginId, redirect);
return saSsoTemplate.buildRedirectUrl(loginId, redirect);
}
/**
@ -47,7 +48,7 @@ public class SaSsoUtil {
* @return 账号id
*/
public static Object getLoginId(String ticket) {
return saSsoInterface.getLoginId(ticket);
return saSsoTemplate.getLoginId(ticket);
}
/**
@ -58,7 +59,7 @@ public class SaSsoUtil {
* @return 账号id
*/
public static <T> T getLoginId(String ticket, Class<T> cs) {
return saSsoInterface.getLoginId(ticket, cs);
return saSsoTemplate.getLoginId(ticket, cs);
}
/**
@ -67,7 +68,7 @@ public class SaSsoUtil {
* @return 账号id
*/
public static Object checkTicket(String ticket) {
return saSsoInterface.checkTicket(ticket);
return saSsoTemplate.checkTicket(ticket);
}
/**
@ -75,7 +76,7 @@ public class SaSsoUtil {
* @param url 下放ticket的url地址
*/
public static void checkAuthUrl(String url) {
saSsoInterface.checkRedirectUrl(url);
saSsoTemplate.checkRedirectUrl(url);
}
/**
@ -85,7 +86,7 @@ public class SaSsoUtil {
* @return [SSO-Server端-认证地址 ]
*/
public static String buildServerAuthUrl(String clientLoginUrl, String back) {
return saSsoInterface.buildServerAuthUrl(clientLoginUrl, back);
return saSsoTemplate.buildServerAuthUrl(clientLoginUrl, back);
}
@ -96,7 +97,7 @@ public class SaSsoUtil {
* @param secretkey 秘钥
*/
public static void checkSecretkey(String secretkey) {
saSsoInterface.checkSecretkey(secretkey);
saSsoTemplate.checkSecretkey(secretkey);
}
/**
@ -106,7 +107,7 @@ public class SaSsoUtil {
* @return 构建完毕的URL
*/
public static String buildCheckTicketUrl(String ticket, String ssoLogoutCallUrl) {
return saSsoInterface.buildCheckTicketUrl(ticket, ssoLogoutCallUrl);
return saSsoTemplate.buildCheckTicketUrl(ticket, ssoLogoutCallUrl);
}
/**
@ -115,7 +116,7 @@ public class SaSsoUtil {
* @param sloCallbackUrl 单点注销时的回调URL
*/
public static void registerSloCallbackUrl(Object loginId, String sloCallbackUrl) {
saSsoInterface.registerSloCallbackUrl(loginId, sloCallbackUrl);
saSsoTemplate.registerSloCallbackUrl(loginId, sloCallbackUrl);
}
/**
@ -124,7 +125,7 @@ public class SaSsoUtil {
* @param fun 调用方法
*/
public static void forEachSloUrl(Object loginId, CallSloUrlFunction fun) {
saSsoInterface.forEachSloUrl(loginId, fun);
saSsoTemplate.forEachSloUrl(loginId, fun);
}
/**
@ -133,7 +134,7 @@ public class SaSsoUtil {
* @return 单点注销URL
*/
public static String buildSloUrl(Object loginId) {
return saSsoInterface.buildSloUrl(loginId);
return saSsoTemplate.buildSloUrl(loginId);
}
/**
@ -143,7 +144,7 @@ public class SaSsoUtil {
* @param fun 调用方法
*/
public static void singleLogout(String secretkey, Object loginId, CallSloUrlFunction fun) {
saSsoInterface.singleLogout(secretkey, loginId, fun);
saSsoTemplate.singleLogout(secretkey, loginId, fun);
}
}

View File

@ -17,7 +17,7 @@
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<!-- 定义sa-token版本号 -->
<sa-token-version>1.15.0.RELEASE</sa-token-version>
<sa-token-version>1.21.0</sa-token-version>
</properties>
<dependencies>

View File

@ -58,7 +58,7 @@ public class ClientAccController {
long userId = getUserIdByOpenid(openid);
// 登录并返回账号信息
StpUtil.setLoginId(userId);
StpUtil.login(userId);
return AjaxJson.getSuccessData(userId).set("openid", openid);
}

View File

@ -1,14 +1,13 @@
server:
port: 8002
# sa-token配置
sa-token:
# token名称 (同时也是cookie名称)
token-name: satoken-client
spring:
# 静态文件路径映射
resources:
static-locations: classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/
# static-locations: file:E:\work\project-yun\sa-token\sa-token-demo-oauth2\sa-token-demo-oauth2-client\src\main\resources\static\
# sa-token配置
sa-token:
# token名称 (同时也是cookie名称)
token-name: satoken-client

View File

@ -17,7 +17,7 @@
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<!-- 定义sa-token版本号 -->
<sa-token-version>1.15.0.RELEASE</sa-token-version>
<sa-token-version>1.21.0</sa-token-version>
</properties>
<dependencies>
@ -39,7 +39,7 @@
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-oauth2</artifactId>
<version>1.15.0-alpha</version>
<version>${sa-token-version}</version>
</dependency>
<!-- sa-token整合redis (使用jackson序列化方式) -->

View File

@ -19,7 +19,7 @@ public class ServerAccController {
public AjaxJson test(String username, String password) {
System.out.println("------------------ 成功进入请求 ------------------");
if("test".equals(username) && "test".equals(password)) {
StpUtil.setLoginId(10001);
StpUtil.login(10001);
return AjaxJson.getSuccess();
}
return AjaxJson.getError();

View File

@ -7,7 +7,8 @@ import org.springframework.stereotype.Component;
import cn.dev33.satoken.oauth2.SaOAuth2Manager;
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
import cn.dev33.satoken.oauth2.logic.SaOAuth2Interface;
import cn.dev33.satoken.oauth2.logic.SaOAuth2Template;
import cn.dev33.satoken.oauth2.logic.SaOAuth2Util;
/**
* 利用Spring完成自动装配
@ -24,7 +25,7 @@ public class SaOAuth2SpringAutowired {
* @return 配置对象
*/
@Bean
@ConfigurationProperties(prefix = "spring.sa-token.oauth2")
@ConfigurationProperties(prefix = "sa-token.oauth2")
public SaOAuth2Config getSaOAuth2Config() {
return new SaOAuth2Config();
}
@ -45,8 +46,8 @@ public class SaOAuth2SpringAutowired {
* @param saOAuth2Interface OAuth2接口Bean
*/
@Autowired(required = false)
public void setSaOAuth2Interface(SaOAuth2Interface saOAuth2Interface) {
SaOAuth2Manager.setInterface(saOAuth2Interface);
public void setSaOAuth2Interface(SaOAuth2Template saOAuth2Interface) {
SaOAuth2Util.saOAuth2Template = saOAuth2Interface;
}

View File

@ -5,14 +5,14 @@ import java.util.List;
import org.springframework.stereotype.Component;
import cn.dev33.satoken.oauth2.logic.SaOAuth2Interface;
import cn.dev33.satoken.oauth2.logic.SaOAuth2Template;
/**
* 使用oauth2.0 所必须的一些自定义实现
* @author kong
*/
@Component
public class SaOAuth2InterfaceImpl implements SaOAuth2Interface {
public class SaOAuth2TemplateImpl extends SaOAuth2Template {
/*

View File

@ -1,18 +1,17 @@
server:
port: 8001
# sa-token配置
sa-token:
# token名称 (同时也是cookie名称)
token-name: satoken-server
spring:
# 静态文件路径映射
resources:
static-locations: classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/
# static-locations: file:E:\work\project-yun\sa-token\sa-token-demo-oauth2\sa-token-demo-oauth2-server\src\main\resources\static\
# sa-token配置
sa-token:
# token名称 (同时也是cookie名称)
token-name: satoken-server
# redis配置
redis:
# Redis数据库索引默认为0

View File

@ -18,14 +18,14 @@ public class SsoClientController {
public String index() {
String str = "<h2>Sa-Token SSO-Client 应用端</h2>" +
"<p>当前会话是否登录:" + StpUtil.isLogin() + "</p>" +
"<p><a href=\"javascript:location.href='/ssoLogin?back=' + encodeURIComponent(location.href);\">登录</a> " +
"<a href=\"javascript:location.href='/ssoLogout?back=' + encodeURIComponent(location.href);\">注销</a></p>";
// "<a href='/ssoLogout' target='_blank'>注销</a></p>"; // 上面是[跳页面]方式这个是[RestAPI]方式 区别在于是否加了back参数
"<p><a href=\"javascript:location.href='/sso/login?back=' + encodeURIComponent(location.href);\">登录</a> " +
"<a href=\"javascript:location.href='/sso/logout?back=' + encodeURIComponent(location.href);\">注销</a></p>";
// "<a href='/sso/logout' target='_blank'>注销</a></p>"; // 上面是[跳页面]方式这个是[RestAPI]方式 区别在于是否加了back参数
return str;
}
// SSO-Client端处理所有SSO相关请求
@RequestMapping("/sso*")
@RequestMapping("/sso/*")
public Object ssoRequest() {
return SaSsoHandle.clientRequest();
}

View File

@ -13,7 +13,7 @@ sa-token:
# SSO-相关配置
sso:
# SSO-Server端 单点登录地址
auth-url: http://sa-sso-server.com:9000/ssoAuth
auth-url: http://sa-sso-server.com:9000/sso/auth
# 是否打开单点注销接口
is-slo: true

View File

@ -19,7 +19,7 @@ import cn.dev33.satoken.util.SaResult;
public class SsoServerController {
// SSO-Server端处理所有SSO相关请求
@RequestMapping("/sso*")
@RequestMapping("/sso/*")
public Object ssoRequest() {
return SaSsoHandle.serverRequest();
}

View File

@ -8,8 +8,8 @@ sa-token:
sso:
# Ticket有效期 (单位: 秒),默认五分钟
ticket-timeout: 300
# 所有允许的授权回调地址 (此处为了方便测试配置为*,线上生产环境一定要配置为详细地地址)
allow-url: http://sa-sso-client1.com:9001/ssoLogin, http://sa-sso-client2.com:9001/ssoLogin, http://sa-sso-client3.com:9001/ssoLogin
# 所有允许的授权回调地址
allow-url: http://sa-sso-client1.com:9001/sso/login, http://sa-sso-client2.com:9001/sso/login, http://sa-sso-client3.com:9001/sso/login
spring:
# Redis配置

View File

@ -20,7 +20,7 @@ $('.login-btn').click(function(){
// 开始登录
setTimeout(function() {
$.ajax({
url: "ssoDoLogin",
url: "sso/doLogin",
type: "post",
data: {
name: $('[name=name]').val(),

View File

@ -22,13 +22,13 @@ public class SsoClientController {
public String index() {
String str = "<h2>Sa-Token SSO-Client 应用端</h2>" +
"<p>当前会话是否登录:" + StpUtil.isLogin() + "</p>" +
"<p><a href=\"javascript:location.href='/ssoLogin?back=' + encodeURIComponent(location.href);\">登录</a>" +
" <a href='/ssoLogout' target='_blank'>注销</a></p>";
"<p><a href=\"javascript:location.href='/sso/login?back=' + encodeURIComponent(location.href);\">登录</a>" +
" <a href='/sso/logout' target='_blank'>注销</a></p>";
return str;
}
// SSO-Client端处理所有SSO相关请求
@RequestMapping("/sso*")
@RequestMapping("/sso/*")
public Object ssoRequest() {
return SaSsoHandle.clientRequest();
}

View File

@ -13,15 +13,15 @@ sa-token:
# SSO-相关配置
sso:
# SSO-Server端 单点登录地址
auth-url: http://sa-sso-server.com:9000/ssoAuth
auth-url: http://sa-sso-server.com:9000/sso/auth
# 使用Http请求校验ticket
is-http: true
# SSO-Server端 ticket校验地址
check-ticket-url: http://sa-sso-server.com:9000/ssoCheckTicket
check-ticket-url: http://sa-sso-server.com:9000/sso/checkTicket
# 打开单点注销功能
is-slo: true
# 单点注销地址
slo-url: http://sa-sso-server.com:9000/ssoLogout
slo-url: http://sa-sso-server.com:9000/sso/logout
# 接口调用秘钥
secretkey: kQwIOrYvnXmSDkwEiFngrKidMcdrgKor

View File

@ -21,7 +21,7 @@ import cn.dev33.satoken.util.SaResult;
public class SsoServerController {
// SSO-Server端处理所有SSO相关请求
@RequestMapping("/sso*")
@RequestMapping("/sso/*")
public Object ssoRequest() {
return SaSsoHandle.serverRequest();
}

View File

@ -9,7 +9,7 @@ sa-token:
# Ticket有效期 (单位: 秒),默认五分钟
ticket-timeout: 300
# 所有允许的授权回调地址
allow-url: http://sa-sso-client1.com:9001/ssoLogin, http://sa-sso-client2.com:9001/ssoLogin, http://sa-sso-client3.com:9001/ssoLogin
allow-url: http://sa-sso-client1.com:9001/sso/login, http://sa-sso-client2.com:9001/sso/login, http://sa-sso-client3.com:9001/sso/login
# 接口调用秘钥用于SSO模式三的单点注销功能
secretkey: kQwIOrYvnXmSDkwEiFngrKidMcdrgKor
# 使用Http请求校验ticket

View File

@ -20,7 +20,7 @@ $('.login-btn').click(function(){
// 开始登录
setTimeout(function() {
$.ajax({
url: "ssoDoLogin",
url: "sso/doLogin",
type: "post",
data: {
name: $('[name=name]').val(),

View File

@ -3,3 +3,4 @@
---
此份考卷将测评您对Sa-Token框架的掌握程度满分100链接[https://ks.wjx.top/vj/wFKPziD.aspx](https://ks.wjx.top/vj/wFKPziD.aspx)

View File

@ -64,7 +64,7 @@
public class SsoServerController {
// SSO-Server端处理所有SSO相关请求
@RequestMapping("/sso*")
@RequestMapping("/sso/*")
public Object ssoRequest() {
return SaSsoHandle.serverRequest();
}
@ -76,7 +76,7 @@ public class SsoServerController {
// 配置未登录时返回的View
.setNotLoginView(() -> {
String msg = "当前会话在SSO-Server端尚未登录请先访问"
+ "<a href='/ssoDoLogin?name=sa&pwd=123456' target='_blank'> doLogin登录 </a>"
+ "<a href='/sso/doLogin?name=sa&pwd=123456' target='_blank'> doLogin登录 </a>"
+ "进行登录之后,刷新页面开始授权";
return msg;
})
@ -184,13 +184,13 @@ public class SsoClientController {
public String index() {
String str = "<h2>Sa-Token SSO-Client 应用端</h2>" +
"<p>当前会话是否登录:" + StpUtil.isLogin() + "</p>" +
"<p><a href=\"javascript:location.href='/ssoLogin?back=' + encodeURIComponent(location.href);\">登录</a> " +
"<a href='/ssoLogout' target='_blank'>注销</a></p>";
"<p><a href=\"javascript:location.href='/sso/login?back=' + encodeURIComponent(location.href);\">登录</a> " +
"<a href='/sso/logout' target='_blank'>注销</a></p>";
return str;
}
// SSO-Client端处理所有SSO相关请求
@RequestMapping("/sso*")
@RequestMapping("/sso/*")
public Object ssoRequest() {
return SaSsoHandle.clientRequest();
}
@ -210,7 +210,7 @@ sa-token:
# SSO-相关配置
sso:
# SSO-Server端 单点登录地址
auth-url: http://sa-sso-server.com:9000/ssoAuth
auth-url: http://sa-sso-server.com:9000/sso/auth
# 是否打开单点注销接口
is-slo: true
@ -311,7 +311,7 @@ public class SaSsoClientApplication {
假设攻击者根据模仿我们的授权地址巧妙的构造一个URL
> [http://sa-sso-server.com:9000/ssoAuth?redirect=https://www.baidu.com/](http://sa-sso-server.com:9000/ssoAuth?redirect=https://www.baidu.com/)
> [http://sa-sso-server.com:9000/sso/auth?redirect=https://www.baidu.com/](http://sa-sso-server.com:9000/sso/auth?redirect=https://www.baidu.com/)
当不知情的小红被诱导访问了这个URL时它将被重定向至百度首页
@ -323,7 +323,7 @@ public class SaSsoClientApplication {
造成此漏洞的直接原因就是SSO-Server认证中心没有对 `redirect地址` 进行任何的限制,防范的方法也很简单,就是对`redirect参数`进行校验如果其不在指定的URL列表中时拒绝下放ticket
我们将其配置为一个具体的URL`allow-url=http://sa-sso-client1.com:9001/ssoLogin`,再次访问上述连接:
我们将其配置为一个具体的URL`allow-url=http://sa-sso-client1.com:9001/sso/login`,再次访问上述连接:
![sso-feifa-rf](https://oss.dev33.cn/sa-token/doc/sso/sso-feifa-rf.png 's-w-sh')
@ -335,7 +335,7 @@ public class SaSsoClientApplication {
| :-------- | :-------- | :-------- | :-------- |
| 配置为* | `*` | <font color="#F00" ></font> | **<font color="#F00" >禁止在生产环境下使用</font>** |
| 配置到域名 | `http://sa-sso-client1.com/*` | <font color="#F70" ></font> | <font color="#F70" >不建议在生产环境下使用</font> |
| 配置到详细地址| `http://sa-sso-client1.com:9001/ssoLogin` | <font color="#080" ></font> | <font color="#080" >可以在生产环境下使用</font> |
| 配置到详细地址| `http://sa-sso-client1.com:9001/sso/login` | <font color="#080" ></font> | <font color="#080" >可以在生产环境下使用</font> |
##### 5.4、疑问为什么不直接回传Token而是先回传ticket再用ticket去查询对应的账号id

View File

@ -66,7 +66,7 @@ sa-token:
# 使用Http请求校验ticket
is-http: true
# SSO-Server端 ticket校验地址
check-ticket-url: http://sa-sso-server.com:9000/ssoCheckTicket
check-ticket-url: http://sa-sso-server.com:9000/sso/checkTicket
```
##### 1.5 启动项目测试
@ -123,7 +123,7 @@ sa-token:
# 打开单点注销功能
is-slo: true
# 单点注销地址
slo-url: http://sa-sso-server.com:9000/ssoLogout
slo-url: http://sa-sso-server.com:9000/sso/logout
# 接口调用秘钥
secretkey: kQwIOrYvnXmSDkwEiFngrKidMcdrgKor
```

View File

@ -115,5 +115,5 @@ sa-token:
# SSO-相关配置
sso:
# SSO-Server端 单点登录地址
auth-url: http://sa-sso-server.com:9000/ssoAuth
auth-url: http://sa-sso-server.com:9000/sso/auth
```

View File

@ -20,7 +20,7 @@
<module>sa-token-alone-redis</module>
<module>sa-token-dao-redis</module>
<module>sa-token-dao-redis-jackson</module>
<!-- <module>sa-token-oauth2</module> -->
<!-- <module>sa-token-oauth2</module> -->
<module>sa-token-quick-login</module>
<module>sa-token-spring-aop</module>
<module>sa-token-temp-jwt</module>

View File

@ -11,9 +11,8 @@
</parent>
<packaging>jar</packaging>
<name>sa-token-dao-redis</name>
<name>sa-token-oauth2</name>
<artifactId>sa-token-oauth2</artifactId>
<version>1.15.0-alpha</version>
<description>sa-token realization oauth2.0</description>
<dependencies>

View File

@ -1,8 +1,6 @@
package cn.dev33.satoken.oauth2;
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
import cn.dev33.satoken.oauth2.logic.SaOAuth2Interface;
import cn.dev33.satoken.oauth2.logic.SaOAuth2InterfaceDefaultImpl;
/**
* sa-token oauth2 模块 总控类
@ -31,24 +29,4 @@ public class SaOAuth2Manager {
SaOAuth2Manager.config = config;
}
/**
* sa-token-oauth2 逻辑 Bean
*/
private static SaOAuth2Interface saOAuth2Interface;
public static SaOAuth2Interface getInterface() {
if (saOAuth2Interface == null) {
// 初始化默认值
synchronized (SaOAuth2Manager.class) {
if (saOAuth2Interface == null) {
setInterface(new SaOAuth2InterfaceDefaultImpl());
}
}
}
return saOAuth2Interface;
}
public static void setInterface(SaOAuth2Interface interfaceObj) {
SaOAuth2Manager.saOAuth2Interface = interfaceObj;
}
}

View File

@ -1,12 +0,0 @@
package cn.dev33.satoken.oauth2.logic;
/**
* SaOAuth2Interface 默认实现类 (只构建userinfo单个权限)
* @author kong
*
*/
public class SaOAuth2InterfaceDefaultImpl implements SaOAuth2Interface {
}

View File

@ -12,7 +12,6 @@ import cn.dev33.satoken.oauth2.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.model.CodeModel;
import cn.dev33.satoken.oauth2.model.RequestAuthModel;
import cn.dev33.satoken.oauth2.util.SaOAuth2Consts;
import cn.dev33.satoken.oauth2.util.SaOAuth2InsideUtil;
import cn.dev33.satoken.util.SaFoxUtil;
/**
@ -20,75 +19,75 @@ import cn.dev33.satoken.util.SaFoxUtil;
* @author kong
*
*/
public interface SaOAuth2Interface {
public class SaOAuth2Template {
// ------------------- 获取数据
/**
* [default] 返回此平台所有权限集合
* 返回此平台所有权限集合
* @return 此平台所有权限名称集合
*/
public default List<String> getAppScopeList() {
public List<String> getAppScopeList() {
return Arrays.asList("userinfo");
}
/**
* [default] 返回指定Client签约的所有Scope名称集合
* 返回指定Client签约的所有Scope名称集合
* @param clientId 应用id
* @return Scope集合
*/
public default List<String> getClientScopeList(String clientId) {
public List<String> getClientScopeList(String clientId) {
// 默认返回此APP的所有权限
return getAppScopeList();
}
/**
* [default] 获取指定 LoginId 对指定 Client 已经授权过的所有 Scope
* 获取指定 LoginId 对指定 Client 已经授权过的所有 Scope
* @param clientId 应用id
* @param loginId 账号id
* @return Scope集合
*/
public default List<String> getGrantScopeList(Object loginId, String clientId) {
public List<String> getGrantScopeList(Object loginId, String clientId) {
// 默认返回空集合
return Arrays.asList();
}
/**
* [default] 返回指定Client允许的回调域名, 多个用逗号隔开, *代表不限制
* 返回指定Client允许的回调域名, 多个用逗号隔开, *代表不限制
* @param clientId 应用id
* @return domain集合
*/
public default String getClientDomain(String clientId) {
public String getClientDomain(String clientId) {
return "*";
}
/**
* [default] 返回指定ClientId的ClientSecret
* 返回指定ClientId的ClientSecret
* @param clientId 应用id
* @return 此应用的秘钥
*/
public default String getClientSecret(String clientId) {
public String getClientSecret(String clientId) {
return null;
}
/**
* [default] 根据ClientId和LoginId返回openid
* 根据ClientId和LoginId返回openid
* @param clientId 应用id
* @param loginId 账号id
* @return 此账号在此Client下的openid
*/
public default String getOpenid(String clientId, Object loginId) {
public String getOpenid(String clientId, Object loginId) {
return null;
}
/**
* [default] 根据ClientId和openid返回LoginId
* 根据ClientId和openid返回LoginId
* @param clientId 应用id
* @param openid openid
* @return LoginId
*/
public default Object getLoginId(String clientId, String openid) {
public Object getLoginId(String clientId, String openid) {
return null;
}
@ -96,11 +95,11 @@ public interface SaOAuth2Interface {
// ------------------- 数据校验
/**
* [default] 检查一个 Client 是否签约了指定的Scope
* 检查一个 Client 是否签约了指定的Scope
* @param clientId 应用id
* @param scope 权限
*/
public default void checkContract(String clientId, String scope) {
public void checkContract(String clientId, String scope) {
List<String> clientScopeList = getClientScopeList(clientId);
List<String> scopelist = Arrays.asList(scope.split(","));
if(clientScopeList.containsAll(scopelist) == false) {
@ -109,26 +108,26 @@ public interface SaOAuth2Interface {
}
/**
* [default] 指定 loginId 是否对一个 Client 授权给了指定 Scope
* 指定 loginId 是否对一个 Client 授权给了指定 Scope
* @param loginId 账号id
* @param clientId 应用id
* @param scope 权限
* @return 是否已经授权
*/
public default boolean isGrant(Object loginId, String clientId, String scope) {
public boolean isGrant(Object loginId, String clientId, String scope) {
List<String> grantScopeList = getGrantScopeList(loginId, clientId);
List<String> scopeList = convertStringToList(scope);
return grantScopeList.containsAll(scopeList);
}
/**
* [default] 指定Client使用指定url作为回调地址是否合法
* 指定Client使用指定url作为回调地址是否合法
* @param clientId 应用id
* @param url 指定url
*/
public default void checkRightUrl(String clientId, String url) {
public void checkRightUrl(String clientId, String url) {
// 首先检测url格式
if(SaOAuth2InsideUtil.isUrl(url) == false) {
if(SaFoxUtil.isUrl(url) == false) {
throw new SaTokenException("url格式错误");
}
// ---- 检测
@ -157,13 +156,13 @@ public interface SaOAuth2Interface {
}
/**
* [default] 校验codeclientIdclientSecret 三者是否正确
* 校验codeclientIdclientSecret 三者是否正确
* @param code 授权码
* @param clientId 应用id
* @param clientSecret 秘钥
* @return CodeModel对象
*/
public default CodeModel checkCodeIdSecret(String code, String clientId, String clientSecret) {
public CodeModel checkCodeIdSecret(String code, String clientId, String clientSecret) {
// 获取授权码信息
CodeModel codeModel = getCode(code);
@ -188,13 +187,13 @@ public interface SaOAuth2Interface {
}
/**
* [default] 校验access_tokenclientIdclientSecret 三者是否正确
* 校验access_tokenclientIdclientSecret 三者是否正确
* @param accessToken access_token
* @param clientId 应用id
* @param clientSecret 秘钥
* @return AccessTokenModel对象
*/
public default AccessTokenModel checkTokenIdSecret(String accessToken, String clientId, String clientSecret) {
public AccessTokenModel checkTokenIdSecret(String accessToken, String clientId, String clientSecret) {
// 获取授权码信息
AccessTokenModel tokenModel = getAccessToken(accessToken);
@ -220,11 +219,11 @@ public interface SaOAuth2Interface {
// ---- 授权码
/**
* [default] 根据参数生成一个授权码并返回
* 根据参数生成一个授权码并返回
* @param authModel 请求授权参数Model
* @return 授权码Model
*/
public default CodeModel generateCode(RequestAuthModel authModel) {
public CodeModel generateCode(RequestAuthModel authModel) {
// 获取参数
String clientId = authModel.getClientId();
@ -276,28 +275,28 @@ public interface SaOAuth2Interface {
}
/**
* [default] 根据授权码获得授权码Model
* 根据授权码获得授权码Model
* @param code 授权码
* @return 授权码Model
*/
public default CodeModel getCode(String code) {
public CodeModel getCode(String code) {
return (CodeModel)SaManager.getSaTokenDao().getObject(getKeyCodeModel(code));
}
/**
* [default] 手动更改授权码对象信息
* 手动更改授权码对象信息
* @param code 授权码
* @param codeModel 授权码Model
*/
public default void updateCode(String code, CodeModel codeModel) {
public void updateCode(String code, CodeModel codeModel) {
SaManager.getSaTokenDao().updateObject(getKeyCodeModel(code), codeModel);
}
/**
* [default] 确认授权一个code
* 确认授权一个code
* @param code 授权码
*/
public default void confirmCode(String code) {
public void confirmCode(String code) {
// 获取codeModel
CodeModel codeModel = getCode(code);
// 如果该code码已经确认
@ -310,10 +309,10 @@ public interface SaOAuth2Interface {
}
/**
* [default] 删除一个授权码
* 删除一个授权码
* @param code 授权码
*/
public default void deleteCode(String code) {
public void deleteCode(String code) {
SaManager.getSaTokenDao().deleteObject(getKeyCodeModel(code));
}
@ -321,11 +320,11 @@ public interface SaOAuth2Interface {
// ------------------- access_token refresh_token 相关
/**
* [default] 根据授权码Model生成一个access_token
* 根据授权码Model生成一个access_token
* @param codeModel 授权码Model
* @return AccessTokenModel
*/
public default AccessTokenModel generateAccessToken(CodeModel codeModel) {
public AccessTokenModel generateAccessToken(CodeModel codeModel) {
// 先校验
if(codeModel == null) {
@ -347,20 +346,20 @@ public interface SaOAuth2Interface {
}
/**
* [default] 根据 access_token 获得其Model详细信息
* 根据 access_token 获得其Model详细信息
* @param accessToken access_token
* @return AccessTokenModel (授权码Model)
*/
public default AccessTokenModel getAccessToken(String accessToken) {
public AccessTokenModel getAccessToken(String accessToken) {
return (AccessTokenModel)SaManager.getSaTokenDao().getObject(getKeyAccessToken(accessToken));
}
/**
* [default] 根据 refresh_token 生成一个新的 access_token
* 根据 refresh_token 生成一个新的 access_token
* @param refreshToken refresh_token
* @return 新的 access_token
*/
public default AccessTokenModel refreshAccessToken(String refreshToken) {
public AccessTokenModel refreshAccessToken(String refreshToken) {
// 获取Model信息
CodeModel codeModel = getRefreshToken(refreshToken);
if(codeModel == null) {
@ -376,38 +375,38 @@ public interface SaOAuth2Interface {
}
/**
* [default] 根据 refresh_token 获得其Model详细信息 (授权码Model)
* 根据 refresh_token 获得其Model详细信息 (授权码Model)
* @param refreshToken refresh_token
* @return RefreshToken (授权码Model)
*/
public default CodeModel getRefreshToken(String refreshToken) {
public CodeModel getRefreshToken(String refreshToken) {
return (CodeModel)SaManager.getSaTokenDao().getObject(getKeyRefreshToken(refreshToken));
}
/**
* [default] 获取 access_token 的有效期
* 获取 access_token 的有效期
* @param accessToken access_token
* @return 有效期
*/
public default long getAccessTokenExpiresIn(String accessToken) {
public long getAccessTokenExpiresIn(String accessToken) {
return SaManager.getSaTokenDao().getObjectTimeout(getKeyAccessToken(accessToken));
}
/**
* [default] 获取 refresh_token 的有效期
* 获取 refresh_token 的有效期
* @param refreshToken refresh_token
* @return 有效期
*/
public default long getRefreshTokenExpiresIn(String refreshToken) {
public long getRefreshTokenExpiresIn(String refreshToken) {
return SaManager.getSaTokenDao().getObjectTimeout(getKeyRefreshToken(refreshToken));
}
/**
* [default] 获取 access_token 所代表的LoginId
* 获取 access_token 所代表的LoginId
* @param accessToken access_token
* @return LoginId
*/
public default Object getLoginIdByAccessToken(String accessToken) {
public Object getLoginIdByAccessToken(String accessToken) {
AccessTokenModel tokenModel = SaOAuth2Util.getAccessToken(accessToken);
if(tokenModel == null) {
throw new SaTokenException("无效access_token");
@ -419,50 +418,50 @@ public interface SaOAuth2Interface {
// ------------------- 自定义策略相关
/**
* [default] 将指定字符串按照逗号分隔符转化为字符串集合
* 将指定字符串按照逗号分隔符转化为字符串集合
* @param str 字符串
* @return 分割后的字符串集合
*/
public default List<String> convertStringToList(String str) {
public List<String> convertStringToList(String str) {
return Arrays.asList(str.split(","));
}
/**
* [default] 生成授权码
* 生成授权码
* @param clientId 应用id
* @param scope 权限
* @param loginId 账号id
* @return 授权码
*/
public default String createCode(String clientId, String scope, Object loginId) {
public String createCode(String clientId, String scope, Object loginId) {
return SaFoxUtil.getRandomString(60).toLowerCase();
}
/**
* [default] 生成AccessToken
* 生成AccessToken
* @param codeModel CodeModel对象
* @return AccessToken
*/
public default String createAccessToken(CodeModel codeModel) {
public String createAccessToken(CodeModel codeModel) {
return SaFoxUtil.getRandomString(60).toLowerCase();
}
/**
* [default] 生成RefreshToken
* 生成RefreshToken
* @param codeModel CodeModel对象
* @return RefreshToken
*/
public default String createRefreshToken(CodeModel codeModel) {
public String createRefreshToken(CodeModel codeModel) {
return SaFoxUtil.getRandomString(60).toLowerCase();
}
/**
* [default] 在url上拼接上kv参数并返回
* 在url上拼接上kv参数并返回
* @param url url
* @param parameStr 参数, 例如 id=1001
* @return 拼接后的url字符串
*/
public default String splicingParame(String url, String parameStr) {
public String splicingParame(String url, String parameStr) {
// 如果参数为空, 直接返回
if(parameStr == null || parameStr.length() == 0) {
return url;
@ -491,11 +490,11 @@ public interface SaOAuth2Interface {
}
/**
* [default] CodeModel 转换为 AccessTokenModel
* CodeModel 转换为 AccessTokenModel
* @param codeModel CodeModel对象
* @return AccessToken对象
*/
public default AccessTokenModel converCodeToAccessToken(CodeModel codeModel) {
public AccessTokenModel converCodeToAccessToken(CodeModel codeModel) {
if(codeModel == null) {
throw new SaTokenException("无效code");
}
@ -518,7 +517,7 @@ public interface SaOAuth2Interface {
* @param code 授权码
* @return key
*/
public default String getKeyCodeModel(String code) {
public String getKeyCodeModel(String code) {
return SaManager.getConfig().getTokenName() + ":oauth2:code:" + code;
}
@ -528,7 +527,7 @@ public interface SaOAuth2Interface {
* @param clientId 应用id
* @return key
*/
public default String getKeyClientLoginId(Object loginId, String clientId) {
public String getKeyClientLoginId(Object loginId, String clientId) {
return SaManager.getConfig().getTokenName() + ":oauth2:newest-code:" + clientId + ":" + loginId;
}
@ -537,7 +536,7 @@ public interface SaOAuth2Interface {
* @param refreshToken refreshToken
* @return key
*/
public default String getKeyRefreshToken(String refreshToken) {
public String getKeyRefreshToken(String refreshToken) {
return SaManager.getConfig().getTokenName() + ":oauth2:refresh-token:" + refreshToken;
}
@ -546,7 +545,7 @@ public interface SaOAuth2Interface {
* @param accessToken accessToken
* @return key
*/
public default String getKeyAccessToken(String accessToken) {
public String getKeyAccessToken(String accessToken) {
return SaManager.getConfig().getTokenName() + ":oauth2:access-token:" + accessToken;
}

View File

@ -2,7 +2,6 @@ package cn.dev33.satoken.oauth2.logic;
import java.util.List;
import cn.dev33.satoken.oauth2.SaOAuth2Manager;
import cn.dev33.satoken.oauth2.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.model.CodeModel;
import cn.dev33.satoken.oauth2.model.RequestAuthModel;
@ -14,6 +13,9 @@ import cn.dev33.satoken.oauth2.model.RequestAuthModel;
*/
public class SaOAuth2Util {
public static SaOAuth2Template saOAuth2Template = new SaOAuth2Template();
// ------------------- 获取数据
/**
@ -21,7 +23,7 @@ public class SaOAuth2Util {
* @return 此平台所有权限名称集合
*/
public static List<String> getAppScopeList() {
return SaOAuth2Manager.getInterface().getAppScopeList();
return saOAuth2Template.getAppScopeList();
}
/**
@ -30,7 +32,7 @@ public class SaOAuth2Util {
* @return Scope集合
*/
public static List<String> getClientScopeList(String clientId) {
return SaOAuth2Manager.getInterface().getClientScopeList(clientId);
return saOAuth2Template.getClientScopeList(clientId);
}
/**
@ -40,7 +42,7 @@ public class SaOAuth2Util {
* @return Scope集合
*/
public static List<String> getGrantScopeList(Object loginId, String clientId) {
return SaOAuth2Manager.getInterface().getGrantScopeList(loginId, clientId);
return saOAuth2Template.getGrantScopeList(loginId, clientId);
}
@ -54,7 +56,7 @@ public class SaOAuth2Util {
* @return 是否已经授权
*/
public static boolean isGrant(Object loginId, String clientId, String scope) {
return SaOAuth2Manager.getInterface().isGrant(loginId, clientId, scope);
return saOAuth2Template.isGrant(loginId, clientId, scope);
}
/**
@ -65,7 +67,7 @@ public class SaOAuth2Util {
* @return CodeModel对象
*/
public static CodeModel checkCodeIdSecret(String code, String clientId, String clientSecret) {
return SaOAuth2Manager.getInterface().checkCodeIdSecret(code, clientId, clientSecret);
return saOAuth2Template.checkCodeIdSecret(code, clientId, clientSecret);
}
/**
@ -76,7 +78,7 @@ public class SaOAuth2Util {
* @return AccessTokenModel对象
*/
public static AccessTokenModel checkTokenIdSecret(String accessToken, String clientId, String clientSecret) {
return SaOAuth2Manager.getInterface().checkTokenIdSecret(accessToken, clientId, clientSecret);
return saOAuth2Template.checkTokenIdSecret(accessToken, clientId, clientSecret);
}
@ -89,7 +91,7 @@ public class SaOAuth2Util {
* @return 授权码Model
*/
public static CodeModel generateCode(RequestAuthModel authModel) {
return SaOAuth2Manager.getInterface().generateCode(authModel);
return saOAuth2Template.generateCode(authModel);
}
/**
@ -98,7 +100,7 @@ public class SaOAuth2Util {
* @return 授权码Model
*/
public static CodeModel getCode(String code) {
return SaOAuth2Manager.getInterface().getCode(code);
return saOAuth2Template.getCode(code);
}
/**
@ -107,7 +109,7 @@ public class SaOAuth2Util {
* @param codeModel 授权码Model
*/
public static void updateCode(String code, CodeModel codeModel) {
SaOAuth2Manager.getInterface().updateCode(code, codeModel);
saOAuth2Template.updateCode(code, codeModel);
}
/**
@ -115,7 +117,7 @@ public class SaOAuth2Util {
* @param code 授权码
*/
public static void confirmCode(String code) {
SaOAuth2Manager.getInterface().confirmCode(code);
saOAuth2Template.confirmCode(code);
}
/**
@ -123,7 +125,7 @@ public class SaOAuth2Util {
* @param code 授权码
*/
public static void deleteCode(String code) {
SaOAuth2Manager.getInterface().deleteCode(code);
saOAuth2Template.deleteCode(code);
}
/**
@ -132,7 +134,7 @@ public class SaOAuth2Util {
* @return AccessTokenModel
*/
public static AccessTokenModel generateAccessToken(CodeModel codeModel) {
return SaOAuth2Manager.getInterface().generateAccessToken(codeModel);
return saOAuth2Template.generateAccessToken(codeModel);
}
/**
@ -141,7 +143,7 @@ public class SaOAuth2Util {
* @return AccessTokenModel (授权码Model)
*/
public static AccessTokenModel getAccessToken(String accessToken) {
return SaOAuth2Manager.getInterface().getAccessToken(accessToken);
return saOAuth2Template.getAccessToken(accessToken);
}
/**
@ -150,7 +152,7 @@ public class SaOAuth2Util {
* @return 新的 access_token
*/
public static AccessTokenModel refreshAccessToken(String refreshToken) {
return SaOAuth2Manager.getInterface().refreshAccessToken(refreshToken);
return saOAuth2Template.refreshAccessToken(refreshToken);
}
/**
@ -159,7 +161,7 @@ public class SaOAuth2Util {
* @return RefreshToken (授权码Model)
*/
public static CodeModel getRefreshToken(String refreshToken) {
return SaOAuth2Manager.getInterface().getRefreshToken(refreshToken);
return saOAuth2Template.getRefreshToken(refreshToken);
}
/**
@ -168,7 +170,7 @@ public class SaOAuth2Util {
* @return 有效期
*/
public static long getAccessTokenExpiresIn(String accessToken) {
return SaOAuth2Manager.getInterface().getAccessTokenExpiresIn(accessToken);
return saOAuth2Template.getAccessTokenExpiresIn(accessToken);
}
/**
@ -177,7 +179,7 @@ public class SaOAuth2Util {
* @return 有效期
*/
public static long getRefreshTokenExpiresIn(String refreshToken) {
return SaOAuth2Manager.getInterface().getRefreshTokenExpiresIn(refreshToken);
return saOAuth2Template.getRefreshTokenExpiresIn(refreshToken);
}
/**
@ -186,7 +188,7 @@ public class SaOAuth2Util {
* @return LoginId
*/
public static Object getLoginIdByAccessToken(String accessToken) {
return SaOAuth2Manager.getInterface().getLoginIdByAccessToken(accessToken);
return saOAuth2Template.getLoginIdByAccessToken(accessToken);
}

View File

@ -12,8 +12,4 @@ public class SaOAuth2Consts {
*/
public static final String UNLIMITED_DOMAIN = "*";
}

View File

@ -1,28 +0,0 @@
package cn.dev33.satoken.oauth2.util;
/**
* sa-token-oauth2 模块内部算法util
* @author kong
*
*/
public class SaOAuth2InsideUtil {
/**
* 验证URL的正则表达式
*/
static final String URL_REGEX = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
/**
* 使用正则表达式判断一个字符串是否为URL
* @param str 字符串
* @return 拼接后的url字符串
*/
public static boolean isUrl(String str) {
if(str == null) {
return false;
}
return str.toLowerCase().matches(URL_REGEX);
}
}