• 用户登录信息存session


    UserInfoHolder --也可以换成user
    /**
    *登录页----用户登录
    * */
    //@ACL
    @PostMapping("/userLogin")
    public BaseResponse userLogin(@RequestBody User user, HttpServletRequest request) {
    BaseResponse response = new BaseResponse();
    String value = null;
    try{
    if(user!=null){
    //判断是否存在,是否是管理员
    List<User> ls = userService.isexist(user.getUserid());
    if(ls.size()>0){
    User userDt = userService.check(user.getUserid(),user.getPassword());
    if(userDt!=null){
    UserRole role = roleService.astrict(user.getUserid());
    if(role!=null){
    // 检验密码
    if (!userDt.getPassword().equals(user.getPassword())) {
    response.setErrorMsg("用户名密码不匹配!");
    response.setSuccess(false);
    response.setErrorCode(ErrorCode.WRONG_USERNAME_OR_PASSWORD.getCode());
    }else{
    // 检验密码
    if (!userDt.getPassword().equals(user.getPassword())) {
    response.setErrorMsg("用户名密码不匹配!");
    response.setSuccess(false);
    response.setErrorCode(ErrorCode.WRONG_USERNAME_OR_PASSWORD.getCode());
    } else {
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
    for (Cookie cookie : cookies) {
    String name = cookie.getName();
    if (cookie.getName().equals("verid")) {
    value = cookie.getValue();
    }
    }
    }
    if (value == null) {
    response.setErrorCode(ErrorCode.OUT_PASSCODE.getCode());
    response.setErrorMsg(ErrorCode.OUT_PASSCODE.getDesc());
    } else {
    Boolean key = redisTemplate.hasKey(value);
    //判断图片验证码是否存在
    if (key) {
    //String codeValue = (String) request.getSession().getAttribute("verid");
    String codeValue = redisTemplate.opsForValue().get(value).toString();
    if (codeValue.equalsIgnoreCase(user.getLoginCode())) {
    UserInfoHolder userInfo = new UserInfoHolder();
    userInfo.setUserid(user.getUserid());
    userInfo.setPassword(user.getPassword());
    request.getSession().setAttribute("user",userInfo); //session里面必须存放序列化后的user
    UserInfoHolder userD = (UserInfoHolder) request.getSession().getAttribute("user");
    userD.setUserid(user.getUserid());
    userD.setUsername(user.getUsername());
    response.setData(role.getFlag()); //返回那种登录人员的用户类型....管理员类型A,B,C
    response.setSuccess(true);
    } else {
    response.setSuccess(false);
    response.setErrorMsg(ErrorCode.VALIDATION_CODE_ERROR.getDesc());
    }
    }
    }
    }
    }
    }else{
    response.setSuccess(false);
    response.setErrorMsg(ErrorCode.LOGIN_GRADE_LACK.getDesc());
    }
    }else{
    response.setSuccess(false);
    response.setErrorMsg(ErrorCode.USER_PASSWORD_ERROR.getDesc());
    }
    }else{
    response.setSuccess(false);
    response.setErrorMsg(ErrorCode.USER_ISEXIST.getDesc());
    }
    }else{
    response.setSuccess(false);
    response.setErrorMsg(ErrorCode.USER_ERROR.getDesc());
    }
    }catch (Exception e){
    logger.info("e=="+e);
    response.setErrorMsg(ErrorCode.SYSTEM_ERROR.getDesc());
    }
    return response;
    }




    /*
    ** 登录页--验证码图片存cook,前端直接调
    */
    @GetMapping("/getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Object[] objs = VerifyUtil.createImage();
    String code = objs[0].toString();

    //生成唯一的序列id
    String verid = UUID.randomUUID().toString().replaceAll("-", "");
    //将uuid串存入cookie
    Cookie cookie = new Cookie(VERIFY_ID, verid);

    response.addCookie(cookie);
    StringBuilder verifyKey = new StringBuilder();
    verifyKey.append(verid);

    //将验证码存入redis
    redisTemplate.opsForValue().set(verifyKey.toString(),code,5, TimeUnit.MINUTES);
    logger.info("获取到的"+verifyKey.toString()+"图形验证码=="+redisTemplate.opsForValue().get(verifyKey.toString()).toString());

    BufferedImage image = (BufferedImage) objs[1];
    response.setContentType("image/png");
    OutputStream os = response.getOutputStream();
    //输出验证码图片文件流
    ImageIO.write(image, "png", os);
    }




    //下面的自己画的验证码图片
    package com.yundaex.secretariat.util;

    import javax.imageio.ImageIO;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Random;

    public class VerifyUtil {
    // 验证码字符集
    private static final char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E',
    'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
    // 字符数量
    private static final int SIZE = 6;
    // 干扰线数量
    private static final int LINES = 10;
    // 宽度
    private static final int WIDTH = 120;
    // 高度
    private static final int HEIGHT = 40;
    // 字体大小
    private static final int FONT_SIZE = 30;

    /**
    * 生成随机验证码及图片 Object[0]:验证码字符串; Object[1]:验证码图片。
    */
    public static Object[] createImage() {
    StringBuffer sb = new StringBuffer();
    // 1.创建空白图片
    BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    // 2.获取图片画笔
    Graphics graphic = image.getGraphics();
    // 3.设置画笔颜色
    graphic.setColor(Color.LIGHT_GRAY);
    // 4.绘制矩形背景
    graphic.fillRect(0, 0, WIDTH, HEIGHT);
    // 5.画随机字符
    Random ran = new Random();
    for (int i = 0; i < SIZE; i++) {
    // 取随机字符索引
    int n = ran.nextInt(chars.length);
    // 设置随机颜色
    // graphic.setColor(getRandomColor(101, 111, 121));
    graphic.setColor(getRandomColor(150, 150, 150));
    // 设置字体大小
    graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
    // 画字符
    graphic.drawString(chars[n] + "", i * WIDTH / SIZE, HEIGHT * 2 / 3);
    // 记录字符
    sb.append(chars[n]);
    }
    // 6.画干扰线
    for (int i = 0; i < LINES; i++) {
    // 设置随机颜色
    graphic.setColor(getRandomColor(255, 255, 255));
    // 随机画线
    graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
    }

    /**
    * //扭曲
    shearX(graphic, WIDTH, HEIGHT, Color.LIGHT_GRAY);
    shearY(graphic, WIDTH, HEIGHT, Color.LIGHT_GRAY);
    */

    // 7.返回验证码和图片

    return new Object[] { sb.toString(), image };
    }


    /**
    * 随机取色
    */
    public static Color getRandomColor(int r, int g, int b) {
    Random ran = new Random();
    Color color = new Color(ran.nextInt(r), ran.nextInt(g), ran.nextInt(b));
    return color;
    }
    /**
    * fc:110 bc:133
    * @param fc
    * @param bc
    * @return
    */
    public static Color getRandColor(int fc, int bc) {
    if (fc > 255){
    fc = 255;
    }
    if (bc > 255){
    bc = 255;
    }
    Random random = new Random();
    int r = fc + random.nextInt(bc - fc - 16);
    int g = fc + random.nextInt(bc - fc - 14);
    int b = fc + random.nextInt(bc - fc - 18);
    return new Color(r, g, b);
    }

    private static void shearX(Graphics g, int w1, int h1, Color color) {
    Random random=new Random();
    int period = 2;

    boolean borderGap = true;
    int frames = 1;
    int phase = random.nextInt(2);

    for (int i = 0; i < h1; i++) {
    double d = (double) (period >> 1)* Math.sin((double) i / (double) period + (2.2831853071795862D * (double) phase)/ (double) frames);
    g.copyArea(0, i, w1, 1, (int) d, 0);
    if (borderGap) {
    g.setColor(color);
    g.drawLine((int) d, i, 0, i);
    g.drawLine((int) d + w1, i, w1, i);
    }
    }

    }

    private static void shearY(Graphics g, int w1, int h1, Color color) {
    Random random=new Random();
    int period = random.nextInt(40) + 10; // 50;

    boolean borderGap = true;
    int frames = 20;
    int phase = random.nextInt(2);
    for (int i = 0; i < w1; i++) {
    double d = (double) (period >> 1)
    * Math.sin((double) i / (double) period
    + (2.2831853071795862D * (double) phase)/ (double) frames);
    g.copyArea(i, 0, 1, h1, 0, (int) d);
    if (borderGap) {
    g.setColor(color);
    g.drawLine(i, (int) d, i, 0);
    g.drawLine(i, (int) d + h1, i, h1);
    }

    }

    }



    }
  • 相关阅读:
    Windows平台使用Gitblit搭建Git服务器图文教程
    yapi部署文档
    Yapi学习笔记
    利用微软认知服务实现语音识别功能
    对.Net Core结合Docker和Jexus的实践
    python-集合、字典
    python-文件操作
    python-函数
    python-运算、分支、深浅拷贝
    linux下的文件结构
  • 原文地址:https://www.cnblogs.com/Darkqueen/p/14808832.html
Copyright © 2020-2023  润新知