提交 | 用户 | 时间
|
1ac2bc
|
1 |
/** |
懒 |
2 |
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng) |
|
3 |
* <p> |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* <p> |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* <p> |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package cn.stylefeng.guns.sys.modular.system.controller; |
|
17 |
|
|
18 |
import cn.stylefeng.guns.base.consts.ConstantsContext; |
|
19 |
import cn.stylefeng.roses.core.util.FileUtil; |
|
20 |
import com.google.code.kaptcha.Constants; |
|
21 |
import com.google.code.kaptcha.Producer; |
|
22 |
import org.springframework.beans.factory.annotation.Autowired; |
|
23 |
import org.springframework.stereotype.Controller; |
|
24 |
import org.springframework.web.bind.annotation.PathVariable; |
|
25 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
26 |
|
|
27 |
import javax.imageio.ImageIO; |
|
28 |
import javax.servlet.ServletOutputStream; |
|
29 |
import javax.servlet.http.HttpServletRequest; |
|
30 |
import javax.servlet.http.HttpServletResponse; |
|
31 |
import javax.servlet.http.HttpSession; |
|
32 |
import java.awt.image.BufferedImage; |
|
33 |
import java.io.IOException; |
|
34 |
|
|
35 |
/** |
|
36 |
* 验证码生成 |
|
37 |
* |
|
38 |
* @author fengshuonan |
|
39 |
* @date 2017-05-05 23:10 |
|
40 |
*/ |
|
41 |
@Controller |
|
42 |
@RequestMapping("/kaptcha") |
|
43 |
public class KaptchaController { |
|
44 |
|
|
45 |
@Autowired |
|
46 |
private Producer producer; |
|
47 |
|
|
48 |
/** |
|
49 |
* 生成验证码 |
|
50 |
*/ |
|
51 |
@RequestMapping("") |
|
52 |
public void index(HttpServletRequest request, HttpServletResponse response) { |
|
53 |
HttpSession session = request.getSession(); |
|
54 |
|
|
55 |
response.setDateHeader("Expires", 0); |
|
56 |
|
|
57 |
// Set standard HTTP/1.1 no-cache headers. |
|
58 |
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); |
|
59 |
|
|
60 |
// Set IE extended HTTP/1.1 no-cache headers (use addHeader). |
|
61 |
response.addHeader("Cache-Control", "post-check=0, pre-check=0"); |
|
62 |
|
|
63 |
// Set standard HTTP/1.0 no-cache header. |
|
64 |
response.setHeader("Pragma", "no-cache"); |
|
65 |
|
|
66 |
// return a jpeg |
|
67 |
response.setContentType("image/jpeg"); |
|
68 |
|
|
69 |
// create the text for the image |
|
70 |
String capText = producer.createText(); |
|
71 |
|
|
72 |
// store the text in the session |
|
73 |
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); |
|
74 |
|
|
75 |
// create the image with the text |
|
76 |
BufferedImage bi = producer.createImage(capText); |
|
77 |
ServletOutputStream out = null; |
|
78 |
try { |
|
79 |
out = response.getOutputStream(); |
|
80 |
} catch (IOException e) { |
|
81 |
e.printStackTrace(); |
|
82 |
} |
|
83 |
|
|
84 |
// write the data out |
|
85 |
try { |
|
86 |
ImageIO.write(bi, "jpg", out); |
|
87 |
} catch (IOException e) { |
|
88 |
e.printStackTrace(); |
|
89 |
} |
|
90 |
try { |
|
91 |
try { |
|
92 |
out.flush(); |
|
93 |
} catch (IOException e) { |
|
94 |
e.printStackTrace(); |
|
95 |
} |
|
96 |
} finally { |
|
97 |
try { |
|
98 |
out.close(); |
|
99 |
} catch (IOException e) { |
|
100 |
e.printStackTrace(); |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* 返回图片 |
|
107 |
* |
|
108 |
* @author stylefeng |
|
109 |
* @Date 2017/5/24 23:00 |
|
110 |
*/ |
|
111 |
@RequestMapping("/{pictureId}") |
|
112 |
public void renderPicture(@PathVariable("pictureId") String pictureId, HttpServletResponse response) { |
|
113 |
String path = ConstantsContext.getFileUploadPath() + pictureId; |
|
114 |
try { |
|
115 |
byte[] bytes = FileUtil.toByteArray(path); |
|
116 |
response.getOutputStream().write(bytes); |
|
117 |
} catch (Exception e) { |
|
118 |
//如果找不到图片就返回一个默认图片 |
|
119 |
try { |
|
120 |
response.sendRedirect("/static/img/girl.gif"); |
|
121 |
} catch (IOException e1) { |
|
122 |
e1.printStackTrace(); |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
} |