puzhibing
2023-05-22 92487fe61cddbc791de1215d58ae53e8bef54c5a
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
package com.agentdriving.user.modular.system.auth;
 
import com.alibaba.fastjson.JSON;
import com.agentdriving.user.modular.system.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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;
 
@Component
public class AuthIntercepter implements HandlerInterceptor {
    private final static Logger log = LoggerFactory.getLogger(AuthIntercepter.class);
    
    @Autowired
    AuthService authService;
 
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws IOException {
        String appid = req.getParameter("appid");
        String sign = req.getParameter("sign");
        ResultUtil resultUtil = authService.checkSyncAuth(appid, sign, req);
        if (resultUtil.getCode() != 200) {
            res.setStatus(HttpStatus.OK.value());
            res.setHeader("Content-type", "text/html;charset=UTF-8");
            res.getWriter().print(JSON.toJSONString(resultUtil));//Res.Failure("req timeout, please try again")
            return false;
        }
        return true;
    }
 
}