This commit is contained in:
六如
2024-10-04 21:48:38 +08:00
parent 00da3cc0a9
commit c08fec74c9
987 changed files with 24735 additions and 22137 deletions

View File

@@ -1,7 +1,7 @@
package com.gitee.sop.sopauth.auth;
/**
* @author tanghc
* @author 六如
*/
public class AccessToken {
/** 过期时间,毫秒 */

View File

@@ -1,7 +1,7 @@
package com.gitee.sop.sopauth.auth;
/**
* @author tanghc
* @author 六如
*/
public interface AppIdManager {
/**

View File

@@ -6,7 +6,7 @@ import javax.validation.constraints.NotBlank;
/**
* 获取accessToken参数对象
* @author tanghc
* @author 六如
*/
@Data
public class FetchTokenParam {

View File

@@ -4,7 +4,7 @@ import lombok.Data;
/**
* 使用app_auth_code换取app_auth_token返回结果
* @author tanghc
* @author 六如
*/
@Data
public class FetchTokenResult {

View File

@@ -3,7 +3,7 @@ package com.gitee.sop.sopauth.auth;
import lombok.Data;
/**
* @author tanghc
* @author 六如
*/
@Data
public class OAuth2Config {

View File

@@ -7,14 +7,14 @@ import javax.servlet.http.HttpServletRequest;
/**
* 认证服务,需要自己实现
* @author tanghc
* @author 六如
*
*/
public interface OAuth2Manager {
/**
* 添加 auth code
*
*
* @param authCode
* code值
* @param authUser
@@ -30,29 +30,29 @@ public interface OAuth2Manager {
*/
void addAccessToken(String accessToken, String refreshToken, OpenUser authUser);
/**
* 删除这个accessToken
* @param accessToken
*/
void removeAccessToken(String accessToken);
/**
* 删除这个refreshToken
* @param refreshToken
*/
void removeRefreshToken(String refreshToken);
/**
* 获取RefreshToken
* @param refreshToken
* @return 返回Token信息
*/
RefreshToken getRefreshToken(String refreshToken);
/**
* 验证auth code是否有效
*
*
* @param authCode
* @return 无效返回false
*/
@@ -60,7 +60,7 @@ public interface OAuth2Manager {
/**
* 根据auth code获取用户
*
*
* @param authCode
* @return 返回用户
*/
@@ -68,13 +68,13 @@ public interface OAuth2Manager {
/**
* 根据access token获取用户
*
*
* @param accessToken
* token值
* @return 返回用户
*/
OpenUser getUserByAccessToken(String accessToken);
/**
* 用户登录,需判断是否已经登录
* @param request
@@ -82,4 +82,4 @@ public interface OAuth2Manager {
* @throws LoginErrorException 登录失败异常
*/
OpenUser login(HttpServletRequest request) throws LoginErrorException;
}
}

View File

@@ -8,7 +8,7 @@ import javax.servlet.http.HttpServletResponse;
import java.net.URISyntaxException;
/**
* @author tanghc
* @author 六如
*/
public interface OAuth2Service {
@@ -20,7 +20,7 @@ public interface OAuth2Service {
3、然后判断用户是否登录了如果没有登录首先到登录页面登录
4、登录成功后生成相应的code即授权码然后重定向到客户端地址如http://localhost:8080/oauth2callback?code=6d250650831fea227749f49a5b49ccad在重定向到的地址中会带上code参数授权码接着客户端可以根据授权码去换取accessToken。
* </pre>
*
*
* @param request req
* @param response resp
* @param authConfig 配置
@@ -30,7 +30,7 @@ public interface OAuth2Service {
*/
OAuthResponse authorize(HttpServletRequest request, HttpServletResponse response, OAuth2Config authConfig)
throws URISyntaxException, OAuthSystemException;
/**
* 通过code获取accessToken.

View File

@@ -5,7 +5,7 @@ import java.io.Serializable;
/**
* 如果要使用oauth2功能自己的用户类需要实现这个对象
*
* @author tanghc
* @author 六如
*/
public interface OpenUser extends Serializable {
/**

View File

@@ -3,7 +3,7 @@ package com.gitee.sop.sopauth.auth;
import java.io.Serializable;
/**
* @author tanghc
* @author 六如
*/
public class RefreshToken implements Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -2,8 +2,8 @@ package com.gitee.sop.sopauth.auth;
/**
* 存放accessToken和refreshToken
*
* @author tanghc
*
* @author 六如
*
*/
public class TokenPair {

View File

@@ -1,7 +1,7 @@
package com.gitee.sop.sopauth.auth.exception;
/**
* @author tanghc
* @author 六如
*/
public class LoginErrorException extends Exception {
private static final long serialVersionUID = -6721499454527023339L;
@@ -22,4 +22,4 @@ public class LoginErrorException extends Exception {
super(cause);
}
}
}

View File

@@ -8,7 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author tanghc
* @author 六如
*/
@Service
@Slf4j

View File

@@ -22,7 +22,7 @@ import java.util.concurrent.TimeUnit;
* oauth2管理默认谷歌缓存实现跟redis实现只能用一个。
* 这里为了演示使用本地缓存正式环境请使用redis保存
* @see OAuth2ManagerRedis OAuth2ManagerRedis
* @author tanghc
* @author 六如
*
*/
@Service
@@ -46,7 +46,7 @@ public class OAuth2ManagerCache implements OAuth2Manager {
@Autowired
private UserInfoMapper userInfoMapper;
private static <T> LoadingCache<String, T> buildCache(int timeout) {
CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
@@ -121,7 +121,7 @@ public class OAuth2ManagerCache implements OAuth2Manager {
if(userInfo == null) {
throw new LoginErrorException("用户名密码不正确");
}
return userInfo;
}

View File

@@ -18,36 +18,36 @@ import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;
/**
*
*
* oauth2管理redis实现这个类跟OAuth2ManagerCache类只能用一个
* 如果要用这个类,
* 1、注释掉OAuth2ManagerCache的@Service。
* 2、在properties中配置redis
* 3、启用这个类的@Service
*
* @author tanghc
* @author 六如
*/
//@Service
public class OAuth2ManagerRedis implements OAuth2Manager {
private static String CODE_PREFIX = "com.gitee.sop.oauth2_code:";
private static String ACCESS_TOKEN_PREFIX = "com.gitee.sop.oauth2_access_token:";
private static String REFRESH_TOKEN_PREFIX = "com.gitee.sop.oauth2_refresh_token:";
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private UserInfoMapper userInfoMapper;
public static String getCodeKey(String code) {
return CODE_PREFIX + code;
}
public static String getAccessTokenKey(String accessToken) {
return ACCESS_TOKEN_PREFIX + accessToken;
}
public static String getRefreshTokenKey(String refreshToken) {
return REFRESH_TOKEN_PREFIX + refreshToken;
}
@@ -56,8 +56,8 @@ public class OAuth2ManagerRedis implements OAuth2Manager {
public void addAuthCode(String authCode, OpenUser authUser) {
long codeTimeoutSeconds = OAuth2Config.getInstance().getCodeTimeoutSeconds();
redisTemplate.opsForValue().set(getCodeKey(authCode),
JSON.toJSONString(authUser),
codeTimeoutSeconds,
JSON.toJSONString(authUser),
codeTimeoutSeconds,
TimeUnit.SECONDS);
}
@@ -108,11 +108,11 @@ public class OAuth2ManagerRedis implements OAuth2Manager {
return null;
}
JSONObject jsonObj = JSON.parseObject(json);
String userJson = jsonObj.getString("openUser");
UserInfo user = JSON.parseObject(userJson, UserInfo.class);
String accessToken = jsonObj.getString("accessToken");
return new RefreshToken(accessToken, user);
}

View File

@@ -35,7 +35,7 @@ import java.net.URISyntaxException;
/**
* oauth2服务端默认实现
*
* @author tanghc
* @author 六如
*/
@Service
@Slf4j

View File

@@ -7,7 +7,7 @@ import org.springframework.context.annotation.Configuration;
/**
* 使用支付宝开放平台功能
*
* @author tanghc
* @author 六如
*/
@Configuration
public class OpenServiceConfig extends AlipayServiceConfiguration {

View File

@@ -13,7 +13,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author tanghc
* @author 六如
*/
@Controller
@Slf4j

View File

@@ -25,7 +25,7 @@ import java.net.URISyntaxException;
/**
* 授权认证
*
* @author tanghc
* @author 六如
*/
@Controller
@RequestMapping("oauth2")

View File

@@ -10,7 +10,7 @@ import com.gitee.fastmybatis.annotation.Table;
* 表名isv_info
* 备注isv信息表
*
* @author tanghc
* @author 六如
*/
@Table(name = "isv_info",pk = @Pk(name = "id"))
@Data

View File

@@ -13,7 +13,7 @@ import java.util.Date;
* 表名user_info
* 备注:用户信息表
*
* @author tanghc
* @author 六如
*/
@Table(name = "user_info",pk = @Pk(name = "id"))
@Data

View File

@@ -5,7 +5,7 @@ import com.gitee.sop.sopauth.entity.IsvInfo;
/**
* @author tanghc
* @author 六如
*/
public interface IsvInfoMapper extends CrudMapper<IsvInfo, Long> {
}

View File

@@ -5,7 +5,7 @@ import com.gitee.sop.sopauth.entity.UserInfo;
/**
* @author tanghc
* @author 六如
*/
public interface UserInfoMapper extends CrudMapper<UserInfo, Long> {
}

View File

@@ -8,7 +8,7 @@ import junit.framework.TestCase;
import lombok.extern.slf4j.Slf4j;
/**
* @author tanghc
* @author 六如
*/
@Slf4j
public class RefreshTokenTest extends TestCase {