liuxiaolong
2019-05-06 f99bc8c6a1d10610373738edd7d0aa0181c81d99
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
package com.cloud.user.controller;
 
import com.cloud.common.utils.MD5Util;
import com.cloud.model.common.TokenInfo;
import com.cloud.model.sys.AppUser;
import com.cloud.model.sys.constants.CredentialType;
import com.cloud.user.filter.AuthNoneIgnore;
import com.cloud.user.service.AppUserService;
import com.cloud.user.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
/*import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.util.OAuth2Utils;*/
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.web.bind.annotation.*;
 
import java.util.HashMap;
import java.util.Map;
 
@Slf4j
@RestController
@RequestMapping("/data/api-u")
@Api(value = "LoginController", description = "登陆、刷新token、退出")
public class LoginController {
 
    @Autowired
    private AppUserService userService;
 
    @Autowired
    private TokenService tokenService;
    /**
     * 系统登陆<br>
     * 根据用户名登录<br>
     * 采用oauth2密码模式获取access_token和refresh_token
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/sys/login")
    @ApiOperation(value = "系统登陆", notes = "采用oauth2密码模式获取access_token和refresh_token", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "登录名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "登录密码", required = true, dataType = "String", paramType = "query"),
    })
    @AuthNoneIgnore
    public TokenInfo login(String username, String password) {
 
        Map<String,Object> params = new HashMap<>();
        params.put("username", username);
        params.put("password", MD5Util.encode(password));
 
        AppUser user = userService.login(params);
        if(user == null){
            return new TokenInfo();
        }
        String token = tokenService.getToken(user);
 
        return new TokenInfo(token);
    }
 
    /**
     * 系统刷新refresh_token
     *
     * @param refresh_token
     * @return
     */
    @PostMapping("/sys/refresh_token")
    @ApiOperation(value = "系统刷新refresh_token", notes = "系统刷新refresh_token", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "refresh_token", value = "refresh_token", required = true, dataType = "String", paramType = "path"),
    })
    public TokenInfo refresh_token(String refresh_token, String access_token) {
 
        Map<String,Object> rs =  new HashMap<>();
 
        tokenService.refreshUserToken(refresh_token);
        return new TokenInfo(refresh_token);
    }
 
    /**
     * 退出
     *
     * @param access_token
     */
    @GetMapping("/sys/logout")
    @ApiOperation(value = "退出", notes = "退出", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "path"),
    })
    public void logout(String access_token, @RequestHeader(required = false, value = "Authorization") String token) {
        if (StringUtils.isBlank(access_token)) {
            if (StringUtils.isNoneBlank(token)) {
                access_token = token.substring(TokenInfo.BEARER_TYPE.length() + 1);
            }
        }
        tokenService.loginOff(access_token);
    }
}