【7.1.3】LoginUser更新Remote调用的方法

pull/22/head
fengshuonan 2021-09-29 14:10:48 +08:00
parent 4580da6c00
commit 6f81cee5c7
10 changed files with 289 additions and 40 deletions

View File

@ -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>

View File

@ -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);
}
}

View File

@ -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.
*
* GunsAPACHE 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);
/**
* loginUserLoginUser
*
* @author fengshuonan
* @date 2021/9/29 11:39
*/
@RequestMapping(value = "/loginUserRemote/getEffectiveLoginUser", method = RequestMethod.POST)
LoginUser getEffectiveLoginUser(@RequestBody LoginUser loginUser);
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
/**

View File

@ -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;
/**

View File

@ -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

View File

@ -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为空");
/**
*

View File

@ -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.
*
* GunsAPACHE 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);
}
}