liuxiaolong
2019-05-06 71af9c46c24b562acc00a71ec6c97c04eb048d9c
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
package com.cloud.device.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.cloud.common.exception.ApplicationException;
 
import com.cloud.common.utils.RequestUtil;
import com.cloud.model.sys.LoginAppUser;
import com.cloud.device.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
 
/**
 * @author lp
 */
@Slf4j
@Component
@Service
public class TokenServiceImpl implements TokenService {
 
    @Autowired
    private  StringRedisTemplate stringRedisTemplate;
 
 
    @Override
    public LoginAppUser currentUser() {
 
        // 请求参数中包含access_token参数
        String token = RequestUtil.getTokenByParam();
        if(token == null){
            throw new ApplicationException("not found token");
        }
        String sUser = stringRedisTemplate.opsForValue().get(token);
        if(StringUtils.isNotEmpty(sUser))
            return JSONObject.parseObject(sUser, LoginAppUser.class);
 
        return new LoginAppUser();
    }
 
    @Override
    public LoginAppUser currentUserByToken(String token) {
        String sUser = stringRedisTemplate.opsForValue().get(token);
        LoginAppUser user = JSONObject.parseObject(sUser, LoginAppUser.class);
 
        return user;
    }
 
 
 
}