xiaoyong931011
2023-02-23 e3d7ec787c2a2b03af577fa151cf78f951a6ae66
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
131
132
133
134
135
package cc.mrbird.febs.common.service;
 
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.ImageType;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.properties.FebsProperties;
import cc.mrbird.febs.common.properties.ValidateCodeProperties;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 验证码服务
 *
 * @author MrBird
 */
@Service
@RequiredArgsConstructor
public class ValidateCodeService {
 
    private final RedisService redisService;
    private final FebsProperties properties;
 
    public void create(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        String key = session.getId();
        ValidateCodeProperties code = properties.getCode();
        setHeader(response, code.getType());
 
        Captcha captcha = createCaptcha(code);
        redisService.set(FebsConstant.CODE_PREFIX  + key, StringUtils.lowerCase(captcha.text()), code.getTime());
        captcha.out(response.getOutputStream());
    }
 
    
    public void check(String key, String value) throws FebsException {
        Object codeInRedis = redisService.get(FebsConstant.CODE_PREFIX + key);
        if (StringUtils.isBlank(value)) {
            throw new FebsException("请输入验证码");
        }
        if (codeInRedis == null) {
            throw new FebsException("验证码已过期");
        }
        if (!StringUtils.equalsIgnoreCase(value, String.valueOf(codeInRedis))) {
            throw new FebsException("验证码不正确");
        }
    }
 
    private Captcha createCaptcha(ValidateCodeProperties code) {
        Captcha captcha;
        if (StringUtils.equalsIgnoreCase(code.getType(), ImageType.GIF)) {
            captcha = new GifCaptcha(code.getWidth(), code.getHeight(), code.getLength());
        } else {
            captcha = new SpecCaptcha(code.getWidth(), code.getHeight(), code.getLength());
        }
        captcha.setCharType(code.getCharType());
        return captcha;
    }
 
    private void setHeader(HttpServletResponse response, String type) {
        if (StringUtils.equalsIgnoreCase(type, ImageType.GIF)) {
            response.setContentType(MediaType.IMAGE_GIF_VALUE);
        } else {
            response.setContentType(MediaType.IMAGE_PNG_VALUE);
        }
        response.setHeader(HttpHeaders.PRAGMA, "No-cache");
        response.setHeader(HttpHeaders.CACHE_CONTROL, "No-cache");
        response.setDateHeader(HttpHeaders.EXPIRES, 0L);
    }
 
 
//    public static void main(String[] args) {
//        List<Integer> lists = new ArrayList<>();
////        lists.add(1);
////        lists.add(2);
////        lists.add(3);
////        lists.add(4);
//        System.out.println(lists);
//        List<Integer> collect = lists
//                .stream()
//                .filter(list -> 1 == list)
//                .collect(Collectors.toList());
//
//        System.out.println(collect);
//
//        List<Integer> collect1 = lists.stream().filter(
//                list -> {
//                    if (1 != list) {
//                        return true;
//                    }
//                    return false;
//                }
//        ).collect(Collectors.toList());
//
//        System.out.println(collect1);
//
//
//        List<Integer> list2 = new ArrayList<>();
//        Random rd = new Random();
//        for (int i = 0; i < 100; i++) {
//            list2.add(rd.nextInt(101));// 随机产生一个[0,100]的数字
//        }
//        System.out.println(list2);
//        // 流处理, 进行数据处理
//        DoubleSummaryStatistics ds = list2.stream().collect(Collectors.summarizingDouble(e -> e));
//        Map<Boolean, List<Integer>> collect2 = list2.stream().collect(Collectors.partitioningBy(e -> e >= 60));
//        Map<Boolean, List<Integer>> collect3 = list2.stream().collect(Collectors.partitioningBy(e -> e >= 80));
//        Map<Boolean, List<Integer>> collect4 = list2.stream().collect(Collectors.partitioningBy(e -> e >= 200));
//        System.out.println("平均分"+ds.getAverage());
//        System.out.println("最高分"+ds.getMax());
//        System.out.println("最低分"+ds.getMin());
//        System.out.println("总分"+ds.getSum());
//        System.out.println("人数"+ds.getCount());
//        System.out.println("大于60分"+collect2.toString());
//        System.out.println("大于80分"+collect3.get(true).toString());
//        System.out.println("大于90分"+collect4.get(true).toString());
//        JSONObject jsonObject = JSONUtil.parseObj(collect2);
//        System.out.println(jsonObject);
//
//    }
}