Spring Cloud Feign 微服务之间图形验证码传递

提供图形验证码服务

需要引入

        
        
            com.github.penggle
            kaptcha
            2.3.2
            
                
                    javax.servlet-api
                    javax.servlet
                
            
        

定义接口

  public interface VerifyService {

    /**
     * 生成图形验证码
     * @return
     * @throws Exception
     */
    @GetMapping("/getVerifiCode")
    byte[] getVerifiCode() throws Exception;

    /**
     * 验证
     * @param code
     * @return
     */
    @PostMapping("/checkVerifiCode")
    Boolean checkVerifiCode(String code);
  }

实现类

    @Resource
    private DefaultKaptcha defaultKaptcha;

    @Override
    public byte[] getVerifiCode() throws Exception {
        //证码字符串生成验
        String text = defaultKaptcha.createText();
        //验证码存入session(可以将text加密储存)
        SessionUtil.session().setAttribute("key", text);
        //验证码转换
        BufferedImage image = defaultKaptcha.createImage(text);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", byteArrayOutputStream);
        //定义响应值,写入byte
        byte[] bytes = byteArrayOutputStream.toByteArray();
        ResponseUtils.buildImageRes(bytes);
        return bytes;
    }

    @Override
    public Boolean checkVerifiCode(String code) {
        HttpSession session = SessionUtil.session();
        String text = (String) session.getAttribute("key");
        if (StringUtils.isBlank(text)) {
            return Boolean.FALSE;
        }
        if (!code.equals(text)) {
            return Boolean.FALSE;
        }
        //从session中移除验证码text
        session.removeAttribute("key");
        return Boolean.TRUE;
    }

相关工具类

public class SessionUtil {
    /**
     * 获取当前session
     *
     * @return
     */
    public static HttpSession session() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request.getSession();
    }
}
public class ResponseUtils {
    /**
     * 创建Response
     * @param bytes
     * @throws IOException
     */
    public static void buildImageRes(byte[] bytes) throws IOException {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletResponse httpServletResponse = servletRequestAttributes.getResponse();
        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setContentType("image/jpeg");
        ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
        responseOutputStream.write(bytes);
        responseOutputStream.flush();
        responseOutputStream.close();
    }
}

调用端

服务引入

@FeignClient(value = "verify-service", path = "/", fallback = VerifyClientFallBack.class)
public interface VerifyClient {
    /**
     * 获取图形验证码
     *
     * @throws Exception
     */
    @GetMapping("/getVerifiCode")
    byte[] getVerifiCode() throws Exception;

    /**
     * 检验图形验证码
     *
     * @param req 请求类
     * @return
     */
    @PostMapping("/checkVerifiCode")
    Boolean checkVerifiCode(String req);
}

服务降级

@Component
public class VerifyClientFallBack implements VerifyClient {
    @Override
    public byte[] getVerifiCode() throws Exception {
        return new byte[0];
    }

    @Override
    public Boolean checkVerifiCode(String req) {
        return Boolean.FALSE;
    }
}

服务调用

    @Resource
    private VerifyClient verifyClient;

    @Override
    public void getVerifiCode() throws Exception {
        byte[] bytes = baseVerifyClient.getVerifiCode();
        ResponseUtils.buildImageRes(bytes);
    }

    @Override
    public Boolean checkVerifiCode(String req) {
        return baseVerifyClient.checkVerifiCode(req);
    }

 

你可能感兴趣的:(Spring,Cloud)