Helius
2020-05-13 895dedb7341da20fb815010630a99988005b81a1
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
package com.xcong.excoin.common.system.controller;
 
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
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.utils.RedisUtils;
import io.swagger.annotations.Api;
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 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 {
 
    @Value("${rsa.private_key}")
    private String privateKey;
 
    @Resource
    private AuthenticationManagerBuilder authenticationManagerBuilder;
 
    @Resource
    private RedisUtils redisUtils;
 
    @PostMapping("/login")
    public Result login(@RequestBody @Validated LoginDto loginDto) {
        UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(loginDto.getUsername(), loginDto.getPassword());
        Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authToken);
        String token = IdUtil.simpleUUID();
        LoginUserBean loginUserBean = (LoginUserBean) authentication.getPrincipal();
        redisUtils.set(AppContants.APP_LOGIN_PREFIX + token, JSONObject.toJSONString(loginUserBean), 300000);
        Map<String, Object> authInfo = new HashMap<String, Object>(2){
            {
                put("token", token);
                put("user", loginUserBean);
            }
        };
        return Result.ok("success", authInfo);
    }
}