package com.ee.consultation.filter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ee.consultation.common.ApiMessage;
import com.ee.consultation.common.constants.Constants;
import com.ee.consultation.common.enums.StatusCode;
import com.ee.consultation.common.util.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Slf4j
@WebFilter(urlPatterns = "/*")
@Order(1)
public class TokenFilter implements Filter {
private final ObjectMapper mapper;
public TokenFilter(ObjectMapper mapper) {
this.mapper = mapper;
}
@Override
public void destroy() {
log.info("filter destroy");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
String originHeader = httpServletRequest.getHeader("Origin");
httpServletResponse.setHeader("Access-Control-Allow-Origin", originHeader);
httpServletResponse.addHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With,requestid");
httpServletResponse.addHeader("Access-Control-Allow-Credentials", "true");
httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
String method =httpServletRequest.getMethod();
if ("OPTIONS".equals(method) || "PUT".equals(method)){
chain.doFilter(request, response);
return;
}
String path = httpServletRequest.getRequestURI();
if(path.indexOf("index") != -1){
chain.doFilter(request, response);
return;
}
if (path != null) {
if (path.contains(Constants.LOGIN)
|| path.contains(Constants.DOC)
|| path.contains(Constants.FAVICON)
|| path.contains(Constants.SWAGGER)
|| path.contains(Constants.API)
|| path.contains(Constants.WEBJARS)
|| path.contains(Constants.HAIRDETECTIONCONTROLLER)
|| path.contains(Constants.DICTDATA)
|| path.contains(Constants.DOCTORFILE_SCREEN)
|| path.contains(Constants.DOCTORFILE_DOCTOR)
) {
chain.doFilter(request, response);
return;
}
}
String token = httpServletRequest.getHeader(Constants.AUTHORIZATION);
if (ObjectUtils.isEmpty(token) || ObjectUtils.isEmpty(UserUtils.getUserId())) {
httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
httpServletResponse.setContentType("application/json;charset=utf-8");
ApiMessage apiMessage = ApiMessage.error(StatusCode.USER_IS_NOT_LOGIN);
httpServletResponse.getWriter().write(mapper.writeValueAsString(apiMessage));
return;
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) {
log.info("filter init");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97