zainali5120
2021-03-17 cbcbaa6a1e96982b73091b0ac8927fafa27c6a39
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
package com.xcong.excoin.common.system.controller;
 
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.common.LoginUserUtils;
import com.xcong.excoin.common.annotations.SubmitRepeat;
import com.xcong.excoin.common.contants.AppContants;
import com.xcong.excoin.common.response.Result;
import com.xcong.excoin.common.system.bean.LoginUserBean;
import com.xcong.excoin.common.system.dto.LoginDto;
import com.xcong.excoin.common.system.dto.RegisterDto;
import com.xcong.excoin.common.system.vo.MemberInfoVo;
import com.xcong.excoin.configurations.properties.ApplicationProperties;
import com.xcong.excoin.configurations.properties.SecurityProperties;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.member.service.MemberService;
import com.xcong.excoin.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @Author wzy
 * @Date 2020/5/11
 * @email wangdoubleone@gmail.com
 * @Version V1.0
 **/
@Slf4j
@Api(value = "登陆注册类", tags = "登陆注册类")
@RestController
@RequestMapping(value = "/")
public class LoginController {
 
    @Resource
    private MemberService memberservice;
 
    @Resource
    private ApplicationProperties applicationProperties;
 
    @Resource
    private SecurityProperties securityProperties;
 
    @Resource
    private AuthenticationManagerBuilder authenticationManagerBuilder;
 
    @Resource
    private RedisUtils redisUtils;
 
    @ApiOperation(value = "登陆接口", notes = "登陆接口")
    @PostMapping("/login")
    public Result login(@RequestBody @Validated LoginDto loginDto, HttpServletRequest request) {
        // 将账号密码交给spring security验证,并调用userServiceDetails
        UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(loginDto.getUsername(), SecureUtil.md5(loginDto.getPassword()));
        Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authToken);
 
        // 获取当前验证过后的用户
        LoginUserBean loginUserBean = (LoginUserBean) authentication.getPrincipal();
 
        // 生成UUID作为token
        String token = IdUtil.simpleUUID();
        String redisToken = "";
        String redisMember = "";
        if (LoginUserUtils.isBrowser(request)) {
            redisToken = AppContants.PC_LOGIN_PREFIX + token;
            redisMember = AppContants.PC_LOGIN_PREFIX + loginUserBean.getMemberEntity().getId();
        } else {
            redisToken = AppContants.APP_LOGIN_PREFIX + token;
            redisMember = AppContants.APP_LOGIN_PREFIX + loginUserBean.getMemberEntity().getId();
        }
 
        if (StrUtil.isNotBlank(redisUtils.getString(redisMember))) {
            if (redisMember.contains(AppContants.APP_LOGIN_PREFIX)) {
                redisUtils.del(AppContants.APP_LOGIN_PREFIX + redisUtils.getString(redisMember));
            } else {
                redisUtils.del(AppContants.PC_LOGIN_PREFIX + redisUtils.getString(redisMember));
            }
        }
        redisUtils.set(redisToken, JSONObject.toJSONString(loginUserBean.getMemberEntity()), applicationProperties.getRedisExpire());
        redisUtils.set(redisMember, token);
        Map<String, Object> authInfo = new HashMap<>();
        //获取返回的个人信息
        MemberInfoVo memberInfoVo = new MemberInfoVo();
        MemberEntity memberEntity = loginUserBean.getMemberEntity();
        memberInfoVo.setEmail(memberEntity.getEmail());
        memberInfoVo.setPhone(memberEntity.getPhone());
        memberInfoVo.setInviteId(memberEntity.getInviteId());
        memberInfoVo.setFingerprintState(memberEntity.getFingerprintState());
        // 开启debug模式,则将加密后的token返回
        if (applicationProperties.isDebug()) {
            authInfo.put("token", token);
            authInfo.put("rsaToken", AppContants.TOKEN_START_WITH + generateAsaToken(token));
            authInfo.put("user", memberInfoVo);
        } else {
            authInfo.put("token", token);
            authInfo.put("user", memberInfoVo);
        }
        return Result.ok("success", authInfo);
    }
 
    public String generateAsaToken(String token) {
        RSA rsa = new RSA(null, securityProperties.getPublicKey());
        return rsa.encryptBase64(token + "_" + System.currentTimeMillis(), KeyType.PublicKey);
    }
 
    @SubmitRepeat
    @ApiOperation(value = "app注册接口", notes = "app注册接口,验证码必须输入可默认为123456")
    @PostMapping(value = "/register")
    public Result register(@RequestBody @Validated RegisterDto registerDto) {
        return memberservice.register(registerDto);
    }
 
}