|  |  | package com.tianbo.warehouse.controller; | 
|  |  |  | 
|  |  | import com.alibaba.fastjson.JSON; | 
|  |  |  | 
|  |  | import com.google.code.kaptcha.impl.DefaultKaptcha; | 
|  |  |  | 
|  |  | import com.thoughtworks.xstream.core.util.Base64Encoder; | 
|  |  | import com.tianbo.warehouse.controller.response.ResultJson; | 
|  |  | import com.tianbo.warehouse.model.ROLE; | 
|  |  | import com.tianbo.warehouse.service.RoleService; | 
|  |  |  | 
|  |  | import com.tianbo.warehouse.util.RedisUtils; | 
|  |  |  | 
|  |  | import lombok.extern.slf4j.Slf4j; | 
|  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|  |  | import org.springframework.web.bind.annotation.PostMapping; | 
|  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
|  |  | import org.springframework.web.bind.annotation.RestController; | 
|  |  |  | 
|  |  | 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.util.HashMap; | 
|  |  | import java.util.List; | 
|  |  | import java.util.Map; | 
|  |  | import java.util.UUID; | 
|  |  |  | 
|  |  | @Slf4j | 
|  |  | @RestController() | 
|  |  | @RequestMapping("/anonymous") | 
|  |  | public class AnonymousController { | 
|  |  |  | 
|  |  | @Autowired | 
|  |  | RoleService roleService; | 
|  |  |  | 
|  |  | @Autowired | 
|  |  | RedisUtils redisUtils; | 
|  |  |  | 
|  |  | @Autowired | 
|  |  | private  DefaultKaptcha captchaProducer; | 
|  |  |  | 
|  |  | /** | 
|  |  | * 配置匿名者可以访问的路由,并更新到redis,匿名者默认可以访问的role_name =ROLE_anonymous | 
|  |  | * 此方法会将所有符合权限组名=ROLE_anonymous的权限更新到redis中,供gateway调用判断权限 | 
|  |  | * @return | 
|  |  | */ | 
|  |  | @PostMapping("initAnonymousRoute") | 
|  |  | public ResultJson initAnonymousRoute(){ | 
|  |  | List<ROLE> list = roleService.getROLE_anonymousPermList(); | 
|  |  | String json = JSON.toJSONString(list); | 
|  |  | boolean result= redisUtils.set("ROLE_anonymous_routers", json,0); | 
|  |  | return result  ? new ResultJson("200","匿名者权限配置成功") :new ResultJson("500","匿名者权限配置失败"); | 
|  |  | } | 
|  |  |  | 
|  |  | /** | 
|  |  | * 生成验证码 | 
|  |  | */ | 
|  |  | @RequestMapping(value = "/randCode") | 
|  |  | public ResultJson getRandCode(){ | 
|  |  |  | 
|  |  | // 获取验证码上的文字 | 
|  |  | String capText = captchaProducer.createText(); | 
|  |  |  | 
|  |  | //  将文件渲染到图片上 | 
|  |  | BufferedImage bi = captchaProducer.createImage(capText); | 
|  |  | ByteArrayOutputStream outputStream = null; | 
|  |  | outputStream = new ByteArrayOutputStream(); | 
|  |  | Base64Encoder encoder = new Base64Encoder(); | 
|  |  | Map<String,Object> map = new HashMap<>(); | 
|  |  | String verifyToken = ""; | 
|  |  | try { | 
|  |  | verifyToken =  UUID.randomUUID().toString(); | 
|  |  | redisUtils.set("verifyToken_" + verifyToken,capText,120); | 
|  |  | ImageIO.write(bi, "jpeg", outputStream); | 
|  |  | map.put("verifyImg","data:image/jpeg;base64,"+encoder.encode(outputStream.toByteArray())); | 
|  |  | } catch (IOException e) { | 
|  |  | e.printStackTrace(); | 
|  |  | return new ResultJson("500","verify get error"); | 
|  |  | } | 
|  |  | return new ResultJson("200","verify get ok",map,verifyToken); | 
|  |  |  | 
|  |  | } | 
|  |  | } | 
... | ... |  |