【auth】【system】增加updateSession接口,更新头像会刷新缓存

pull/3/head
fengshuonan 2021-01-09 10:52:37 +08:00
parent 604d6027b3
commit 26a79e6c21
4 changed files with 40 additions and 3 deletions

View File

@ -26,6 +26,16 @@ public interface SessionManagerApi {
*/
void createSession(String token, LoginUser loginUser);
/**
* loginUser
*
* @param token token
* @param loginUser
* @author fengshuonan
* @date 2021/1/9 10:43
*/
void updateSession(String token, LoginUser loginUser);
/**
* token
*

View File

@ -11,7 +11,6 @@ import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -86,6 +85,18 @@ public class DefaultSessionManager implements SessionManagerApi {
}
@Override
public void updateSession(String token, LoginUser loginUser) {
LoginUser session = this.getSession(token);
// 该用户session为空不能更新
if (session == null) {
return;
}
loginUserCache.put(token, loginUser, sessionExpiredSeconds);
}
@Override
public LoginUser getSession(String token) {
return loginUserCache.get(token);
@ -173,4 +184,5 @@ public class DefaultSessionManager implements SessionManagerApi {
public List<LoginUser> onlineUserList() {
return new ArrayList<>(loginUserCache.getAllValues());
}
}

View File

@ -26,7 +26,7 @@ public class SysUserRequest extends BaseRequest {
/**
*
*/
@NotNull(message = "userId不能为空", groups = {edit.class, delete.class, detail.class, grantRole.class, grantData.class, updateInfo.class, resetPwd.class, changeStatus.class, updateAvatar.class})
@NotNull(message = "userId不能为空", groups = {edit.class, delete.class, detail.class, grantRole.class, grantData.class, updateInfo.class, resetPwd.class, changeStatus.class})
private Long userId;
/**

View File

@ -7,6 +7,7 @@ import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.expander.AuthConfigExpander;
import cn.stylefeng.roses.kernel.auth.api.password.PasswordStoredEncryptApi;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleUserInfo;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
@ -216,9 +217,23 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Override
public void updateAvatar(SysUserRequest sysUserRequest) {
// 新头像文件id
Long fileId = sysUserRequest.getAvatar();
// 从当前用户获取用户id
LoginUser loginUser = LoginContext.me().getLoginUser();
sysUserRequest.setUserId(loginUser.getUserId());
// 更新用户头像
SysUser sysUser = this.querySysUser(sysUserRequest);
sysUser.setAvatar(sysUserRequest.getAvatar());
sysUser.setAvatar(fileId);
this.updateById(sysUser);
// 更新当前用户的session信息
SimpleUserInfo simpleUserInfo = loginUser.getSimpleUserInfo();
simpleUserInfo.setAvatar(fileId);
sessionManagerApi.updateSession(LoginContext.me().getToken(), loginUser);
}
@Override