mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
5.0
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package com.gitee.sop.sopauth.auth;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
public class AccessToken {
|
||||
/** 过期时间,毫秒 */
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package com.gitee.sop.sopauth.auth;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
public interface AppIdManager {
|
||||
/**
|
||||
|
@@ -6,7 +6,7 @@ import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* 获取accessToken参数对象
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
@Data
|
||||
public class FetchTokenParam {
|
||||
|
@@ -4,7 +4,7 @@ import lombok.Data;
|
||||
|
||||
/**
|
||||
* 使用app_auth_code换取app_auth_token返回结果
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
@Data
|
||||
public class FetchTokenResult {
|
||||
|
@@ -3,7 +3,7 @@ package com.gitee.sop.sopauth.auth;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
@Data
|
||||
public class OAuth2Config {
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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.
|
||||
|
@@ -5,7 +5,7 @@ import java.io.Serializable;
|
||||
/**
|
||||
* 如果要使用oauth2功能,自己的用户类需要实现这个对象
|
||||
*
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
public interface OpenUser extends Serializable {
|
||||
/**
|
||||
|
@@ -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;
|
||||
|
@@ -2,8 +2,8 @@ package com.gitee.sop.sopauth.auth;
|
||||
|
||||
/**
|
||||
* 存放accessToken和refreshToken
|
||||
*
|
||||
* @author tanghc
|
||||
*
|
||||
* @author 六如
|
||||
*
|
||||
*/
|
||||
public class TokenPair {
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -35,7 +35,7 @@ import java.net.URISyntaxException;
|
||||
/**
|
||||
* oauth2服务端默认实现
|
||||
*
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
|
@@ -7,7 +7,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
/**
|
||||
* 使用支付宝开放平台功能
|
||||
*
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
@Configuration
|
||||
public class OpenServiceConfig extends AlipayServiceConfiguration {
|
||||
|
@@ -13,7 +13,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
@Controller
|
||||
@Slf4j
|
||||
|
@@ -25,7 +25,7 @@ import java.net.URISyntaxException;
|
||||
/**
|
||||
* 授权认证
|
||||
*
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("oauth2")
|
||||
|
@@ -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
|
||||
|
@@ -13,7 +13,7 @@ import java.util.Date;
|
||||
* 表名:user_info
|
||||
* 备注:用户信息表
|
||||
*
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
@Table(name = "user_info",pk = @Pk(name = "id"))
|
||||
@Data
|
||||
|
@@ -5,7 +5,7 @@ import com.gitee.sop.sopauth.entity.IsvInfo;
|
||||
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
public interface IsvInfoMapper extends CrudMapper<IsvInfo, Long> {
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@ import com.gitee.sop.sopauth.entity.UserInfo;
|
||||
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
public interface UserInfoMapper extends CrudMapper<UserInfo, Long> {
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ import junit.framework.TestCase;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
* @author 六如
|
||||
*/
|
||||
@Slf4j
|
||||
public class RefreshTokenTest extends TestCase {
|
||||
|
Reference in New Issue
Block a user