地质所 沉降监测网建设项目
zmk
2024-05-16 6cc8b13ee47303a907f8e57b018406c46d8928e4
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
package com.javaweb.web.controller.system;
 
import com.javaweb.cms.util.CmsConstants;
import com.javaweb.common.constant.RSAKey;
import com.javaweb.common.core.controller.BaseController;
import com.javaweb.common.core.domain.AjaxResult;
import com.javaweb.common.utils.ServletUtils;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.framework.util.ShiroUtils;
import com.javaweb.system.domain.SysUser;
import com.javaweb.system.service.ISysConfigService;
 
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
 
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * 登录验证
 * 
 * @author javaweb
 */
@Controller
public class SysLoginController extends BaseController
{
    @Autowired
    private ISysConfigService configService;
 
    private String getLoginPageCode(){
        return configService.selectConfigByKey(CmsConstants.KEY_LOGIN_PAGE);
    }
    @GetMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response)
    {
        SysUser user = ShiroUtils.getSysUser();
        if(user!=null){
            return "redirect:/index";
        }
        // 如果是Ajax请求,返回Json字符串。
        if (ServletUtils.isAjaxRequest(request))
        {
            return ServletUtils.renderString(response, "{\"code\":\"1\",\"msg\":\"未登录或登录超时。请重新登录\"}");
        }
        String loginPageCode=getLoginPageCode();
        if(StringUtils.isEmpty(loginPageCode)){
            return "login";
        }else{
            //配置了login.page参数
            return "loginPage/"+loginPageCode+"/login";//页面在cms模块loginPage文件夹下
        }
 
    }
    
    @PostMapping("/login")
    @ResponseBody
    public AjaxResult ajaxLogin(String username, String password, Boolean rememberMe)
    {
       
        RSA  rsa = new RSA(RSAKey.privatekey, null);
        
        username = new String(rsa.decryptStr(username, KeyType.PrivateKey));
        password = new String(rsa.decryptStr(password, KeyType.PrivateKey));
        
     
        
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        Subject subject = SecurityUtils.getSubject();
        try
        {
            subject.login(token);
            ServletUtils.setLoginCookie(username, password, rememberMe);
            return success();
        }
        catch (AuthenticationException e)
        {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage()))
            {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }
 
    @GetMapping("/unauth")
    public String unauth()
    {
        return "error/unauth";
    }
}