通过 @ModelAttribute 获取token
@Controller
public class AuthenticatorController extends BaseController {
@ModelAttribute
public void getUserId(HttpServletRequest request, HttpServletResponse response, Model model) throws NoSuchAlgorithmException {
log.info("进入到AuthenticatorController");
String deviceId = request.getParameter("deviceId");
String channel = request.getParameter("channel");
String version = request.getParameter("version");
String signature = request.getParameter("signature");
String token = request.getParameter("token");
String temp;
String type = "";
try {
type = signature.substring(signature.length() - 1, signature.length());
} catch (Exception e) {
throw new BusinessException(ErrorCodeEnum.TOKEN_ERROR);
}
if ("1".equals(channel)) {
if ("1".equals(type)) {
temp = PasswordUtil.md5(deviceId + channel + version + deviceId.substring(deviceId.length() - 5, deviceId.length())) + "1";
} else if ("2".equals(type)) {
temp = PasswordUtil.md5(deviceId.substring(deviceId.length() - 5, deviceId.length()) + deviceId + channel + version) + "2";
} else {
temp = PasswordUtil.md5(deviceId + deviceId.substring(deviceId.length() - 5, deviceId.length()) + channel + version) + "3";
}
} else {
if ("1".equals(type)) {
temp = PasswordUtil.md5(deviceId.substring(0, 5) + deviceId + channel + version) + "1";
} else if ("2".equals(type)) {
temp = PasswordUtil.md5(deviceId + channel + version + deviceId.substring(0, 5)) + "2";
} else {
temp = PasswordUtil.md5(deviceId + channel + deviceId.substring(0, 5) + version) + "3";
}
}
if (!temp.equals(signature)) {
throw new BusinessException(ErrorCodeEnum.TOKEN_ERROR);
}
String url = request.getRequestURI();
if (checkNeedAuthority(url)) {
try {
if (StrUtil.isNotBlank(token)) {
Long userId = RedisUtil.getCacheObject(VisionTimeConstant.REDIS_USER_TICKET + ":" + token);
if (Objects.isNull(userId) || userId == 0L) {
throw new BusinessException(ErrorCodeEnum.TOKEN_IS_NULL);
} else {
model.addAttribute("userId", userId);
}
} else {
throw new BusinessException(ErrorCodeEnum.TOKEN_IS_NULL);
}
} catch (Exception e) {
if (checkNeedToken(url)) {
model.addAttribute("userId", null);
} else {
throw new BusinessException(ErrorCodeEnum.TOKEN_IS_NULL);
}
}
} else {
try {
if (StrUtil.isNotBlank(token)) {
Long userId = RedisUtil.getCacheObject(VisionTimeConstant.REDIS_USER_TICKET + ":" + token);
if (userId == null) {
model.addAttribute("userId", null);
} else {
model.addAttribute("userId", userId);
}
} else {
model.addAttribute("userId", null);
}
} catch (Exception e) {
model.addAttribute("userId", null);
}
}
}
public boolean checkNeedAuthority(String url) {
log.info("checkNeedAuthority : {}", url);
if (url.contains("user_bannerList")) {
return false;
} else if (url.contains("user")) {
return true;
} else {
return true;
}
}
private static final String[] urls = {
"user_cityList",
"forgetPassword",
"sendMobileVerify",
"login",
"bindPhone",
"queryAllArticleIndex",
"queryArticleDetailById",
"queryDetailById",
"queryAllSpecByItemId",
"queryAllParamByItemId",
"queryAllDict",
"queryAllByMerchantId",
"queryAllMerchant",
"queryMerchantDetail",
"queryAllMerchantComment",
"area"
};
public boolean checkNeedToken(String url) {
log.info("checkNeedToken : {}", url);
for (int i = 1; i <= urls.length; i++) {
if (url.contains(urls[i - 1])) {
return true;
}
}
return false;
}
}