Commit a1573ab5 by hewei

Merge branch 'future/strokeList' into 'master'

商家登陆jwt添加mcid

See merge request hewei/Jumeirah!29
parents 9ca4c871 cf683383
...@@ -2,9 +2,11 @@ package com.jumeirah.api.merchant.controller; ...@@ -2,9 +2,11 @@ package com.jumeirah.api.merchant.controller;
import io.geekidea.springbootplus.framework.common.api.ApiResult; import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog; import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.shiro.jwt.JwtToken;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles; import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -26,6 +28,8 @@ public class MerchantHelloWorldController { ...@@ -26,6 +28,8 @@ public class MerchantHelloWorldController {
@OperationLog(name = "helloWorld") @OperationLog(name = "helloWorld")
@ApiOperation(value = "Hello World", response = String.class) @ApiOperation(value = "Hello World", response = String.class)
public ApiResult<String> helloWorld() throws IOException { public ApiResult<String> helloWorld() throws IOException {
JwtToken jwtToken = (JwtToken) SecurityUtils.getSubject().getPrincipal();
log.debug("Hello World...app"); log.debug("Hello World...app");
return ApiResult.ok("Hello World app merchant"); return ApiResult.ok("Hello World app merchant");
} }
...@@ -34,6 +38,8 @@ public class MerchantHelloWorldController { ...@@ -34,6 +38,8 @@ public class MerchantHelloWorldController {
@OperationLog(name = "needRole") @OperationLog(name = "needRole")
@ApiOperation(value = "needRole", response = String.class) @ApiOperation(value = "needRole", response = String.class)
public ApiResult<String> needRole() throws IOException { public ApiResult<String> needRole() throws IOException {
JwtToken jwtToken = (JwtToken) SecurityUtils.getSubject().getPrincipal();
log.debug("Hello World...app"); log.debug("Hello World...app");
return ApiResult.ok("Hello World app"); return ApiResult.ok("Hello World app");
} }
...@@ -43,6 +49,8 @@ public class MerchantHelloWorldController { ...@@ -43,6 +49,8 @@ public class MerchantHelloWorldController {
@ApiOperation(value = "needRoleAdmin", response = String.class) @ApiOperation(value = "needRoleAdmin", response = String.class)
@RequiresPermissions("merchant:admin") @RequiresPermissions("merchant:admin")
public ApiResult<String> needRoleAdmin() throws IOException { public ApiResult<String> needRoleAdmin() throws IOException {
JwtToken jwtToken = (JwtToken) SecurityUtils.getSubject().getPrincipal();
log.debug("Hello World...app"); log.debug("Hello World...app");
return ApiResult.ok("Hello World needRoleAdmin"); return ApiResult.ok("Hello World needRoleAdmin");
} }
...@@ -52,6 +60,8 @@ public class MerchantHelloWorldController { ...@@ -52,6 +60,8 @@ public class MerchantHelloWorldController {
@ApiOperation(value = "needRoleAll", response = String.class) @ApiOperation(value = "needRoleAll", response = String.class)
@RequiresRoles("merchant:all") @RequiresRoles("merchant:all")
public ApiResult<String> needRoleAll() throws IOException { public ApiResult<String> needRoleAll() throws IOException {
JwtToken jwtToken = (JwtToken) SecurityUtils.getSubject().getPrincipal();
log.debug("Hello World...app"); log.debug("Hello World...app");
return ApiResult.ok("Hello World needRoleAll"); return ApiResult.ok("Hello World needRoleAll");
} }
...@@ -60,6 +70,8 @@ public class MerchantHelloWorldController { ...@@ -60,6 +70,8 @@ public class MerchantHelloWorldController {
@OperationLog(name = "noRole") @OperationLog(name = "noRole")
@ApiOperation(value = "noRole", response = String.class) @ApiOperation(value = "noRole", response = String.class)
public ApiResult<String> noRole() throws IOException { public ApiResult<String> noRole() throws IOException {
JwtToken jwtToken = (JwtToken) SecurityUtils.getSubject().getPrincipal();
log.debug("Hello World...app"); log.debug("Hello World...app");
return ApiResult.ok("Hello World app noRole"); return ApiResult.ok("Hello World app noRole");
} }
......
...@@ -116,7 +116,7 @@ public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser> ...@@ -116,7 +116,7 @@ public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser>
log.debug("token:{}", token); log.debug("token:{}", token);
// 创建AuthenticationToken // 创建AuthenticationToken
JwtToken jwtToken = JwtToken.build(token, appUser.getId().toString(), appUser.getId(), newSalt, expireSecond, LoginClientTypeEnum.APP.getType()); JwtToken jwtToken = JwtToken.build(token, appUser.getId().toString(), appUser.getId(), newSalt, expireSecond, LoginClientTypeEnum.APP.getType(), null);
boolean enableShiro = springBootPlusProperties.getShiro().isEnable(); boolean enableShiro = springBootPlusProperties.getShiro().isEnable();
if (enableShiro) { if (enableShiro) {
......
...@@ -92,14 +92,10 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper, ...@@ -92,14 +92,10 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper,
return merchantUserMapper.selectOne(new QueryWrapper<MerchantUser>(sysUser)); return merchantUserMapper.selectOne(new QueryWrapper<MerchantUser>(sysUser));
} }
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@Override @Override
public ApiResult<LoginMerUserTokenVo> login(MerchantLoginParam merchantLoginParam) throws Exception { public ApiResult<LoginMerUserTokenVo> login(MerchantLoginParam merchantLoginParam) throws Exception {
// // 校验验证码
// checkVerifyCode(merchantLoginParam.getVerifyToken(), merchantLoginParam.getCode());
String username = merchantLoginParam.getUsername(); String username = merchantLoginParam.getUsername();
// 从数据库中获取登录用户信息 // 从数据库中获取登录用户信息
MerchantUser merchantUser = getMerUserByUsername(username); MerchantUser merchantUser = getMerUserByUsername(username);
...@@ -108,7 +104,9 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper, ...@@ -108,7 +104,9 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper,
return ApiResult.result(ApiCode.PWD_OR_USERNAME_ERROR, null); return ApiResult.result(ApiCode.PWD_OR_USERNAME_ERROR, null);
} }
if (StateEnum.DISABLE.getCode().equals(merchantUser.getState())) { if (StateEnum.DISABLE.getCode().equals(merchantUser.getState())) {
return ApiResult.result(ApiCode.PWD_OR_USERNAME_ERROR, null); log.error("登录失败,禁用:{}", merchantLoginParam);
return ApiResult.result(ApiCode.LOGIN_EXCEPTION, null);
} }
// 实际项目中,前端传过来的密码应先加密 // 实际项目中,前端传过来的密码应先加密
...@@ -123,17 +121,6 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper, ...@@ -123,17 +121,6 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper,
// 将系统用户对象转换成登录用户对象 // 将系统用户对象转换成登录用户对象
LoginUserVo loginSysUserVo = UserConvert.INSTANCE.merchantUserToLoginSysUserVo(merchantUser); LoginUserVo loginSysUserVo = UserConvert.INSTANCE.merchantUserToLoginSysUserVo(merchantUser);
// // 获取部门
// SysDepartment sysDepartment = sysDepartmentService.getById(merchantUser.getDepartmentId());
// if (sysDepartment == null) {
// throw new AuthenticationException("部门不存在");
// }
// if (!StateEnum.ENABLE.getCode().equals(sysDepartment.getState())) {
// throw new AuthenticationException("部门已禁用");
// }
// loginSysUserVo.setDepartmentId(sysDepartment.getId())
// .setDepartmentName(sysDepartment.getName());
// 获取当前用户角色 // 获取当前用户角色
Long roleId = merchantUser.getRoleId(); Long roleId = merchantUser.getRoleId();
MerchantRole merchantRole = merchantRoleService.getById(roleId); MerchantRole merchantRole = merchantRoleService.getById(roleId);
...@@ -149,9 +136,6 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper, ...@@ -149,9 +136,6 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper,
// 获取当前用户权限 // 获取当前用户权限
Set<String> permissionCodes = merchantRolePermissionService.getPermissionCodesByRoleId(roleId); Set<String> permissionCodes = merchantRolePermissionService.getPermissionCodesByRoleId(roleId);
// if (CollectionUtils.isEmpty(permissionCodes)) {
// throw new AuthenticationException("权限列表不能为空");
// }
loginSysUserVo.setPermissionCodes(permissionCodes); loginSysUserVo.setPermissionCodes(permissionCodes);
// 获取数据库中保存的盐值 // 获取数据库中保存的盐值
...@@ -163,7 +147,7 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper, ...@@ -163,7 +147,7 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper,
log.debug("token:{}", token); log.debug("token:{}", token);
// 创建AuthenticationToken // 创建AuthenticationToken
JwtToken jwtToken = JwtToken.build(token, username,merchantUser.getId(), newSalt, expireSecond, LoginClientTypeEnum.MERCHANT.getType()); JwtToken jwtToken = JwtToken.build(token, username, merchantUser.getId(), newSalt, expireSecond, LoginClientTypeEnum.MERCHANT.getType(),merchantUser.getMcId());
boolean enableShiro = springBootPlusProperties.getShiro().isEnable(); boolean enableShiro = springBootPlusProperties.getShiro().isEnable();
if (enableShiro) { if (enableShiro) {
...@@ -188,9 +172,6 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper, ...@@ -188,9 +172,6 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper,
loginSysUserTokenVo.setToken(token); loginSysUserTokenVo.setToken(token);
loginSysUserTokenVo.setLoginSysUserVo(loginSysUserVo); loginSysUserTokenVo.setLoginSysUserVo(loginSysUserVo);
// 设置token响应头
// response.setHeader(JwtTokenUtil.getTokenName(), loginSysUserTokenVo.getToken());
return ApiResult.ok(loginSysUserTokenVo); return ApiResult.ok(loginSysUserTokenVo);
} }
...@@ -222,7 +203,6 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper, ...@@ -222,7 +203,6 @@ public class MerchantUserServiceImpl extends BaseServiceImpl<MerchantUserMapper,
// 创建一个该公司默认的管理员账号 // 创建一个该公司默认的管理员账号
return ApiResult.ok(); return ApiResult.ok();
} }
......
...@@ -162,7 +162,7 @@ public class SysLoginServiceImpl implements SysLoginService { ...@@ -162,7 +162,7 @@ public class SysLoginServiceImpl implements SysLoginService {
log.debug("token:{}", token); log.debug("token:{}", token);
// 创建AuthenticationToken // 创建AuthenticationToken
JwtToken jwtToken = JwtToken.build(token, username,sysUser.getId(), newSalt, expireSecond, LoginClientTypeEnum.SYSTEM.getType()); JwtToken jwtToken = JwtToken.build(token, username,sysUser.getId(), newSalt, expireSecond, LoginClientTypeEnum.SYSTEM.getType(),null);
boolean enableShiro = springBootPlusProperties.getShiro().isEnable(); boolean enableShiro = springBootPlusProperties.getShiro().isEnable();
if (enableShiro) { if (enableShiro) {
......
...@@ -42,11 +42,11 @@ public class MerchantLoginRedisServiceImpl implements MerchantLoginRedisService ...@@ -42,11 +42,11 @@ public class MerchantLoginRedisServiceImpl implements MerchantLoginRedisService
* username:num * username:num
*/ */
@Override @Override
public void cacheLoginInfo(JwtToken jwtToken, LoginUserVo loginSysUserVo) { public void cacheLoginInfo(JwtToken jwtToken, LoginUserVo loginUserVo) {
if (jwtToken == null) { if (jwtToken == null) {
throw new IllegalArgumentException("jwtToken不能为空"); throw new IllegalArgumentException("jwtToken不能为空");
} }
if (loginSysUserVo == null) { if (loginUserVo == null) {
throw new IllegalArgumentException("loginSysUserVo不能为空"); throw new IllegalArgumentException("loginSysUserVo不能为空");
} }
// token // token
...@@ -54,20 +54,20 @@ public class MerchantLoginRedisServiceImpl implements MerchantLoginRedisService ...@@ -54,20 +54,20 @@ public class MerchantLoginRedisServiceImpl implements MerchantLoginRedisService
// 盐值 // 盐值
String salt = jwtToken.getSalt(); String salt = jwtToken.getSalt();
// 登录用户名称 // 登录用户名称
String username = loginSysUserVo.getUsername(); String username = loginUserVo.getUsername();
// token md5值 // token md5值
String tokenMd5 = DigestUtils.md5Hex(token); String tokenMd5 = DigestUtils.md5Hex(token);
// Redis缓存JWT Token信息 // Redis缓存JWT Token信息
JwtTokenRedisVo jwtTokenRedisVo = ShiroMapstructConvert.INSTANCE.jwtTokenToJwtTokenRedisVo(jwtToken); JwtTokenRedisVo jwtTokenRedisVo = ShiroMapstructConvert.INSTANCE.jwtTokenToJwtTokenRedisVo(jwtToken);
jwtTokenRedisVo.setUserId(loginSysUserVo.getId()); jwtTokenRedisVo.setUserId(loginUserVo.getId());
jwtTokenRedisVo.setMcId(jwtToken.getMcId());
// 用户客户端信息 // 用户客户端信息
ClientInfo clientInfo = ClientInfoUtil.get(HttpServletRequestUtil.getRequest()); ClientInfo clientInfo = ClientInfoUtil.get(HttpServletRequestUtil.getRequest());
// Redis缓存登录用户信息 // Redis缓存登录用户信息
// 将LoginSysUserVo对象复制到LoginSysUserRedisVo,使用mapstruct进行对象属性复制 // 将LoginSysUserVo对象复制到LoginSysUserRedisVo,使用mapstruct进行对象属性复制
LoginUserRedisVo loginSysUserRedisVo = LoginSysUserVoConvert.INSTANCE.voToRedisVo(loginSysUserVo); LoginUserRedisVo loginSysUserRedisVo = LoginSysUserVoConvert.INSTANCE.voToRedisVo(loginUserVo);
loginSysUserRedisVo.setSalt(salt); loginSysUserRedisVo.setSalt(salt);
loginSysUserRedisVo.setClientInfo(clientInfo); loginSysUserRedisVo.setClientInfo(clientInfo);
......
...@@ -80,7 +80,7 @@ public class JwtFilter extends AuthenticatingFilter { ...@@ -80,7 +80,7 @@ public class JwtFilter extends AuthenticatingFilter {
String username = JwtUtil.getUsername(token); String username = JwtUtil.getUsername(token);
return JwtToken.build(token, username, jwt.getUserId(), shiroLoginService.getSalt(token), jwtProperties.getExpireSecond(), jwt.getType()); return JwtToken.build(token, username, jwt.getUserId(), shiroLoginService.getSalt(token), jwtProperties.getExpireSecond(), jwt.getType(),jwt.getMcId());
} }
......
...@@ -37,9 +37,17 @@ import java.util.Date; ...@@ -37,9 +37,17 @@ import java.util.Date;
public class JwtToken implements HostAuthenticationToken { public class JwtToken implements HostAuthenticationToken {
private static final long serialVersionUID = 5101247566043093405L; private static final long serialVersionUID = 5101247566043093405L;
/**
* 客户端类型
*/
private String type; private String type;
/** /**
* mcId
*/
private Long mcId;
/**
* 登录ip * 登录ip
*/ */
private String host; private String host;
...@@ -78,7 +86,7 @@ public class JwtToken implements HostAuthenticationToken { ...@@ -78,7 +86,7 @@ public class JwtToken implements HostAuthenticationToken {
private String credentials; private String credentials;
public static JwtToken build(String token, String username,Long userId, String salt, long expireSecond, String type) { public static JwtToken build(String token, String username,Long userId, String salt, long expireSecond, String type,Long mcId) {
DecodedJWT decodedJwt = JwtUtil.getJwtInfo(token); DecodedJWT decodedJwt = JwtUtil.getJwtInfo(token);
Date createDate = decodedJwt.getIssuedAt(); Date createDate = decodedJwt.getIssuedAt();
Date expireDate = decodedJwt.getExpiresAt(); Date expireDate = decodedJwt.getExpiresAt();
...@@ -91,7 +99,8 @@ public class JwtToken implements HostAuthenticationToken { ...@@ -91,7 +99,8 @@ public class JwtToken implements HostAuthenticationToken {
.setType(type) .setType(type)
.setCreateDate(createDate) .setCreateDate(createDate)
.setExpireSecond(expireSecond) .setExpireSecond(expireSecond)
.setExpireDate(expireDate); .setExpireDate(expireDate)
.setMcId(mcId);
} }
......
...@@ -100,7 +100,7 @@ public class ShiroLoginServiceImpl implements ShiroLoginService { ...@@ -100,7 +100,7 @@ public class ShiroLoginServiceImpl implements ShiroLoginService {
// 生成新token字符串 // 生成新token字符串
String newToken = JwtUtil.generateToken(username, salt, Duration.ofSeconds(expireSecond)); String newToken = JwtUtil.generateToken(username, salt, Duration.ofSeconds(expireSecond));
// 生成新JwtToken对象 // 生成新JwtToken对象
JwtToken newJwtToken = JwtToken.build(newToken, username, jwtToken.getUserId(), salt, expireSecond, jwtToken.getType()); JwtToken newJwtToken = JwtToken.build(newToken, username, jwtToken.getUserId(), salt, expireSecond, jwtToken.getType(), null);
// 更新redis缓存 // 更新redis缓存
sysLoginRedisService.refreshLoginInfo(token, username, newJwtToken); sysLoginRedisService.refreshLoginInfo(token, username, newJwtToken);
log.debug("刷新token成功,原token:{},新token:{}", token, newToken); log.debug("刷新token成功,原token:{},新token:{}", token, newToken);
......
...@@ -33,9 +33,16 @@ import java.util.Date; ...@@ -33,9 +33,16 @@ import java.util.Date;
public class JwtTokenRedisVo implements Serializable { public class JwtTokenRedisVo implements Serializable {
private static final long serialVersionUID = 1831633309466775223L; private static final long serialVersionUID = 1831633309466775223L;
/**
* 客户端类型
*/
private String type; private String type;
/** /**
* mcId
*/
private Long mcId;
/**
* 登录ip * 登录ip
*/ */
private String host; private String host;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment