|  | @ -2,6 +2,8 @@ package com.yihu.jw.security.oauth2.provider.endpoint;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
	
		
			
				|  |  | import com.google.code.kaptcha.impl.DefaultKaptcha;
 | 
	
		
			
				|  |  | import com.google.code.kaptcha.util.Config;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.login.BaseLoginLogDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.patient.BasePatientDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.ylzinfo.OauthYlzConfigDO;
 | 
	
	
		
			
				|  | @ -20,6 +22,9 @@ import com.yihu.jw.security.service.OauthYlzConfigService;
 | 
	
		
			
				|  |  | import com.yihu.jw.security.utils.DateUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.security.utils.SerializeUtil;
 | 
	
		
			
				|  |  | import com.yihu.utils.security.RSAUtils;
 | 
	
		
			
				|  |  | import io.swagger.annotations.Api;
 | 
	
		
			
				|  |  | import io.swagger.annotations.ApiOperation;
 | 
	
		
			
				|  |  | import io.swagger.annotations.ApiParam;
 | 
	
		
			
				|  |  | import org.apache.commons.collections.map.HashedMap;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
	
		
			
				|  | @ -49,21 +54,24 @@ import org.springframework.web.context.request.RequestContextHolder;
 | 
	
		
			
				|  |  | import org.springframework.web.context.request.ServletRequestAttributes;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import javax.annotation.PostConstruct;
 | 
	
		
			
				|  |  | import javax.imageio.ImageIO;
 | 
	
		
			
				|  |  | import javax.servlet.http.Cookie;
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpServletResponse;
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpSession;
 | 
	
		
			
				|  |  | import java.awt.image.BufferedImage;
 | 
	
		
			
				|  |  | import java.io.ByteArrayOutputStream;
 | 
	
		
			
				|  |  | import java.io.IOException;
 | 
	
		
			
				|  |  | import java.security.KeyPair;
 | 
	
		
			
				|  |  | import java.security.PrivateKey;
 | 
	
		
			
				|  |  | import java.security.interfaces.RSAPrivateKey;
 | 
	
		
			
				|  |  | import java.security.interfaces.RSAPublicKey;
 | 
	
		
			
				|  |  | import java.text.SimpleDateFormat;
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | import java.util.UUID;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  | import sun.misc.BASE64Encoder;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * <p>
 | 
	
	
		
			
				|  | @ -74,6 +82,7 @@ import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  |  * @author Progr1mmer
 | 
	
		
			
				|  |  |  * @created on 2018/8/29
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Api(description = "认证服务")
 | 
	
		
			
				|  |  | @RestController
 | 
	
		
			
				|  |  | public class WlyyLoginEndpoint extends AbstractEndpoint {
 | 
	
		
			
				|  |  | 
 | 
	
	
		
			
				|  | @ -109,6 +118,7 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private OauthWlyyConfigService oauthWlyyConfigService;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @PostConstruct
 | 
	
		
			
				|  |  |     private void init() {
 | 
	
		
			
				|  |  |         super.setTokenGranter(tokenGranter);
 | 
	
	
		
			
				|  | @ -122,6 +132,8 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
 | 
	
		
			
				|  |  |      *                    captcha 验证码
 | 
	
		
			
				|  |  |      *                    password 密码
 | 
	
		
			
				|  |  |      *                    username 用户名/手机/身份证号
 | 
	
		
			
				|  |  |      *                    key 图形验证码键值
 | 
	
		
			
				|  |  |      *                    text 用户输入的图形验证码
 | 
	
		
			
				|  |  |      *                    login_type 用户类型 1或默认为user,2:医生登录,3:患者登录,4:第三方同步账号登录,5.易联众居民健康卡授权登录
 | 
	
		
			
				|  |  |      * @param httpSession
 | 
	
		
			
				|  |  |      * @return
 | 
	
	
		
			
				|  | @ -129,6 +141,16 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @RequestMapping(value = "/oauth/login", method = RequestMethod.POST)
 | 
	
		
			
				|  |  |     public ResponseEntity<Oauth2Envelop<WlyyUserSimple>> login(@RequestParam Map<String, String> parameters, HttpSession httpSession) throws Exception {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //图形验证码验证
 | 
	
		
			
				|  |  |         String key = parameters.get("key");
 | 
	
		
			
				|  |  |         String text = parameters.get("text");
 | 
	
		
			
				|  |  |         if(org.apache.commons.lang3.StringUtils.isNotBlank(key)&& org.apache.commons.lang3.StringUtils.isNotBlank(text)){
 | 
	
		
			
				|  |  |             if(!verifyCaptcha(key,text)){
 | 
	
		
			
				|  |  |                 throw new InvalidRequestException("img_captcha error");
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String username = parameters.get("username");
 | 
	
		
			
				|  |  |         if (StringUtils.isEmpty(username)) {
 | 
	
		
			
				|  |  |             throw new InvalidRequestException("username");
 | 
	
	
		
			
				|  | @ -832,4 +854,49 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
 | 
	
		
			
				|  |  |         return ObjEnvelop.getSuccess("success",map);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @RequestMapping(value = "/oauth/imgCaptcha", method = RequestMethod.GET)
 | 
	
		
			
				|  |  |     @ApiOperation("生成验证码")
 | 
	
		
			
				|  |  |     public ObjEnvelop createCaptcha()throws Exception{
 | 
	
		
			
				|  |  |         return ObjEnvelop.getSuccess("success",generateCaptcha());
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private Map<String, String> generateCaptcha() throws IOException {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         DefaultKaptcha captchaProducer = new DefaultKaptcha();
 | 
	
		
			
				|  |  |         Properties properties = new Properties();
 | 
	
		
			
				|  |  |         properties.put("kaptcha.textproducer.font.color", "blue");
 | 
	
		
			
				|  |  |         properties.put("kaptcha.textproducer.font.size", "45");
 | 
	
		
			
				|  |  |         properties.put("kaptcha.textproducer.char.length", "4");
 | 
	
		
			
				|  |  |         properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
 | 
	
		
			
				|  |  |         properties.put("kaptcha.textproducer.char.string","0123456789QWERTYUIOPLKJHGFDSAZXCVBNM");
 | 
	
		
			
				|  |  |         Config config = new Config(properties);
 | 
	
		
			
				|  |  |         captchaProducer.setConfig(config);
 | 
	
		
			
				|  |  |         String captchaText = captchaProducer.createText();
 | 
	
		
			
				|  |  |         BufferedImage image = captchaProducer.createImage(captchaText);
 | 
	
		
			
				|  |  |         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 | 
	
		
			
				|  |  |         ImageIO.write(image, "png", outputStream);
 | 
	
		
			
				|  |  |         String base64Img = new BASE64Encoder().encode(outputStream.toByteArray());
 | 
	
		
			
				|  |  |         base64Img = "data:image/jpeg;base64,"+base64Img.replaceAll("\n", "").replaceAll("\r", "");//格式化处理
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String key = "hwlyy:captcha:" + UUID.randomUUID().toString();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Map<String, String> data = new HashMap<>();
 | 
	
		
			
				|  |  |         data.put("key", key);
 | 
	
		
			
				|  |  |         data.put("image", base64Img);
 | 
	
		
			
				|  |  |         data.put("format", "png");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         redisTemplate.opsForValue().set(key,captchaText.toLowerCase(),5, TimeUnit.MINUTES);
 | 
	
		
			
				|  |  |         return data;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public boolean verifyCaptcha(String key,String text){
 | 
	
		
			
				|  |  |         boolean pass = false;
 | 
	
		
			
				|  |  |         String captcha = redisTemplate.opsForValue().get(key);
 | 
	
		
			
				|  |  |         if (org.apache.commons.lang3.StringUtils.isNotBlank(captcha)&& captcha.equals(text.toLowerCase())){
 | 
	
		
			
				|  |  |             pass = true;
 | 
	
		
			
				|  |  |             redisTemplate.delete(key);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return pass;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |