三只松鼠 пре 4 година
родитељ
комит
3478333fd1
1 измењених фајлова са 58 додато и 0 уклоњено
  1. +58
    -0
      src/main/java/com/taauav/common/interceptor/AppInterceptor.java

+ 58
- 0
src/main/java/com/taauav/common/interceptor/AppInterceptor.java Прегледај датотеку

@@ -0,0 +1,58 @@
package com.taauav.common.interceptor;

import com.alibaba.fastjson.JSON;
import com.taauav.common.bean.Response;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* 前台登陆拦截器
* @author dyg
*/
@Component
public class AppInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//获取小程序传递的openid 如果没有openid 需要登录的页面返回登录信息
String openid = request.getHeader("token");
String url = request.getRequestURI();
//排除不需要验证的url
if (openid == null && validateUrl(url) && false) {
String msg = "请先授权";
Response result = new Response();
result.failure(msg);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-type", "application/json;charset=UTF-8");
response.setStatus(200);
try {
response.getWriter().write((String) Objects.requireNonNull(JSON.toJSON(result).toString()));
} catch (IOException var6) {
var6.printStackTrace();
}

return false;
}
return true;
}

private boolean validateUrl(String url) {
boolean f = true;
List<String> list = new ArrayList<>();
list.add("app/login/login");
for (String st : list) {
if (st.equals(url)) {
f = false;
break;
}
}
return f;
}
}

Loading…
Откажи
Сачувај