19 changed files with 1809 additions and 1514 deletions
@ -0,0 +1,24 @@ |
|||||
|
package com.ccsens.ptccsens.annotation; |
||||
|
|
||||
|
import java.lang.annotation.*; |
||||
|
|
||||
|
/** |
||||
|
* @description: 用于标识方法需要登录,获取userId |
||||
|
* 如果未登录,直接返回用户未登录 |
||||
|
* @author: wuHuiJuan |
||||
|
* @create: 2019/12/09 09:48 |
||||
|
*/ |
||||
|
@Documented |
||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||
|
@Target(ElementType.METHOD) |
||||
|
public @interface MustLogin { |
||||
|
/** |
||||
|
* -1 不处理 |
||||
|
* 0: 数组 |
||||
|
* 1:List |
||||
|
* 2:Set |
||||
|
* 3: Map |
||||
|
* */ |
||||
|
byte type() default -1; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,138 @@ |
|||||
|
package com.ccsens.ptccsens.aspect; |
||||
|
|
||||
|
import cn.hutool.core.util.ObjectUtil; |
||||
|
import cn.hutool.core.util.StrUtil; |
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.ccsens.ptccsens.annotation.MustLogin; |
||||
|
import com.ccsens.ptccsens.bean.po.ProUser; |
||||
|
import com.ccsens.ptccsens.persist.dao.UserDao; |
||||
|
import com.ccsens.ptccsens.util.Constant; |
||||
|
import com.ccsens.util.CodeEnum; |
||||
|
import com.ccsens.util.JsonResponse; |
||||
|
import com.ccsens.util.WebConstant; |
||||
|
import com.ccsens.util.bean.dto.QueryDto; |
||||
|
import com.ccsensptos.tallsdk.bean.dto.TallTokenDto; |
||||
|
import com.ccsensptos.tallsdk.bean.vo.TallTokenVo; |
||||
|
import com.ccsensptos.tallsdk.util.TokenUtil; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.aspectj.lang.ProceedingJoinPoint; |
||||
|
import org.aspectj.lang.Signature; |
||||
|
import org.aspectj.lang.annotation.Around; |
||||
|
import org.aspectj.lang.annotation.Aspect; |
||||
|
import org.aspectj.lang.annotation.Pointcut; |
||||
|
import org.aspectj.lang.reflect.MethodSignature; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.core.annotation.Order; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.springframework.web.context.request.RequestContextHolder; |
||||
|
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import java.lang.reflect.Array; |
||||
|
import java.lang.reflect.Method; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @description: |
||||
|
* @author: wuHuiJuan |
||||
|
* @create: 2019/12/09 09:54 |
||||
|
*/ |
||||
|
@Order(0) |
||||
|
@Slf4j |
||||
|
@Aspect |
||||
|
@Component |
||||
|
public class MustLoginAspect { |
||||
|
@Resource |
||||
|
private UserDao userDao; |
||||
|
|
||||
|
@Pointcut("@annotation(com.ccsens.ptccsens.annotation.MustLogin)") |
||||
|
public void loginAdvice(){} |
||||
|
|
||||
|
@Around("loginAdvice()") |
||||
|
public Object around(ProceedingJoinPoint pjp) throws Throwable { |
||||
|
|
||||
|
HttpServletRequest request = ((ServletRequestAttributes) |
||||
|
RequestContextHolder.getRequestAttributes()).getRequest(); |
||||
|
|
||||
|
final String authHeader = request.getHeader(WebConstant.HEADER_KEY_TOKEN); |
||||
|
|
||||
|
Object[] args = pjp.getArgs(); |
||||
|
QueryDto dto = args == null || args.length < 1 ? null : (QueryDto) args[0]; |
||||
|
|
||||
|
//获取userId
|
||||
|
ProUser user = null; |
||||
|
if(StrUtil.isNotEmpty(authHeader)){ |
||||
|
log.info("MustLogin————token:{}", authHeader); |
||||
|
//通过token查找用户信息
|
||||
|
//TODO 根据token获取用户信息
|
||||
|
TallTokenVo.UserIdByToken userByToken = TokenUtil.getUserByToken(new TallTokenDto.GetUserByToken(authHeader, Constant.APP_ID, Constant.APP_SECRET)); |
||||
|
if(ObjectUtil.isNull(userByToken)){ |
||||
|
return JsonResponse.newInstance().ok(CodeEnum.NOT_LOGIN); |
||||
|
} |
||||
|
//通过手机号获取用户在服务内的userId
|
||||
|
if(StrUtil.isNotBlank(userByToken.getPhone())){ |
||||
|
user = userDao.getUserIdByPhone(userByToken.getPhone()); |
||||
|
log.info("{}获取user:{}", authHeader, user); |
||||
|
} |
||||
|
} |
||||
|
Signature signature = pjp.getSignature(); |
||||
|
MethodSignature methodSignature = (MethodSignature) signature; |
||||
|
Method targetMethod = methodSignature.getMethod(); |
||||
|
|
||||
|
MustLogin mustLoginAnnotation = targetMethod.getAnnotation(MustLogin.class); |
||||
|
fillSpecial(dto, mustLoginAnnotation); |
||||
|
|
||||
|
//必须登录,未登录直接返回未登录相关信息
|
||||
|
if (user == null) { |
||||
|
return JsonResponse.newInstance().ok(CodeEnum.NOT_LOGIN); |
||||
|
} |
||||
|
// JSONObject json = JSONObject.parseObject(JSON.toJSONString(response.getData()));
|
||||
|
// Long userId = json.getLong("id");
|
||||
|
// String userName = json.getString("userName");
|
||||
|
// String avatarUrl = json.getString("avatarUrl");
|
||||
|
// String phone = json.getString("phone");
|
||||
|
// if (userId == null || userId == 0) {
|
||||
|
// return JsonResponse.newInstance().ok(CodeEnum.NOT_LOGIN);
|
||||
|
// }
|
||||
|
|
||||
|
if (dto != null) { |
||||
|
dto.setUserId(user.getId()); |
||||
|
dto.setPhone(user.getPhone()); |
||||
|
} |
||||
|
|
||||
|
Object result = pjp.proceed(); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
private void fillSpecial(QueryDto dto, MustLogin mustLoginAnnotation) { |
||||
|
if (mustLoginAnnotation == null) { |
||||
|
return; |
||||
|
} |
||||
|
if (dto != null && mustLoginAnnotation.type() > -1) { |
||||
|
switch (mustLoginAnnotation.type()) { |
||||
|
case 0: |
||||
|
Object obj = dto.getParam(); |
||||
|
if (obj!= null && !obj.getClass().isArray()) { |
||||
|
Class<?> aClass = dto.getParam().getClass(); |
||||
|
Object o = Array.newInstance(aClass, 1); |
||||
|
Array.set(o, 0, dto.getParam()); |
||||
|
dto.setParam(o); |
||||
|
} |
||||
|
break; |
||||
|
case 1: |
||||
|
Object obj1 = dto.getParam(); |
||||
|
if (obj1!= null && !(obj1 instanceof List)) { |
||||
|
ArrayList arrayList = new ArrayList(); |
||||
|
arrayList.add(dto.getParam()); |
||||
|
dto.setParam(arrayList); |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue