lyh
3 天以前 3ce27b7faf8850d101a1511a685250fe562dca18
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.lxzn.auth.controller;
 
import com.lxzn.api.auth.AuthControllerApi;
import com.lxzn.auth.RedisUtil;
import com.lxzn.auth.security.AuthService;
import com.lxzn.framework.domain.auth.ext.AuthToken;
import com.lxzn.framework.domain.auth.request.LoginRequest;
import com.lxzn.framework.domain.auth.response.JwtResult;
import com.lxzn.framework.domain.auth.response.LoginResult;
import com.lxzn.framework.domain.ucenter.response.UcenterCode;
import com.lxzn.framework.exception.ExceptionCast;
import com.lxzn.framework.model.response.CommonCode;
import com.lxzn.framework.model.response.ResponseResult;
import com.lxzn.framework.utils.CookieUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
 
/**
 * @author Administrator
 * @version 1.0
 **/
@RestController
@RequestMapping("/auth")
public class AuthController implements AuthControllerApi {
 
    @Value("${auth.clientId}")
    private String clientId;
    @Value("${auth.clientSecret}")
    private String clientSecret;
    @Value("${auth.cookieDomain}")
    private String cookieDomain;
    @Value("${auth.cookieMaxAge}")
    private int cookieMaxAge;
 
    @Autowired
    private AuthService authService;
    @Autowired
    private RedisUtil redisUtil;
    @Value("${auth.cookieName}")
    private String cookieName;
 
    @Override
    @PostMapping("/user/login")
    public LoginResult login(LoginRequest loginRequest) {
        if(loginRequest == null || StringUtils.isEmpty(loginRequest.getUsername())){
            ExceptionCast.cast(UcenterCode.UCENTER_USERNAME_NONE);
        }
        if(loginRequest == null || StringUtils.isEmpty(loginRequest.getPassword())){
            ExceptionCast.cast(UcenterCode.UCENTER_PASSWORD_NONE);
        }
        //账号
        String username = loginRequest.getUsername();
        //密码
        String password = loginRequest.getPassword();
 
        //申请令牌
        LoginResult loginResult =  authService.login(username,password,clientId,clientSecret);
 
        //用户身份令牌
        String access_token = loginResult.getToken();
        //将令牌存储到cookie
        this.saveCookie(access_token);
 
        return loginResult;
    }
 
    //将令牌存储到cookie
    private void saveCookie(String token){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        //HttpServletResponse response,String domain,String path, String name, String value, int maxAge,boolean httpOnly
        CookieUtil.addCookie(response,cookieDomain,"/",cookieName,token,cookieMaxAge,false);
 
    }
    //从cookie删除token
    private void clearCookie(String token){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        //HttpServletResponse response,String domain,String path, String name, String value, int maxAge,boolean httpOnly
        CookieUtil.addCookie(response,cookieDomain,"/",cookieName,token,0,false);
 
    }
 
    //取出cookie中的身份令牌
    private String getTokenFormCookie(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Map<String, String> map = CookieUtil.readCookie(request, cookieName);
        if(map!=null && map.containsKey(cookieName)){
            return map.get(cookieName);
        }
        return null;
    }
 
    @Override
    @PostMapping("/user/logout")
    public ResponseResult logout() {
        //取出cookie中的用户身份令牌
        String uid = getTokenFormCookie();
        //删除redis中的token
        boolean result = redisUtil.delToken(uid);
        //清除cookie
        this.clearCookie(uid);
        return new ResponseResult(CommonCode.SUCCESS);
    }
 
    @Override
    @GetMapping("/user/jwt")
    public JwtResult userJwt() {
        //取出cookie中的用户身份令牌
        String uid = getTokenFormCookie();
        if(uid == null){
            return new JwtResult(CommonCode.FAIL,null);
        }
 
        //拿身份令牌从redis中查询jwt令牌
        AuthToken userToken = redisUtil.getUserToken(uid);
        if(userToken!=null){
            //将jwt令牌返回给用户
            String jwt_token = userToken.getJwt_token();
            return new JwtResult(CommonCode.SUCCESS,jwt_token);
        }
        return null;
    }
}