AuthController.java
2.27 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
65
66
67
68
69
70
71
package com.meishu.controller;
import cn.hutool.core.lang.UUID;
import com.meishu.model.AuthTicket;
import com.meishu.service.AuthTicketService;
import com.meishu.util.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
/**
* 登陆 获取ticket
*/
@RestController
@RequestMapping("/auth")
public class AuthController {
@Resource
private AuthTicketService ticketService;
// @Value("${auth.ticket-expire}")
// private Integer expireSec;
// 模拟登录
@PostMapping("/login")
public Result login(String username, String password, HttpSession session) {
if (!"admin".equals(username) || !"123456".equals(password)) {
return Result.fail("账号密码错误");
}
session.setAttribute("user", username);
return Result.success("登录成功");
}
// 获取免登Ticket
@GetMapping("/getTicket")
public Result getTicket(String platform, HttpSession session) {
String user = (String) session.getAttribute("user");
if (user == null) return Result.fail("请先登录");
String ticket = UUID.fastUUID().toString(true);
LocalDateTime expire = LocalDateTime.now().plusSeconds(300);
AuthTicket authTicket = new AuthTicket();
authTicket.setTicket(ticket);
authTicket.setUsername(user);
authTicket.setPlatform(platform);
authTicket.setExpireTime(expire);
authTicket.setUsed(0);
ticketService.save(authTicket);
Map<String, String> map = new HashMap<>();
map.put("ticket", ticket);
map.put("redirectUrl", getRedirect(platform));
return Result.success(map);
}
private String getRedirect(String platform) {
switch (platform) {
case "erp": return "https://erp.xxx.com/callback";
case "crm": return "https://crm.xxx.com/callback";
default: return "";
}
}
}