huanghongfa
2020-12-20 def38240262b4403377d4c16beac3ea048f1e658
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
package com.panzhihua.auth.service.impl;
 
import com.panzhihua.auth.service.LoginService;
import com.panzhihua.common.constants.TokenConstant;
import com.panzhihua.common.model.vos.LoginReturnVO;
import com.panzhihua.common.utlis.JWTTokenUtil;
import com.panzhihua.common.constants.UserConstants;
import com.panzhihua.common.model.vos.LoginUserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: token
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-11-19 17:07
 **/
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private AuthenticationManager authenticationManager;
    @Autowired
    private StringRedisTemplate redisTemplate;
 
    /**
     * 微信小程序登录
     *
     * @param openId 微信标识
     * @return jwt
     */
    @Override
    public LoginReturnVO loginApplets(String openId) {
        Authentication authentication = null;
        authentication = authenticationManager
                .authenticate(new UsernamePasswordAuthenticationToken(openId+"_1", UserConstants.PASSWORD));
        LoginUserInfoVO loginUser = (LoginUserInfoVO) authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO=new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
    /**
     * 小程序用户登出
     *
     * @param token 登录用户token
     */
    @Override
    public void logoutApplets(String token) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        token= token.replaceAll(TokenConstant.TOKEN_PRE,"");
        valueOperations.set(UserConstants.LOGOUT_TOKEN+token,token, TokenConstant.EXPIRETIME, TimeUnit.MINUTES);
    }
 
    /**
     * 运营后台登录
     *
     * @param account  账户
     * @param password 密码
     * @return 登录结果
     */
    @Override
    public LoginReturnVO loginAppletsBackStage(String account, String password) {
        Authentication authentication = null;
        authentication = authenticationManager
                .authenticate(new UsernamePasswordAuthenticationToken(account+"_2", password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO) authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO=new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
    /**
     * 社区后台登录
     *
     * @param account  账户
     * @param password 密码
     * @return 登录结果
     */
    @Override
    public LoginReturnVO loginCommunityBackage(String account, String password) {
        Authentication authentication = null;
        authentication = authenticationManager
                .authenticate(new UsernamePasswordAuthenticationToken(account+"_3", password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO) authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO=new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
 
}