Login.java
2.17 KB
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
package com.huaheng.api;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
@Api(tags = {"Login"}, description = "登陆接口")
public class Login extends BaseController {
@PostMapping("/login")
@ResponseBody
public AjaxResult ajaxLogin(String username, String password, String warehouse, Boolean rememberMe)
{
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
{
return error("用户名和密码不能为空");
}
if (StringUtils.isEmpty(warehouse))
{
return error("请选择仓库");
}
UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
Subject subject = SecurityUtils.getSubject();
try
{
subject.login(token);
}
catch (AuthenticationException e)
{
String msg = "用户或密码错误";
if (StringUtils.isNotEmpty(e.getMessage()))
{
msg = e.getMessage();
}
return error(msg);
}
String[] warehouseArray = warehouse.split(",");
ShiroUtils.getUser().setWarehouseId(Integer.valueOf(warehouseArray[0]));
ShiroUtils.getUser().setWarehouseCode(warehouseArray[1]);
return success();
}
@PostMapping("/heartbeat")
@ApiOperation("心跳接口,用于延长cookie有效期")
public AjaxResult heartbeat()
{
return AjaxResult.success("success");
}
}