leutu
2024-05-08 543e4eb01ca210b20876e8139cb3d0403d7d065c
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
package com.skyline.electricity.interceptor;
 
import com.skyline.electricity.controller.*;
import org.springframework.beans.factory.annotation.*;
import org.apache.commons.lang3.*;
import javax.servlet.http.*;
import org.springframework.web.servlet.*;
import java.util.*;
import java.io.*;
import org.apache.commons.codec.binary.Base64;
 
public class LoginInterceptor implements HandlerInterceptor
{
    @Autowired
    private RemoteController remoteController;
    
    public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
        final HttpSession session = request.getSession();
        final IAMUtils iamUtils = new IAMUtils();
        final String code = request.getParameter("code");
        final String iamAccessToken = request.getParameter("iamAccessToken");
        if (code != null && iamAccessToken != null) {
            Object userId = null;
            try {
                userId = iamUtils.getUserInfoFromIam(iamAccessToken);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            if (userId != null && !"".equals(userId)) {
                final Cookie token = new Cookie("iam-access-token", iamAccessToken);
                token.setMaxAge(-1);
                token.setPath("/");
                response.addCookie(token);
                return true;
            }
            return false;
        }
        else {
            if (null == code) {
                return "" != this.getToken(request);
            }
            String tokenOfCode = null;
            try {
                tokenOfCode = iamUtils.getTokenByCode(code);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            if (StringUtils.isNotBlank((CharSequence)tokenOfCode)) {
                final Cookie token = new Cookie("iam-access-token", tokenOfCode);
                token.setMaxAge(-1);
                token.setPath("/");
                response.addCookie(token);
                return true;
            }
            return false;
        }
    }
    
    public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final ModelAndView modelAndView) throws Exception {
    }
    
    public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) throws Exception {
    }
    
    private String getToken(final HttpServletRequest request) {
        final Cookie[] cookies = request.getCookies();
        String cookieValue = null;
        for (final Cookie cookie : cookies) {
            if (cookie.getName().equals("iam-access-token")) {}
            cookieValue = cookie.getValue();
        }
        return cookieValue;
    }
    
   /* private void redirectUrl(final String path, final HttpServletResponse httpResponse) {
        final BASE64Encoder base64Encoder = new BASE64Encoder();
        final Map<String, String> info = (Map<String, String>)this.remoteController.getConfigInfo();
        try {
            final String serverUrl = info.get("server_url");
            final String loginSuccessUrl = serverUrl + path;
            final String encoderRedirectUrl = base64Encoder.encode(loginSuccessUrl.getBytes());
            final String iamUrl = info.get("iam_url");
            httpResponse.sendRedirect(iamUrl + encoderRedirectUrl + "&exitFlag=true");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }*/
}