You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-wms-java/src/main/java/com/glxp/api/aspect/AuthorizeAspect.java

128 lines
4.2 KiB
Java

package com.glxp.api.aspect;
import cn.hutool.core.util.StrUtil;
import com.glxp.api.annotation.AuthRuleAnnotation;
import com.glxp.api.dao.auth.AuthLicenseDao;
import com.glxp.api.entity.auth.AuthLicense;
import com.glxp.api.exception.JsonException;
import com.glxp.api.util.JwtUtils;
import com.glxp.api.common.enums.ResultEnum;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
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.Method;
import java.util.Enumeration;
/**
* 登录验证 AOP
*/
@Aspect
@Component
@Slf4j
public class AuthorizeAspect {
@Resource
private AuthLicenseDao authLicenseDao;
@Pointcut("@annotation(com.glxp.api.annotation.AuthRuleAnnotation)")
public void adminLoginVerify() {
}
/**
* 登录验证
*
* @param joinPoint
*/
@Before("adminLoginVerify()")
public void doAdminAuthVerify(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
throw new JsonException(ResultEnum.NOT_NETWORK);
}
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
// 根据头部名称获取相应的值
String value = request.getHeader(name);
// log.info("Header Name: " + name);
// log.info("Header Value: " + value);
}
String id = getAdminId(request);
if (StrUtil.isBlank(id)) {
String apiKey = request.getHeader("api_key");
String secretKey = request.getHeader("secret_key");
AuthLicense authLicense = authLicenseDao.get(apiKey);
if (authLicense != null && authLicense.getSecretKey().equals(secretKey)) {
} else {
throw new JsonException(ResultEnum.LOGIN_VERIFY_FALL);
}
} else {
String query = request.getQueryString();
Long adminId = null;
try {
adminId = Long.valueOf(id);
} catch (Exception e) {
throw new JsonException(ResultEnum.LOGIN_VERIFY_FALL);
}
String token = getToken(request);
if (token == null) {
throw new JsonException(ResultEnum.LOGIN_VERIFY_FALL);
}
// 验证 token
Claims claims = JwtUtils.parse(token);
if (claims == null) {
throw new JsonException(ResultEnum.LOGIN_VERIFY_FALL);
}
Long jwtAdminId = Long.valueOf(claims.get("admin_id").toString());
if (adminId.compareTo(jwtAdminId) != 0) {
throw new JsonException(ResultEnum.LOGIN_VERIFY_FALL);
}
}
// 判断是否进行权限验证
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//从切面中获取当前方法
Method method = signature.getMethod();
//得到了方,提取出他的注解
AuthRuleAnnotation action = method.getAnnotation(AuthRuleAnnotation.class);
// 进行权限验证
// authRuleVerify(action.value(), adminId);
}
public String getAdminId(HttpServletRequest request) {
String id = request.getHeader("ADMINID");
if (StrUtil.isBlank(id)) {
id = request.getHeader("ADMIN_ID");
}
return id;
}
public String getToken(HttpServletRequest request) {
String token = request.getHeader("ADMINTOKEN");
if (StrUtil.isBlank(token)) {
token = request.getHeader("ADMIN_TOKEN");
}
return token;
}
}