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

3 4
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
郑杰 已提交
5
import lombok.extern.slf4j.Slf4j;
6
import me.zhengjie.aop.log.Log;
7 8
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.monitor.service.RedisService;
9
import me.zhengjie.modules.security.security.AuthenticationInfo;
10
import me.zhengjie.modules.security.security.AuthorizationUser;
11
import me.zhengjie.modules.security.security.ImgResult;
12
import me.zhengjie.modules.security.security.JwtUser;
13
import me.zhengjie.modules.security.utils.VerifyCodeUtils;
14 15
import me.zhengjie.utils.EncryptUtils;
import me.zhengjie.modules.security.utils.JwtTokenUtil;
Z
zhengjie 已提交
16
import me.zhengjie.utils.SecurityUtils;
17
import me.zhengjie.utils.StringUtils;
郑杰 已提交
18 19 20 21 22 23
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.core.userdetails.UserDetailsService;
24
import org.springframework.validation.annotation.Validated;
郑杰 已提交
25
import org.springframework.web.bind.annotation.*;
26 27 28
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
郑杰 已提交
29 30

/**
31
 * @author Zheng Jie
郑杰 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45
 * @date 2018-11-23
 * 授权、根据token获取用户详细信息
 */
@Slf4j
@RestController
@RequestMapping("auth")
public class AuthenticationController {

    @Value("${jwt.header}")
    private String tokenHeader;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

46 47 48
    @Autowired
    private RedisService redisService;

郑杰 已提交
49 50 51 52 53 54 55 56 57
    @Autowired
    @Qualifier("jwtUserDetailsService")
    private UserDetailsService userDetailsService;

    /**
     * 登录授权
     * @param authorizationUser
     * @return
     */
58
    @Log("用户登录")
郑杰 已提交
59
    @PostMapping(value = "${jwt.auth.path}")
60
    public ResponseEntity login(@Validated @RequestBody AuthorizationUser authorizationUser){
郑杰 已提交
61

62 63 64 65 66 67 68 69 70 71
        // 查询验证码
        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("验证码错误");
        }
72
        final JwtUser jwtUser = (JwtUser) userDetailsService.loadUserByUsername(authorizationUser.getUsername());
郑杰 已提交
73

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

78
        if(!jwtUser.isEnabled()){
郑杰 已提交
79 80 81 82
            throw new AccountExpiredException("账号已停用,请联系管理员");
        }

        // 生成令牌
83
        final String token = jwtTokenUtil.generateToken(jwtUser);
郑杰 已提交
84 85

        // 返回 token
86
        return ResponseEntity.ok(new AuthenticationInfo(token,jwtUser));
郑杰 已提交
87 88 89 90 91 92 93
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping(value = "${jwt.auth.account}")
94
    public ResponseEntity getUserInfo(){
Z
zhengjie 已提交
95
        JwtUser jwtUser = (JwtUser)userDetailsService.loadUserByUsername(SecurityUtils.getUsername());
郑杰 已提交
96 97
        return ResponseEntity.ok(jwtUser);
    }
98 99 100 101 102 103 104

    /**
     * 获取验证码
     */
    @GetMapping(value = "vCode")
    public ImgResult getCode(HttpServletResponse response) throws IOException {

105 106
        //生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
107
        String uuid = IdUtil.simpleUUID();
108 109 110
        redisService.saveCode(uuid,verifyCode);
        // 生成图片
        int w = 111, h = 36;
111
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
112
        VerifyCodeUtils.outputImage(w, h, stream, verifyCode);
113 114 115 116 117 118 119 120 121
        try {
            return new ImgResult(Base64.encode(stream.toByteArray()),uuid);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            stream.close();
        }
    }
郑杰 已提交
122
}