Helius
2021-06-16 5728be2af515b2200e782aa201ca5d4d67d9ea47
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.ibeetl.admin.core.web;
 
import com.ibeetl.admin.core.entity.CoreOrg;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.rbac.UserLoginInfo;
import com.ibeetl.admin.core.rbac.tree.MenuItem;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.service.CoreUserService;
import com.ibeetl.admin.core.util.HttpRequestLocal;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.util.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
 
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Random;
 
//@Controller
@RestController
@Slf4j
public class IndexController {
 
    @Autowired
    CorePlatformService platformService;
 
    @Autowired
    CoreUserService userService;
 
    @Autowired
    HttpRequestLocal httpRequestLocal;
 
 
 
 
    @RequestMapping("/")
    public ModelAndView login() {
        ModelAndView view = new ModelAndView("/login.html");
        return view;
    }
 
    /**
     * 登陆验证码
     */
    @RequestMapping("/verify")
    void verifyCode(HttpServletRequest req, HttpServletResponse resp)throws IOException {
        // 创建图片
        int width = 80;
        int height = 40;
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
 
//        创建图层获得画板
        Graphics g = image.getGraphics();
//        确认画笔颜色
        g.setColor(Color.BLACK);
        //填充矩形
        g.fillRect(0,0,width-2,height-2);
        // String dataString="ABCDEFGHIJHLMNOPQRSTUVWXYZabcdefghijklmnopqlstuvwxyz1234567890";
        String dataString="1234567890";
        //设置字体
        g.setFont(new Font("宋体",Font.BOLD,30));
        //缓存随机生成的字符
        StringBuffer buf = new StringBuffer();
        Random random = new Random();
 
        //截取字符
        for(int i=0;i<4;i++){
            //设置字体颜色  随机
            g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            //获得一个随机字符
            int index = random.nextInt(10);
            String str = dataString.substring(index,index+1);
            //加入画板
            g.drawString(str,20*i,30);
            buf.append(str);
        }
 
 
        //干扰线
        for(int i=0;i<10;i++){
            g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            g.setColor(new Color(16,16,16));
            g.drawLine(random.nextInt(width),random.nextInt(height),random.nextInt(width),random.nextInt(height)) ;
        }
        HttpSession session = req.getSession();
        log.info("获取的验证码是:  {}" , buf.toString());
        session.setAttribute("cap",buf.toString());
 
        //设置响应类型
        resp.setContentType("image/jpeg");
        //将图片发送给浏览器
        ImageIO.write(image,"jpg",resp.getOutputStream());
    }
 
    @PostMapping("/login.do")
    public ModelAndView login(String code, String password, String verify, HttpServletRequest request) {
 
        String ipAddr = request.getRequestURL().toString();
        if(!request.getSession().getAttribute("cap").toString().equalsIgnoreCase(verify)){
            if(ipAddr.indexOf("192.168.0.100") != -1){
 
            }else{
                throw new PlatformException("验证码错误");
            }
        }
        UserLoginInfo info = userService.login(code, password);
        if (info == null) {
            throw new PlatformException("用户名密码错");
        }
        CoreUser user = info.getUser();
        CoreOrg currentOrg = info.getOrgs().get(0);
        for (CoreOrg org : info.getOrgs()) {
            if (org.getId() == user.getOrgId()) {
                currentOrg = org;
                break;
            }
        }
 
        info.setCurrentOrg(currentOrg);
        // 记录登录信息到session
        this.platformService.setLoginUser(info.getUser(), info.getCurrentOrg(), info.getOrgs());
        ModelAndView view = new ModelAndView("redirect:/index.do");
        return view;
    }
    
    @RequestMapping("/index.do")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/index.html");
        CoreUser currentUser = platformService.getCurrentUser();
        Long orgId = platformService.getCurrentOrgId();
        MenuItem menuItem = platformService.getMenuItem(currentUser.getId(), orgId);
        view.addObject("menus", menuItem);
        return view;
    }
 
    @RequestMapping("/logout.do")
    public ModelAndView logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Enumeration eum = session.getAttributeNames();
        while(eum.hasMoreElements()) {
            String key = (String)eum.nextElement();
            session.removeAttribute(key);
        }
        ModelAndView view = new ModelAndView("redirect:/");
        return view;
    }
    @RequestMapping("/changeOrg.do")
    public ModelAndView changeOrg(HttpServletRequest request,Long orgId) {
        platformService.changeOrg(orgId);
        ModelAndView view = new ModelAndView("redirect:/index.do");
        return view;
    }
 
}