AuthenticationController.java 4.6 KB
Newer Older
1
package me.zhengjie.modules.security.rest;
郑杰 已提交
2

3
import cn.hutool.core.util.IdUtil;
D
dqjdda 已提交
4
import com.wf.captcha.ArithmeticCaptcha;
5 6
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
郑杰 已提交
7
import lombok.extern.slf4j.Slf4j;
8
import me.zhengjie.aop.log.Log;
9 10
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.monitor.service.RedisService;
D
dqjdda 已提交
11 12
import me.zhengjie.modules.security.security.AuthInfo;
import me.zhengjie.modules.security.security.AuthUser;
13
import me.zhengjie.modules.security.security.ImgResult;
14
import me.zhengjie.modules.security.security.JwtUser;
15
import me.zhengjie.modules.security.service.OnlineUserService;
16 17
import me.zhengjie.utils.EncryptUtils;
import me.zhengjie.modules.security.utils.JwtTokenUtil;
Z
zhengjie 已提交
18
import me.zhengjie.utils.SecurityUtils;
19
import me.zhengjie.utils.StringUtils;
郑杰 已提交
20
import org.springframework.beans.factory.annotation.Qualifier;
D
dqjdda 已提交
21
import org.springframework.beans.factory.annotation.Value;
22
import org.springframework.http.HttpStatus;
郑杰 已提交
23 24 25
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.core.userdetails.UserDetailsService;
26
import org.springframework.validation.annotation.Validated;
郑杰 已提交
27
import org.springframework.web.bind.annotation.*;
28
import javax.servlet.http.HttpServletRequest;
郑杰 已提交
29 30

/**
31
 * @author Zheng Jie
郑杰 已提交
32 33 34 35 36
 * @date 2018-11-23
 * 授权、根据token获取用户详细信息
 */
@Slf4j
@RestController
37 38
@RequestMapping("/auth")
@Api(tags = "系统:系统授权接口")
郑杰 已提交
39 40
public class AuthenticationController {

D
dqjdda 已提交
41 42
    @Value("${jwt.codeKey}")
    private String codeKey;
D
dqjdda 已提交
43

44
    private final JwtTokenUtil jwtTokenUtil;
郑杰 已提交
45

46
    private final RedisService redisService;
47

48 49
    private final UserDetailsService userDetailsService;

50 51 52
    private final OnlineUserService onlineUserService;

    public AuthenticationController(JwtTokenUtil jwtTokenUtil, RedisService redisService, @Qualifier("jwtUserDetailsService") UserDetailsService userDetailsService, OnlineUserService onlineUserService) {
53 54 55
        this.jwtTokenUtil = jwtTokenUtil;
        this.redisService = redisService;
        this.userDetailsService = userDetailsService;
56
        this.onlineUserService = onlineUserService;
57
    }
郑杰 已提交
58

59
    @Log("用户登录")
60 61
    @ApiOperation("登录授权")
    @PostMapping(value = "/login")
62
    public ResponseEntity login(@Validated @RequestBody AuthUser authorizationUser, HttpServletRequest request){
郑杰 已提交
63

64 65 66 67 68 69 70 71 72 73
        // 查询验证码
        String code = redisService.getCodeVal(authorizationUser.getUuid());
        // 清除验证码
        redisService.delete(authorizationUser.getUuid());
        if (StringUtils.isBlank(code)) {
            throw new BadRequestException("验证码已过期");
        }
        if (StringUtils.isBlank(authorizationUser.getCode()) || !authorizationUser.getCode().equalsIgnoreCase(code)) {
            throw new BadRequestException("验证码错误");
        }
74
        final JwtUser jwtUser = (JwtUser) userDetailsService.loadUserByUsername(authorizationUser.getUsername());
郑杰 已提交
75

76
        if(!jwtUser.getPassword().equals(EncryptUtils.encryptPassword(authorizationUser.getPassword()))){
郑杰 已提交
77 78 79
            throw new AccountExpiredException("密码错误");
        }

80
        if(!jwtUser.isEnabled()){
郑杰 已提交
81 82 83
            throw new AccountExpiredException("账号已停用,请联系管理员");
        }
        // 生成令牌
84
        final String token = jwtTokenUtil.generateToken(jwtUser);
85 86
        // 保存在线信息
        onlineUserService.save(jwtUser, token, request);
郑杰 已提交
87
        // 返回 token
D
dqjdda 已提交
88
        return ResponseEntity.ok(new AuthInfo(token,jwtUser));
郑杰 已提交
89 90
    }

91 92
    @ApiOperation("获取用户信息")
    @GetMapping(value = "/info")
93
    public ResponseEntity getUserInfo(){
Z
zhengjie 已提交
94
        JwtUser jwtUser = (JwtUser)userDetailsService.loadUserByUsername(SecurityUtils.getUsername());
郑杰 已提交
95 96
        return ResponseEntity.ok(jwtUser);
    }
97

98
    @ApiOperation("获取验证码")
D
dqjdda 已提交
99 100 101 102 103 104 105 106
    @GetMapping(value = "/code")
    public ImgResult getCode(){
        // 算术类型 https://gitee.com/whvse/EasyCaptcha
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(111, 36);
        // 几位数运算,默认是两位
        captcha.setLen(2);
        // 获取运算的结果:5
        String result = captcha.text();
D
dqjdda 已提交
107
        String uuid = codeKey + IdUtil.simpleUUID();
D
dqjdda 已提交
108 109
        redisService.saveCode(uuid,result);
        return new ImgResult(captcha.toBase64(),uuid);
110
    }
111 112 113 114 115 116 117

    @ApiOperation("退出登录")
    @DeleteMapping(value = "/logout")
    public ResponseEntity logout(HttpServletRequest request){
        onlineUserService.logout(jwtTokenUtil.getToken(request));
        return new ResponseEntity(HttpStatus.OK);
    }
郑杰 已提交
118
}