【auth】从新整理session超时情况的校验和逻辑

pull/3/head
fengshuonan 2021-01-02 22:22:21 +08:00
parent 25f5d010da
commit 05f00b0558
7 changed files with 64 additions and 38 deletions

View File

@ -49,7 +49,9 @@ public interface AuthServiceApi {
void logoutWithToken(String token);
/**
* token
* jwt tokenjwt
* <p>
* jwt便tokentokentoken
*
* @param token token
* @throws AuthException token
@ -58,16 +60,6 @@ public interface AuthServiceApi {
*/
void validateToken(String token) throws AuthException;
/**
* token
*
* @param token token
* @return true-tokenfalse-token
* @author fengshuonan
* @date 2020/10/19 14:16
*/
boolean getTokenFlag(String token);
/**
* token
*

View File

@ -73,4 +73,14 @@ public interface SessionManagerApi {
*/
void refreshSession(String token);
/**
* cookie
* <p>
*
*
* @author fengshuonan
* @date 2021/1/2 20:25
*/
void destroySessionCookie();
}

View File

@ -1,7 +1,5 @@
package cn.stylefeng.roses.kernel.auth.api.cookie;
import cn.hutool.core.convert.Convert;
import javax.servlet.http.Cookie;
/**
@ -27,7 +25,7 @@ public abstract class SessionCookieCreator {
*/
public Cookie createCookie(String cookieName, String cookieValue, Integer sessionExpiredSeconds) {
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setMaxAge(Convert.toInt(sessionExpiredSeconds));
cookie.setMaxAge(sessionExpiredSeconds);
this.expandCookieProp(cookie);
return cookie;
}

View File

@ -15,44 +15,44 @@ import lombok.Getter;
public enum AuthExceptionEnum implements AbstractExceptionEnum {
/**
*
* token
*/
AUTH_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "01", "认证失败,请检查您的登录是否过期"),
AUTH_EXPIRED_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "01", "当前登录会话过期,请重新登录"),
/**
* jwt tokentokentokenjwt
*/
TOKEN_PARSE_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "02", "TOKEN解析失败请传递正常TOKEN"),
/**
*
*/
PARAM_EMPTY(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "02", "登陆失败,账号或密码参数为空"),
PARAM_EMPTY(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "03", "登陆失败,账号或密码参数为空"),
/**
*
*/
USERNAME_PASSWORD_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "03", "账号或密码错误"),
USERNAME_PASSWORD_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "04", "账号或密码错误"),
/**
* StrUtil.format()
*/
USER_STATUS_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "04", "当前用户被{},请检查用户状态是否正常"),
USER_STATUS_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "05", "当前用户被{},请检查用户状态是否正常"),
/**
*
*/
ACCOUNT_IS_BLANK(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "05", "登陆失败,账号参数为空"),
ACCOUNT_IS_BLANK(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "06", "登陆失败,账号参数为空"),
/**
* token
*/
TOKEN_GET_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "06", "获取token失败请检查header和param中是否传递了用户token"),
TOKEN_GET_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "07", "获取token失败请检查header和param中是否传递了用户token"),
/**
*
*/
RESOURCE_DEFINITION_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "07", "获取资源为空请检查当前请求url是否存在对应的ResourceDefinition"),
/**
* token
*/
TOKEN_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "08", "获取不到token对应的用户信息请检查登录是否过期"),
RESOURCE_DEFINITION_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "08", "获取资源为空请检查当前请求url是否存在对应的ResourceDefinition"),
/**
*

View File

@ -14,6 +14,7 @@ import cn.stylefeng.roses.kernel.auth.api.pojo.auth.LoginResponse;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.jwt.api.context.JwtContext;
import cn.stylefeng.roses.kernel.jwt.api.exception.JwtException;
import cn.stylefeng.roses.kernel.jwt.api.exception.enums.JwtExceptionEnum;
import cn.stylefeng.roses.kernel.jwt.api.pojo.payload.DefaultJwtPayload;
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import cn.stylefeng.roses.kernel.system.UserServiceApi;
@ -24,6 +25,9 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import static cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum.AUTH_EXPIRED_ERROR;
import static cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum.TOKEN_PARSE_ERROR;
/**
*
*
@ -77,15 +81,26 @@ public class AuthServiceImpl implements AuthServiceApi {
@Override
public void validateToken(String token) throws AuthException {
try {
// 1. 先校验jwt token本身是否有问题
JwtContext.me().validateTokenWithException(token);
} catch (JwtException e) {
throw new AuthException(e.getErrorCode(), e.getUserTip());
}
}
@Override
public boolean getTokenFlag(String token) {
return JwtContext.me().validateToken(token);
// 2. 判断session里是否有这个token
LoginUser session = sessionManagerApi.getSession(token);
if (session == null) {
throw new AuthException(AUTH_EXPIRED_ERROR);
}
} catch (JwtException jwtException) {
// jwt token本身过期的话返回 AUTH_EXPIRED_ERROR
if (JwtExceptionEnum.JWT_EXPIRED_ERROR.getErrorCode().equals(jwtException.getErrorCode())) {
throw new AuthException(AUTH_EXPIRED_ERROR);
} else {
// 其他情况为返回jwt解析错误
throw new AuthException(TOKEN_PARSE_ERROR);
}
} catch (Exception jwtSelfException) {
// 其他jwt解析错误
throw new AuthException(TOKEN_PARSE_ERROR);
}
}
@Override
@ -114,7 +129,7 @@ public class AuthServiceImpl implements AuthServiceApi {
// 6. 如果会话信息为空,则判定此次校验失败
if (loginUser == null) {
throw new AuthException(AuthExceptionEnum.AUTH_ERROR);
throw new AuthException(AUTH_EXPIRED_ERROR);
}
}

View File

@ -11,8 +11,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Set;
import static cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum.AUTH_EXPIRED_ERROR;
import static cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum.PERMISSION_RES_VALIDATE_ERROR;
import static cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum.TOKEN_ERROR;
/**
* service
@ -37,7 +37,7 @@ public class PermissionServiceImpl implements PermissionServiceApi {
// 2. 获取token对应的用户信息
LoginUser session = sessionManagerApi.getSession(token);
if (session == null) {
throw new AuthException(TOKEN_ERROR);
throw new AuthException(AUTH_EXPIRED_ERROR);
}
// 3. 验证用户有没有当前url的权限

View File

@ -76,7 +76,7 @@ public class DefaultSessionManager implements SessionManagerApi {
// 如果开启了cookie存储会话信息则需要给HttpServletResponse添加一个cookie
if (AuthConfigExpander.getSessionAddToCookie()) {
String sessionCookieName = AuthConfigExpander.getSessionCookieName();
Cookie cookie = sessionCookieCreator.createCookie(sessionCookieName, token, Convert.toInt(sessionExpiredSeconds));
Cookie cookie = sessionCookieCreator.createCookie(sessionCookieName, token, Convert.toInt(AuthConfigExpander.getAuthJwtTimeoutSeconds()));
HttpServletResponse response = HttpServletUtil.getResponse();
response.addCookie(cookie);
}
@ -155,4 +155,15 @@ public class DefaultSessionManager implements SessionManagerApi {
}
}
@Override
public void destroySessionCookie() {
// 如果开启了cookie存储会话信息则需要给HttpServletResponse添加一个cookie
if (AuthConfigExpander.getSessionAddToCookie()) {
String sessionCookieName = AuthConfigExpander.getSessionCookieName();
Cookie cookie = sessionCookieCreator.createCookie(sessionCookieName, null, 0);
HttpServletResponse response = HttpServletUtil.getResponse();
response.addCookie(cookie);
}
}
}