| | |
| | | package com.java110.web.smo.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.java110.common.constant.CommonConstant; |
| | | import com.java110.common.constant.ServiceConstant; |
| | | import com.java110.common.util.Assert; |
| | | import com.java110.core.base.smo.BaseServiceSMO; |
| | | import com.java110.utils.cache.CommonCache; |
| | | import com.java110.utils.util.Assert; |
| | | import com.java110.core.context.IPageData; |
| | | import com.java110.core.factory.AuthenticationFactory; |
| | | import com.java110.core.factory.ValidateCodeFactory; |
| | | import com.java110.web.core.BaseComponentSMO; |
| | | import com.java110.web.smo.ILoginServiceSMO; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.*; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.util.Random; |
| | | |
| | | /** |
| | | * 登录信息实现类 |
| | |
| | | */ |
| | | |
| | | @Service("loginServiceSMOImpl") |
| | | public class LoginServiceSMOImpl extends BaseServiceSMO implements ILoginServiceSMO { |
| | | public class LoginServiceSMOImpl extends BaseComponentSMO implements ILoginServiceSMO { |
| | | private final static Logger logger = LoggerFactory.getLogger(LoginServiceSMOImpl.class); |
| | | |
| | | |
| | | private static char[] chs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray(); |
| | | private static final int NUMBER_OF_CHS = 4; |
| | | private static final int IMG_WIDTH = 65; |
| | | private static final int IMG_HEIGHT = 25; |
| | | private static Random r = new Random(); |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | |
| | |
| | | |
| | | Assert.jsonObjectHaveKey(pd.getReqData(),"username","请求报文格式错误或未包含username信息"); |
| | | JSONObject loginInfo = JSONObject.parseObject(pd.getReqData()); |
| | | |
| | | //调用 验证码组件验证码是否正确 |
| | | responseEntity = this.invokeComponent("validate-code","validate",pd); |
| | | if(responseEntity.getStatusCode() != HttpStatus.OK){ |
| | | return responseEntity; |
| | | } |
| | | |
| | | loginInfo.put("passwd", AuthenticationFactory.passwdMd5(loginInfo.getString("passwd"))); |
| | | responseEntity = this.callCenterService(restTemplate,pd,loginInfo.toJSONString(),ServiceConstant.SERVICE_API_URL+"/api/user.service.login",HttpMethod.POST); |
| | | responseEntity = this.callCenterService(restTemplate,pd,loginInfo.toJSONString(), "http://api.java110.com:8008/api/user.service.login",HttpMethod.POST); |
| | | if(responseEntity.getStatusCode() == HttpStatus.OK){ |
| | | pd.setToken(JSONObject.parseObject(responseEntity.getBody()).getString("token")); |
| | | } |
| | |
| | | */ |
| | | @Override |
| | | public ResponseEntity<String> generateValidateCode(IPageData pd) { |
| | | int w = 200, h = 80; |
| | | String verifyCode = ValidateCodeFactory.generateVerifyCode(4); |
| | | ResponseEntity<String> verifyCodeImage = null; |
| | | try { |
| | | verifyCodeImage = new ResponseEntity<>(ValidateCodeFactory.outputImage(200, 80, verifyCode), HttpStatus.OK); |
| | | |
| | | //将验证码存入Redis中 |
| | | CommonCache.setValue(pd.getSessionId()+"_validateCode",verifyCode.toLowerCase(),CommonCache.defaultExpireTime); |
| | | |
| | | |
| | | BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB); // 实例化BufferedImage |
| | | Graphics g = image.getGraphics(); |
| | | Color c = new Color(200, 200, 255); // 验证码图片的背景颜色 |
| | | g.setColor(c); |
| | | g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT); // 图片的边框 |
| | | |
| | | StringBuffer sb = new StringBuffer(); // 用于保存验证码字符串 |
| | | int index; // 数组的下标 |
| | | for (int i = 0; i < NUMBER_OF_CHS; i++) { |
| | | index = r.nextInt(chs.length); // 随机一个下标 |
| | | g.setColor(new Color(r.nextInt(88), r.nextInt(210), r.nextInt(150))); // 随机一个颜色 |
| | | g.drawString(chs[index] + "", 15 * i + 3, 18); // 画出字符 |
| | | sb.append(chs[index]); // 验证码字符串 |
| | | } |
| | | |
| | | /*request.getSession().setAttribute("piccode", sb.toString()); // 将验证码字符串保存到session中 |
| | | ImageIO.write(image, "jpg", response.getOutputStream());*/ |
| | | return null; |
| | | }catch (Exception e){ |
| | | logger.error("生成验证码失败,",e); |
| | | verifyCodeImage = new ResponseEntity<>("", HttpStatus.INTERNAL_SERVER_ERROR); |
| | | } |
| | | return verifyCodeImage; |
| | | } |
| | | |
| | | /** |
| | | * 校验验证码 |
| | | * @param pd 页面请求对象 |
| | | * @return |
| | | */ |
| | | public ResponseEntity<String> validate(IPageData pd){ |
| | | |
| | | logger.debug("校验验证码参数:{}",pd.toString()); |
| | | ResponseEntity<String> verifyResult = null; |
| | | Assert.jsonObjectHaveKey(pd.getReqData(),"validateCode","请求报文中未包含 validateCode节点"+pd.toString()); |
| | | |
| | | String code = CommonCache.getValue(pd.getSessionId()+"_validateCode"); |
| | | |
| | | if(JSONObject.parseObject(pd.getReqData()).getString("validateCode").toLowerCase().equals(code)){ |
| | | verifyResult = new ResponseEntity<>("成功", HttpStatus.OK); |
| | | }else{ |
| | | pd.setToken(""); |
| | | verifyResult = new ResponseEntity<>("验证码错误", HttpStatus.INTERNAL_SERVER_ERROR); |
| | | } |
| | | |
| | | |
| | | return verifyResult; |
| | | } |
| | | |
| | | |
| | | public RestTemplate getRestTemplate() { |
| | | return restTemplate; |
| | | } |