zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.changhong.epc.zuul.rest;
 
import com.changhong.epc.constter.base.BaseConst;
import com.changhong.epc.constter.system.PrivateRestConst;
import com.changhong.epc.constter.system.UserInfo;
import com.changhong.epc.constter.system.UserInfo.LogInInfo;
import com.changhong.epc.constter.system.prop.ChangHongProperties;
import com.changhong.epc.rely.api.service.UserService;
import com.changhong.epc.rely.api.service.impl.UserServiceImpl;
import com.changhong.epc.zuul.filter.Des1;
import com.changhong.epc.zuul.service.LoginService;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
import com.iemsoft.framework.cloud.redis.service.CacheUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.WebUtils;
 
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Objects;
 
@Slf4j
@RestController
public class LoginRest implements PrivateRestConst{
    
    @Autowired
    private ChangHongProperties changHongProperties;
 
    @Resource
    private UserServiceImpl.UserInfoServiceImpl userInfoService;
    
    @Resource
    private LoginService loginService;
    
    @GetMapping("/login")
    public void login(@RequestParam String permType, HttpServletRequest request, HttpServletResponse response) throws Exception{
        if(WebUtils.getCookie(request, TOKEN) == null){
            // System.err.println("LoginRest kongde ");
            return;
        }
        /*
         * 获得登录信息
         */
        UserInfo.LogInInfo loginInfo=new LogInInfo();
        loginInfo.setToken(WebUtils.getCookie(request, TOKEN).getValue());
        loginInfo.setOpenId(WebUtils.getCookie(request, OPEN_ID).getValue());
        // System.out.println("进入:"+JSONTool.toJson(loginInfo));
        UserInfo user = loginService.getUserInfo(loginInfo);
        log.debug("当前用户:"+JSONTool.toJson(user));
        /*
         * 跳转默认页
         */
        response.sendRedirect(
                new URIBuilder(changHongProperties.getLoginCallBack())
                        .setParameter("permType", permType)
                        .toString()
        );
                
    }
 
    @PostMapping("/token/validate")
    public ResultReport tokenValidate(UserInfo.LogInInfo logInInfo){
        String key = String.format("token_%s"
                ,logInInfo.getOpenId());
        if(!Objects.equals(CacheUtils.get(key, String.class), logInInfo.getToken())){
            return new ResultReport();
        }
        return new ResultReport(1, true);
    }
 
    @Resource
    private UserService userService;
 
    @PostMapping("/getUserInfo")
    public UserInfo getUserInfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
        /*
         * 获得登录信息
         */
        UserInfo.LogInInfo loginInfo=new LogInInfo();
        String token = URLDecoder.decode(WebUtils.getCookie(request, TOKEN).getValue(),"UTF-8").replaceAll(" ","+");
        loginInfo.setToken(Des1.desDecrypt(token,Des1.SECRETKEY));
        String openId = URLDecoder.decode(WebUtils.getCookie(request, OPEN_ID).getValue(),"UTF-8").replaceAll(" ","+");
        loginInfo.setOpenId(Des1.desDecrypt(openId, Des1.SECRETKEY));
 
        ThreadData.set(BaseConst.TOKEN, token);
        ThreadData.set(BaseConst.OPEN_ID, openId);
 
        // System.out.println("进入:"+JSONTool.toJson(loginInfo));
        log.debug("开始添加cookie缓存,有效期1800秒");
        String key = String.format("token_%s"
                ,loginInfo.getOpenId());
        log.debug("当前缓存中登陆token:"+key);
        String time = SpringUtil.getBean(ChangHongProperties.class).getTokenTimeOut();
        log.debug("当前超时时间:"+time);
        CacheUtils.set(key, loginInfo.getToken(), Long.parseLong(time));
        // System.out.println("刚存得cookie:"+CacheUtils.get(key,String.class));
        //加密cookie
        Cookie tokenC = new Cookie(TOKEN, URLEncoder.encode(Des1.desEncryp(loginInfo.getToken(), Des1.SECRETKEY), "UTF-8"));
        log.debug("加密后token:"+Des1.desEncryp(loginInfo.getToken(), Des1.SECRETKEY));
        tokenC.setPath("/");
        tokenC.setMaxAge(-1);
        Cookie openIdC = new Cookie(OPEN_ID, URLEncoder.encode(Des1.desEncryp(loginInfo.getOpenId(), Des1.SECRETKEY), "UTF-8"));
        log.debug("加密后openId:"+Des1.desEncryp(loginInfo.getOpenId(), Des1.SECRETKEY));
        openIdC.setPath("/");
        openIdC.setMaxAge(-1);
        response.addCookie(tokenC);
        response.addCookie(openIdC);
        return loginService.getUserInfo(loginInfo);
                
    }
 
 
    @GetMapping("/")
    public void locationLogin(HttpServletResponse httpServletResponse)throws IOException {
        httpServletResponse.sendRedirect(changHongProperties.getHomeUrl());
    }
 
 
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ResultReport{
 
        private Integer userId;
 
        private Boolean display;
 
    }
 
}