whycxzp
6 天以前 9f475b23410a0a68fdc76699ff9462253824b476
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
package com.whyc.controller;
 
import com.whyc.dto.Response;
import com.whyc.util.ActionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
 
@RequestMapping("message")
@RestController
@Api(tags = "验证码")
public class MessageController {
 
    @ApiOperation("获取验证码")
    @GetMapping("/getFontDynamicCode")
    public Response getFontDynamicCode() throws NoSuchAlgorithmException {
        StringBuilder sb = new StringBuilder();
        String originStr = "0123456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ0123456789";
        SecureRandom random;
        random = SecureRandom.getInstance("SHA1PRNG");
 
        for (int i = 0; i < 4; i++) {
            sb.append(originStr.charAt(random.nextInt(originStr.length())));
        }
        //内存Session中存储动态口令
        ActionUtil.getSession().setAttribute("fontDynamicCode", sb.toString());
        return new Response().set(1,sb.toString());
    }
 
}