mirror of https://gitee.com/stylefeng/roses
【7.1.3】LoginUser更新Remote调用的方法
parent
4580da6c00
commit
6f81cee5c7
|
@ -17,6 +17,13 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<!--解析需要转化时间-->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!--config模块的api-->
|
||||
<!--权限相关的配置要放到容器里-->
|
||||
<dependency>
|
||||
|
@ -38,6 +45,20 @@
|
|||
<artifactId>scanner-api</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--web-->
|
||||
<!--ResourcePersistenceApi会用到web,用在提供feign接口时-->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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为空");
|
||||
|
||||
/**
|
||||
* 错误编码
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue