jinpengyong
2020-10-21 d3c5982218db1413c8f609f1f51b49c49a2db496
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
package com.moral.controller;
 
import java.util.HashMap;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.moral.service.TokenService;
 
@RestController
public class TokenControllers {
 
    @Resource
    private TokenService tokenService;
 
    @GetMapping("oauth/token/{type}/{username}/{password}")
    public Map<String, Object> getAuthToken(@PathVariable("username") String username,
            @PathVariable("password") String password, @PathVariable("type") String type, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password) || ObjectUtils.isEmpty(type)) {
            result.put("msg", "参数输入不合法");
        } else {
            String url = request.getRequestURL().toString().replace(request.getRequestURI(), "") + request.getContextPath();
            String realPath = request.getServletContext().getRealPath("/");
            result = tokenService.getAuthToken(type, username, password, url);
        }
        return result;
    }
 
    @PostMapping("oauth/token/{refresh_token}")
    public Map<String, Object> getAuthToken(@PathVariable("refresh_token") String refresh_token,HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        if (ObjectUtils.isEmpty(refresh_token)) {
            result.put("msg", "参数输入不合法");
        } else {
            String url = request.getRequestURL().toString().replace(request.getRequestURI(), "") + request.getContextPath();
            result = tokenService.getAuthToken(refresh_token, url);
        }
        return result;
    }
}