# g-whitelist-spring-boot-starter **Repository Path**: haloer/g-whitelist-spring-boot-starter ## Basic Information - **Project Name**: g-whitelist-spring-boot-starter - **Description**: No description available - **Primary Language**: Java - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-04-15 - **Last Updated**: 2021-05-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 安装 **1、maven引用** ```xml com.github g-whitelist-spring-boot-starter {LATEST_VERSION} ``` **2、在启动类开启注解`@EnableWhiteList`** **3、在`application.yml`文件中配置白名单信息** ```yaml g: whitelist: users: aaa,111,xiaofuge ``` ## 使用示例 **1、拦截url中的key,参数类型是parameter** ``` @DoWhiteList(key = "userId") @RequestMapping(path = "/api/queryUserInfo", method = RequestMethod.GET) public UserInfo queryUserInfo(@RequestParam String userId,@RequestParam(required = false) String nn) { logger.info("查询用户信息,userId:{}", userId); return new UserInfo("虫虫:" + userId, 19, "天津市东丽区万科赏溪苑14-0000"); } ``` **成功:** 请求地址:http://localhost:8081/api/queryUserInfo?userId=axx 返回结果:success **失败:** 请求地址:http://localhost:8081/api/queryUserInfo?userId=fdgfds 返回结果:500错误,后台抛出 AccessRejectException 异常 **2、拦截url中的key,参数类型是path** ``` @DoWhiteList(key = "userId",location = Location.path) @RequestMapping(path = "/api/queryUserInfo/v1/{userId}", method = RequestMethod.GET) public UserInfo queryUserInfoV1(@PathVariable String userId, @RequestParam(required = false) String nn) { logger.info("查询用户信息,userId:{}", userId); return new UserInfo("虫虫:" + userId, 19, "天津市东丽区万科赏溪苑14-0000"); } ``` **成功:** 请求地址:http://localhost:8081/api/queryUserInfo/v1/xxx 返回结果:success **失败:** 请求地址:http://localhost:8081/api/queryUserInfo/v1/fdgfds 返回结果:500错误,后台抛出 AccessRejectException 异常 **3、拦截url中的key,参数类型是header** ``` @DoWhiteList(key = "userId",location = Location.header) @RequestMapping(path = "/api/queryUserInfo/v2", method = RequestMethod.GET) public UserInfo queryUserInfoV2( @RequestParam(required = false) String nn) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); String userId = request.getHeader("userId"); logger.info("查询用户信息,userId:{}", userId); return new UserInfo("虫虫:" + userId, 19, "天津市东丽区万科赏溪苑14-0000"); } ``` **成功:** 请求地址:http://localhost:8081/api/queryUserInfo/v2 > heaer中存放 userId:xxx 返回结果:success **失败:** 请求地址:http://localhost:8081/api/queryUserInfo/v2 > heaer中存放 userId:fdgfds 返回结果:500错误,后台抛出 AccessRejectException 异常