|
@@ -1,37 +1,102 @@
|
|
|
package com.ym.mec.auth.web.controller;
|
|
|
|
|
|
+import com.google.code.kaptcha.Constants;
|
|
|
+import com.google.code.kaptcha.Producer;
|
|
|
+import com.ym.mec.auth.config.constant.SecurityConstants;
|
|
|
+import com.ym.mec.common.controller.BaseController;
|
|
|
+import com.ym.mec.common.validcode.SmsCodeService;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiImplicitParam;
|
|
|
+import io.swagger.annotations.ApiImplicitParams;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
-
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
-
|
|
|
-import com.ym.mec.common.controller.BaseController;
|
|
|
-import com.ym.mec.common.validcode.SmsCodeService;
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@RestController
|
|
|
-@RequestMapping("sms")
|
|
|
-@Api(description = "短信服务")
|
|
|
+@RequestMapping("code")
|
|
|
+@Api(description = "验证码服务")
|
|
|
public class SmsCodeController extends BaseController {
|
|
|
|
|
|
@Autowired
|
|
|
private SmsCodeService smsCodeService;
|
|
|
+ @Autowired
|
|
|
+ private Producer captchaProducer;
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String,String> redisTemplate;
|
|
|
|
|
|
@ApiOperation("发送登录短信验证码")
|
|
|
@ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String")
|
|
|
- @PostMapping("/sendVerifyCode")
|
|
|
- //@PreAuthorize("hasAnyAuthority('student')")
|
|
|
+ @PostMapping("/sendSms")
|
|
|
@PreAuthorize("@pcs.hasPermissions('sys_user_manage')")
|
|
|
public Object sendLoginVerifyCode(String mobile) {
|
|
|
smsCodeService.sendValidCode(mobile);
|
|
|
return succeed();
|
|
|
}
|
|
|
+
|
|
|
+ @PostMapping("/verifyLoginImage")
|
|
|
+ @ApiOperation("校验登录图形验证码")
|
|
|
+ @ApiImplicitParams({ @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String"),
|
|
|
+ @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String") })
|
|
|
+ public Object verifyImageCode(String phone,String code){
|
|
|
+ if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
|
|
|
+ return failed(SecurityConstants.PARAM_VERIFY_EXCEPTION);
|
|
|
+ }
|
|
|
+ String redisKey = Constants.KAPTCHA_SESSION_KEY + phone;
|
|
|
+ if(redisTemplate.hasKey(redisKey)){
|
|
|
+ if(StringUtils.equals(redisTemplate.opsForValue().get(redisKey),code)){
|
|
|
+ return succeed();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return failed(SecurityConstants.VERIFY_FAILURE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/getLoginImage")
|
|
|
+ @ApiOperation("获取登录图片验证码")
|
|
|
+ @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String")
|
|
|
+ public void getKaptchaImage(HttpServletResponse response, HttpSession session,String phone) throws Exception {
|
|
|
+ if(StringUtils.isEmpty(phone)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ response.setDateHeader("Expires", 0);
|
|
|
+
|
|
|
+ // Set standard HTTP/1.1 no-cache headers.
|
|
|
+ response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
|
|
|
+ // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
|
|
|
+ response.addHeader("Cache-Control", "post-check=0, pre-check=0");
|
|
|
+ // Set standard HTTP/1.0 no-cache header.
|
|
|
+ response.setHeader("Pragma", "no-cache");
|
|
|
+ // return a jpeg
|
|
|
+ response.setContentType("image/jpeg");
|
|
|
+ // create the text for the image
|
|
|
+ String capText = captchaProducer.createText();
|
|
|
+
|
|
|
+ redisTemplate.opsForValue().set(Constants.KAPTCHA_SESSION_KEY + phone,capText,3, TimeUnit.MINUTES);
|
|
|
+ // create the image with the text
|
|
|
+ BufferedImage bi = captchaProducer.createImage(capText);
|
|
|
+ ServletOutputStream out = response.getOutputStream();
|
|
|
+ // write the data out
|
|
|
+ ImageIO.write(bi, "jpg", out);
|
|
|
+ try {
|
|
|
+ out.flush();
|
|
|
+ } finally {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
@PostMapping("/query")
|
|
|
public Object query() {
|