Skip to content

Commit

Permalink
Merge branch 'dev' into extract-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyd-c authored Aug 3, 2019
2 parents 3e8c475 + d9967b2 commit d4296d1
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 110 deletions.
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: java

sudo: false # faster builds

install: true

jdk:
- openjdk8

notifications:
email: false

script:
- export TZ=Asia/Shanghai
- mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
- mvn cobertura:cobertura -Dcobertura.report.format=xml -Dmaven.javadoc.skip.true

after_success:
- bash <(curl -s https://codecov.io/bash)
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
<a target="_blank" href="https://apidoc.gitee.com/yadong.zhang/JustAuth/">
<img src="https://img.shields.io/badge/Docs-1.9.5-orange.svg" ></img>
</a>
<a href="https://codecov.io/gh/zhangyd-c/JustAuth">
<img src="https://codecov.io/gh/zhangyd-c/JustAuth/branch/master/graph/badge.svg" />
</a>
<a href='https://gitee.com/yadong.zhang/JustAuth/stargazers'>
<img src='https://gitee.com/yadong.zhang/JustAuth/badge/star.svg?theme=white' alt='star'></img>
</a>
<a target="_blank" href='https://github.com/zhangyd-c/JustAuth'>
<img src="https://img.shields.io/github/stars/zhangyd-c/JustAuth.svg?style=social" alt="github star"></img>
</a>
</p>

<center>
Expand Down
26 changes: 20 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<junit-version>4.11</junit-version>
<fastjson-version>1.2.58</fastjson-version>
<alipay-sdk-version>3.7.4.ALL</alipay-sdk-version>
<slf4j-version>1.7.25</slf4j-version>
<jacoco-version>0.8.2</jacoco-version>
</properties>

<dependencies>
Expand Down Expand Up @@ -95,11 +95,6 @@
<version>${alipay-sdk-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j-version}</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -190,6 +185,25 @@
<check/>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/me/zhyd/oauth/cache/AuthCacheConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.zhyd.oauth.cache;

/**
* AuthCache配置类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @date 2019/8/1 17:15
* @since 1.8
*/
public class AuthCacheConfig {

/**
* 默认缓存过期时间:3分钟
* 鉴于授权过程中,根据个人的操作习惯,或者授权平台的不同(google等),每个授权流程的耗时也有差异,不过单个授权流程一般不会太长
* 本缓存工具默认的过期时间设置为3分钟,即程序默认认为3分钟内的授权有效,超过3分钟则默认失效,失效后删除
*/
public static long timeout = 3 * 60 * 1000;

/**
* 是否开启定时{@link AuthDefaultCache#pruneCache()}的任务
*/
public static boolean schedulePrune = true;
}
12 changes: 4 additions & 8 deletions src/main/java/me/zhyd/oauth/cache/AuthDefaultCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
*/
public class AuthDefaultCache implements AuthCache {

/**
* 默认缓存过期时间:3分钟
* 鉴于授权过程中,根据个人的操作习惯,或者授权平台的不同(google等),每个授权流程的耗时也有差异,不过单个授权流程一般不会太长
* 本缓存工具默认的过期时间设置为3分钟,即程序默认认为3分钟内的授权有效,超过3分钟则默认失效,失效后删除
*/
private static final long DEF_TIMEOUT = 3 * 60 * 1000;
/**
* state cache
*/
Expand All @@ -33,7 +27,9 @@ public class AuthDefaultCache implements AuthCache {
private final Lock readLock = cacheLock.readLock();

public AuthDefaultCache() {
this.schedulePrune(DEF_TIMEOUT);
if (AuthCacheConfig.schedulePrune) {
this.schedulePrune(AuthCacheConfig.timeout);
}
}

/**
Expand All @@ -44,7 +40,7 @@ public AuthDefaultCache() {
*/
@Override
public void set(String key, String value) {
set(key, value, DEF_TIMEOUT);
set(key, value, AuthCacheConfig.timeout);
}

/**
Expand Down
156 changes: 156 additions & 0 deletions src/main/java/me/zhyd/oauth/log/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package me.zhyd.oauth.log;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.io.PrintStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* 针对JustAuth提供的轻量级的日志打印工具
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @date 2019/8/1 17:14
* @see Log#debug(String)
* @see Log#debug(String, Throwable)
* @see Log#warn(String)
* @see Log#warn(String, Throwable)
* @see Log#error(String)
* @see Log#error(String, Throwable)
* @since 1.8
*/
public class Log {

public static void debug(String msg) {
debug(msg, null);
}

public static void warn(String msg) {
warn(msg, null);
}

public static void error(String msg) {
error(msg, null);
}

public static void debug(String msg, Throwable t) {
print(Level.DEBUG, msg, t, System.out);
}

public static void warn(String msg, Throwable t) {
print(Level.WARN, msg, t, System.out);
}

public static void error(String msg, Throwable t) {
print(Level.ERROR, msg, t, System.err);
}

/**
* 打印日志内容,格式:2019-08-02 20:44:07 main me.zhyd.oauth.log.Log(debug:39) [DEBUG] - xxxx
*
* @param level 日志级别
* @param msg 日志内容
* @param t 异常信息
* @param ps 实际执行打印的PrintStream
*/
private static void print(Level level, String msg, Throwable t, PrintStream ps) {
if (Config.enable) {
if (level.getLevelNum() >= Config.level.getLevelNum()) {
ps.println(String.format("%s %s %s [%s] - %s", getDate(), Thread.currentThread().getName(), getCaller(), level, msg));
writeThrowable(t, ps);
ps.flush();
}
}
}

/**
* 获取调用方的信息
*
* @return 返回调用方的信息,格式:class(method:lineNumber)
*/
private static String getCaller() {
int offset = 2;
StackTraceElement[] stackTraceArr = (new Throwable()).getStackTrace();
StackTraceElement stackTrace = null;
if (offset >= stackTraceArr.length) {
offset = offset - 1;
}
stackTrace = stackTraceArr[offset];
return stackTrace.getClassName() +
"(" +
stackTrace.getMethodName() +
':' +
stackTrace.getLineNumber() +
")";
}

/**
* 获取格式化后的日期
*
* @return string
*/
private static String getDate() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}

/**
* 打印异常信息
*
* @param t 异常
* @param targetStream 实际执行打印的PrintStream
*/
private static void writeThrowable(Throwable t, PrintStream targetStream) {
if (t != null) {
t.printStackTrace(targetStream);
}
}

/**
* 日志级别
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @date 2019/8/2 19:49
* @since 1.8
*/
@Getter
@AllArgsConstructor
public enum Level {
/**
* DEBUG: 普通级别
*/
DEBUG(10),
/**
* WARN: 警告级别
*/
WARN(30),
/**
* ERROR: 异常级别
*/
ERROR(40);

private int levelNum;
}

/**
* 日志配置
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @date 2019/8/1 17:14
* @since 1.8
*/
static class Config {

/**
* 需要打印的日志级别
*/
static Level level = Level.DEBUG;
/**
* 是否启用日志打印功能,默认启用
*/
static boolean enable = true;
}
}
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.log.Log;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken;
Expand All @@ -25,7 +26,6 @@
* @author yangkai.shen (https://xkcoding.com)
* @since 1.0.0
*/
@Slf4j
public abstract class AuthDefaultRequest implements AuthRequest {
protected AuthConfig config;
protected AuthSource source;
Expand Down Expand Up @@ -82,7 +82,7 @@ public AuthResponse login(AuthCallback authCallback) {
AuthUser user = this.getUserInfo(authToken);
return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(user).build();
} catch (Exception e) {
log.error("Failed to login with oauth authorization.", e);
Log.error("Failed to login with oauth authorization.", e);
return this.responseError(e);
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/me/zhyd/oauth/request/AuthMiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.log.Log;
import me.zhyd.oauth.model.*;
import me.zhyd.oauth.utils.UrlBuilder;

import java.text.MessageFormat;
Expand All @@ -25,7 +22,6 @@
* @author yangkai.shen (https://xkcoding.com)
* @since 1.5.0
*/
@Slf4j
public class AuthMiRequest extends AuthDefaultRequest {
private static final String PREFIX = "&&&START&&&";

Expand Down Expand Up @@ -96,7 +92,7 @@ protected AuthUser getUserInfo(AuthToken authToken) {
JSONObject emailPhone = userEmailPhone.getJSONObject("data");
authUser.setEmail(emailPhone.getString("email"));
} else {
log.warn("小米开发平台暂时不对外开放用户手机及邮箱信息的获取");
Log.warn("小米开发平台暂时不对外开放用户手机及邮箱信息的获取");
}

return authUser;
Expand Down
Loading

0 comments on commit d4296d1

Please sign in to comment.