package com.xxx.vulnweb.filter;
import com.xxx.vulnweb.model.ResponseModel;
import com.xxx.vulnweb.model.user.UserModel;
import com.xxx.vulnweb.util.JsonParseImpl;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Objects;
@Component
@Order(1)
public class LoginFilter implements Filter {
@Autowired
ResponseModel responseModel;
@Autowired
JsonParseImpl jsonParse;
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
if (Objects.equals(request.getRequestURI(), "/login")) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
UserModel user = (UserModel) request.getSession().getAttribute("info");
if (user != null) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
servletResponse.setContentType("application/json");
responseModel.setCode(401);
responseModel.setMsg("Unauthorized");
responseModel.setData(null);
servletResponse.getOutputStream().write(jsonParse.dumps(responseModel).getBytes());
}
}
}
}