diff --git a/kernel-d-auth/auth-api/pom.xml b/kernel-d-auth/auth-api/pom.xml index ecee4e6ec..0b261594f 100644 --- a/kernel-d-auth/auth-api/pom.xml +++ b/kernel-d-auth/auth-api/pom.xml @@ -17,6 +17,13 @@ + + + com.fasterxml.jackson.core + jackson-annotations + provided + + @@ -38,6 +45,20 @@ scanner-api ${roses.version} + + + + + javax.servlet + javax.servlet-api + provided + + + org.springframework + spring-web + provided + + diff --git a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/CommonLoginUserUtil.java b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/CommonLoginUserUtil.java new file mode 100644 index 000000000..cd4d69ee2 --- /dev/null +++ b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/CommonLoginUserUtil.java @@ -0,0 +1,63 @@ +package cn.stylefeng.roses.kernel.auth.api.loginuser; + +import cn.hutool.core.util.StrUtil; +import cn.stylefeng.roses.kernel.auth.api.exception.AuthException; +import cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum; +import cn.stylefeng.roses.kernel.auth.api.expander.AuthConfigExpander; +import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; + +/** + * 获取当前登录用户的相关方法 + * + * @author fengshuonan + * @date 2021/9/28 17:46 + */ +public class CommonLoginUserUtil { + + /** + * 获取当前登录用户Token + * + * @author fengshuonan + * @date 2021/9/28 17:46 + */ + public static String getToken() { + + // 获取当前http请求 + HttpServletRequest request = HttpServletUtil.getRequest(); + + // 1. 优先从param参数中获取token + String parameterToken = request.getParameter(AuthConfigExpander.getAuthTokenParamName()); + + // 不为空则直接返回param的token + if (StrUtil.isNotBlank(parameterToken)) { + return parameterToken; + } + + // 2. 从header中获取token + String authToken = request.getHeader(AuthConfigExpander.getAuthTokenHeaderName()); + if (StrUtil.isNotBlank(authToken)) { + return authToken; + } + + // 3. 从cookie中获取token + String sessionCookieName = AuthConfigExpander.getSessionCookieName(); + Cookie[] cookies = request.getCookies(); + if (cookies != null && cookies.length > 0) { + for (Cookie cookie : cookies) { + + // 如果cookie有对应的值,并且不为空 + if (sessionCookieName.equals(cookie.getName()) + && StrUtil.isNotBlank(cookie.getValue())) { + return cookie.getValue(); + } + } + } + + // 获取不到token,直接告诉用户 + throw new AuthException(AuthExceptionEnum.TOKEN_GET_ERROR); + } + +} diff --git a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/api/LoginUserRemoteApi.java b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/api/LoginUserRemoteApi.java new file mode 100644 index 000000000..d4bb71aac --- /dev/null +++ b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/api/LoginUserRemoteApi.java @@ -0,0 +1,71 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.auth.api.loginuser.api; + +import cn.stylefeng.roses.kernel.auth.api.loginuser.pojo.LoginUserRequest; +import cn.stylefeng.roses.kernel.auth.api.loginuser.pojo.SessionValidateResponse; +import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * 获取当前登录用户的远程调用方法,供微服务使用 + * + * @author fengshuonan + * @date 2021/9/29 10:08 + */ +public interface LoginUserRemoteApi { + + /** + * 通过token获取登录的用户 + * + * @author fengshuonan + * @date 2021/9/29 10:08 + */ + @RequestMapping(value = "/loginUserRemote/getLoginUserByToken", method = RequestMethod.POST) + LoginUser getLoginUserByToken(@RequestBody LoginUserRequest loginUserRequest); + + /** + * 判断token是否存在会话 + * + * @author fengshuonan + * @date 2021/9/29 11:39 + */ + @RequestMapping(value = "/loginUserRemote/haveSession", method = RequestMethod.GET) + SessionValidateResponse haveSession(@RequestParam("token") String token); + + /** + * 通过loginUser获取刷新后的LoginUser对象 + * + * @author fengshuonan + * @date 2021/9/29 11:39 + */ + @RequestMapping(value = "/loginUserRemote/getEffectiveLoginUser", method = RequestMethod.POST) + LoginUser getEffectiveLoginUser(@RequestBody LoginUser loginUser); + +} + diff --git a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/pojo/LoginUserRequest.java b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/pojo/LoginUserRequest.java new file mode 100644 index 000000000..95286ff14 --- /dev/null +++ b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/pojo/LoginUserRequest.java @@ -0,0 +1,23 @@ +package cn.stylefeng.roses.kernel.auth.api.loginuser.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 获取登录用户信息的请求 + * + * @author fengshuonan + * @date 2021/9/29 11:25 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class LoginUserRequest { + + /** + * 当前登录用户的token + */ + private String token; + +} diff --git a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/pojo/SessionValidateResponse.java b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/pojo/SessionValidateResponse.java new file mode 100644 index 000000000..979c1811d --- /dev/null +++ b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/loginuser/pojo/SessionValidateResponse.java @@ -0,0 +1,23 @@ +package cn.stylefeng.roses.kernel.auth.api.loginuser.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * Session校验 + * + * @author fengshuonan + * @date 2021/9/29 11:37 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class SessionValidateResponse { + + /** + * 校验结果 + */ + private Boolean validateResult; + +} diff --git a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/pojo/login/LoginUser.java b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/pojo/login/LoginUser.java index de9a1af60..ba18d2fdf 100644 --- a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/pojo/login/LoginUser.java +++ b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/pojo/login/LoginUser.java @@ -32,6 +32,7 @@ import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo; import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleUserInfo; import cn.stylefeng.roses.kernel.rule.constants.RuleConstants; import cn.stylefeng.roses.kernel.scanner.api.annotation.field.ChineseDescription; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.io.Serializable; @@ -124,6 +125,7 @@ public class LoginUser implements Serializable { * 登录的时间 */ @ChineseDescription("登录的时间") + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date loginTime; /** diff --git a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/pojo/login/basic/SimpleUserInfo.java b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/pojo/login/basic/SimpleUserInfo.java index f0fc05526..1ffdf905a 100644 --- a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/pojo/login/basic/SimpleUserInfo.java +++ b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/pojo/login/basic/SimpleUserInfo.java @@ -24,6 +24,7 @@ */ package cn.stylefeng.roses.kernel.auth.api.pojo.login.basic; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.util.Date; @@ -55,6 +56,7 @@ public class SimpleUserInfo { /** * 生日 */ + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date birthday; /** diff --git a/kernel-d-auth/auth-sdk/src/main/java/cn/stylefeng/roses/kernel/auth/auth/LoginUserImpl.java b/kernel-d-auth/auth-sdk/src/main/java/cn/stylefeng/roses/kernel/auth/auth/LoginUserImpl.java index a55af5608..69060c941 100644 --- a/kernel-d-auth/auth-sdk/src/main/java/cn/stylefeng/roses/kernel/auth/auth/LoginUserImpl.java +++ b/kernel-d-auth/auth-sdk/src/main/java/cn/stylefeng/roses/kernel/auth/auth/LoginUserImpl.java @@ -25,24 +25,20 @@ package cn.stylefeng.roses.kernel.auth.auth; import cn.hutool.core.util.ObjectUtil; -import cn.hutool.core.util.StrUtil; import cn.stylefeng.roses.kernel.auth.api.LoginUserApi; import cn.stylefeng.roses.kernel.auth.api.SessionManagerApi; import cn.stylefeng.roses.kernel.auth.api.context.LoginUserHolder; import cn.stylefeng.roses.kernel.auth.api.exception.AuthException; import cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum; -import cn.stylefeng.roses.kernel.auth.api.expander.AuthConfigExpander; +import cn.stylefeng.roses.kernel.auth.api.loginuser.CommonLoginUserUtil; import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser; import cn.stylefeng.roses.kernel.dsctn.api.constants.DatasourceContainerConstants; import cn.stylefeng.roses.kernel.dsctn.api.context.CurrentDataSourceContext; import cn.stylefeng.roses.kernel.rule.constants.RuleConstants; -import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil; import cn.stylefeng.roses.kernel.system.api.UserServiceApi; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; /** * 当前登陆用户的接口实现 @@ -61,40 +57,7 @@ public class LoginUserImpl implements LoginUserApi { @Override public String getToken() { - - // 获取当前http请求 - HttpServletRequest request = HttpServletUtil.getRequest(); - - // 1. 优先从param参数中获取token - String parameterToken = request.getParameter(AuthConfigExpander.getAuthTokenParamName()); - - // 不为空则直接返回param的token - if (StrUtil.isNotBlank(parameterToken)) { - return parameterToken; - } - - // 2. 从header中获取token - String authToken = request.getHeader(AuthConfigExpander.getAuthTokenHeaderName()); - if (StrUtil.isNotBlank(authToken)) { - return authToken; - } - - // 3. 从cookie中获取token - String sessionCookieName = AuthConfigExpander.getSessionCookieName(); - Cookie[] cookies = request.getCookies(); - if (cookies != null && cookies.length > 0) { - for (Cookie cookie : cookies) { - - // 如果cookie有对应的值,并且不为空 - if (sessionCookieName.equals(cookie.getName()) - && StrUtil.isNotBlank(cookie.getValue())) { - return cookie.getValue(); - } - } - } - - // 获取不到token,直接告诉用户 - throw new AuthException(AuthExceptionEnum.TOKEN_GET_ERROR); + return CommonLoginUserUtil.getToken(); } @Override diff --git a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/exception/enums/user/SysUserExceptionEnum.java b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/exception/enums/user/SysUserExceptionEnum.java index 21fff4c9d..d9d7c09f1 100644 --- a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/exception/enums/user/SysUserExceptionEnum.java +++ b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/exception/enums/user/SysUserExceptionEnum.java @@ -96,7 +96,12 @@ public enum SysUserExceptionEnum implements AbstractExceptionEnum { /** * 系统错误,账号存在多个 */ - ACCOUNT_HAVE_MANY(RuleConstants.BUSINESS_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "712", "系统错误,账号存在多个,账号为:{}"); + ACCOUNT_HAVE_MANY(RuleConstants.BUSINESS_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "712", "系统错误,账号存在多个,账号为:{}"), + + /** + * 请求参数token为空 + */ + TOKEN_EMPTY(RuleConstants.BUSINESS_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "713", "请求参数token为空"); /** * 错误编码 diff --git a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/provider/LoginUserProvider.java b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/provider/LoginUserProvider.java new file mode 100644 index 000000000..1160db07d --- /dev/null +++ b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/provider/LoginUserProvider.java @@ -0,0 +1,76 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.system.modular.user.provider; + +import cn.hutool.core.util.StrUtil; +import cn.stylefeng.roses.kernel.auth.api.SessionManagerApi; +import cn.stylefeng.roses.kernel.auth.api.loginuser.api.LoginUserRemoteApi; +import cn.stylefeng.roses.kernel.auth.api.loginuser.pojo.LoginUserRequest; +import cn.stylefeng.roses.kernel.auth.api.loginuser.pojo.SessionValidateResponse; +import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser; +import cn.stylefeng.roses.kernel.system.api.UserServiceApi; +import cn.stylefeng.roses.kernel.system.api.exception.SystemModularException; +import cn.stylefeng.roses.kernel.system.api.exception.enums.user.SysUserExceptionEnum; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * 图形验证码 + * + * @author chenjinlong + * @date 2021/1/15 15:11 + */ +@RestController +public class LoginUserProvider implements LoginUserRemoteApi { + + @Resource + private SessionManagerApi sessionManagerApi; + + @Resource + private UserServiceApi userServiceApi; + + @Override + public LoginUser getLoginUserByToken(@RequestBody LoginUserRequest loginUserRequest) { + if (StrUtil.isBlank(loginUserRequest.getToken())) { + throw new SystemModularException(SysUserExceptionEnum.TOKEN_EMPTY); + } + return sessionManagerApi.getSession(loginUserRequest.getToken()); + } + + @Override + public SessionValidateResponse haveSession(@RequestParam("token") String token) { + boolean validateFlag = sessionManagerApi.haveSession(token); + return new SessionValidateResponse(validateFlag); + } + + @Override + public LoginUser getEffectiveLoginUser(@RequestBody LoginUser loginUser) { + return userServiceApi.getEffectiveLoginUser(loginUser); + } + +}