当前位置:   article > 正文

springboot 接口隐藏_springboot如何隐藏接口url

springboot如何隐藏接口url

springboot 接口隐藏

                             

                                                    

                                   

接口隐藏

              

应用:秒杀场景中,避免直接暴露秒杀接口,实现url动态化

                   

实现方法

  1. 前端页面点击/getPath,获取动态路径path,同时后端存储path
  2. 获取成功后,将path与秒杀路径拼接,如"/"+path+"/miaosha"
  3. 后端读取动态路径path(@PathVariable),并进行检验
  4. 如果与后端存储的路径匹配,则执行后续步骤,否则抛异常退出

                                  

说明

  1. 秒杀地址动态化可防止秒杀地址直接暴露,用户只能点击按钮触发秒杀请求
  2. 但是,用户可使用脚本频繁点击按钮,因此需要使用限流措施防止刷接口

                                 

                                   

使用示例

                    

***********

dto 层

            

ResponseResult

  1. @Data
  2. public class ResponseResult<T> {
  3. private String code;
  4. private String status;
  5. private String message;
  6. private T data;
  7. }

                        

***********

util 层

             

StoreUtil

  1. public class StoreUtil {
  2. private static final Map<String,String> map=new HashMap<>();
  3. public static void setValue(String key, String value){
  4. map.put(key, value);
  5. }
  6. public static String getValue(String key){
  7. return map.get(key);
  8. }
  9. public static void deleteKey(String key){
  10. map.remove(key);
  11. }
  12. }

                   

***********

service 层

                  

DynamicUrlService

  1. @Service
  2. public class DynamicUrlService {
  3. public String createPath(String userId, String goodId){
  4. //此处也可验证秒杀开始时间,秒杀开始后返回path
  5. String key=userId+goodId;
  6. String value=RandomStringUtils.randomAlphanumeric(20);
  7. value=DigestUtils.md5DigestAsHex(value.getBytes(StandardCharsets.UTF_8));
  8. StoreUtil.setValue(key,value);
  9. return value;
  10. }
  11. public boolean checkPath(String pathKey, String pathValue){
  12. if (pathKey==null || pathValue==null){
  13. return false;
  14. }
  15. synchronized (this){
  16. if (pathValue.equals(StoreUtil.getValue(pathKey))){
  17. StoreUtil.deleteKey(pathKey);
  18. return true;
  19. }
  20. }
  21. return false;
  22. }
  23. }

                       

***********

config 层

                    

WebConfig

  1. @Configuration
  2. public class WebConfig implements WebMvcConfigurer {
  3. @Override
  4. public void addViewControllers(ViewControllerRegistry registry) {
  5. registry.addViewController("/index").setViewName("index");
  6. }
  7. }

                         

***********

controller 层

                

HelloController

  1. @RestController
  2. public class HelloController {
  3. @Resource
  4. private DynamicUrlService dynamicUrlService;
  5. @RequestMapping("/getPath")
  6. public ResponseResult<String> getPath(String userId, String goodId){
  7. if ( (userId==null||userId=="")
  8. || (goodId==null||goodId=="") ){
  9. throw new RuntimeException("userId或者goodId不能为空");
  10. }
  11. String path = dynamicUrlService.createPath(userId,goodId);
  12. ResponseResult<String> result=new ResponseResult<>();
  13. result.setCode("000000");
  14. result.setStatus("success");
  15. result.setData(path);
  16. return result;
  17. }
  18. @RequestMapping("/{path}/hello")
  19. public ResponseResult<String> hello(@PathVariable("path") String path,
  20. String userId, String goodId,
  21. String name, Integer age){
  22. System.out.println(userId+" "+goodId);
  23. System.out.println(path);
  24. String key=userId+goodId;
  25. if (!dynamicUrlService.checkPath(key,path)){
  26. throw new RuntimeException("path 检验出错");
  27. }
  28. System.out.println(name+" "+age);
  29. ResponseResult<String> result=new ResponseResult<>();
  30. result.setCode("000000");
  31. result.setStatus("success");
  32. result.setData(name+" "+age);
  33. return result;
  34. }
  35. @ExceptionHandler
  36. public ResponseResult<String> handleException(Exception e){
  37. ResponseResult<String> result=new ResponseResult<>();
  38. result.setCode("111111");
  39. result.setStatus("error");
  40. result.setMessage(e.getMessage());
  41. return result;
  42. }
  43. }

                        

***********

前端页面

                                            

index.html

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml"
  3. xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Title</title>
  7. <script src="/jquery/jquery-3.6.0.min.js"></script>
  8. <script src="/layui-v2.6.8/layui/layui.js"></script>
  9. <link rel="stylesheet" href="/layui-v2.6.8/layui/css/layui.css">
  10. <script>
  11. $(function (){
  12. let path;
  13. $("#btn").click(function (){
  14. $.get({
  15. url: "/getPath",
  16. data: {
  17. userId: $("#userId").val(),
  18. goodId: $("#goodId").val()
  19. },
  20. success: function (result){
  21. if (result.code === "000000" ){
  22. path=result.data;
  23. $.get({
  24. url: "/"+path+"/hello",
  25. data: {
  26. userId: $("#userId").val(),
  27. goodId: $("#goodId").val(),
  28. name: "瓜田李下",
  29. age: 20
  30. },
  31. success: function (result){
  32. if (result.code==="000000"){
  33. layer.msg(result.data);
  34. }else {
  35. layer.msg(result.message)
  36. }
  37. }
  38. })
  39. }else {
  40. layer.msg(result.message);
  41. }
  42. }
  43. })
  44. })
  45. })
  46. </script>
  47. </head>
  48. <body>
  49. <div th:align="center">
  50. <div class="layui-form-item">
  51. <label class="layui-form-label" style="color: coral;font-weight: bolder">userId</label>
  52. <div class="layui-input-inline">
  53. <input type="text" id="userId" required lay-verify="required" placeholder="请输入userId" autocomplete="off" class="layui-input">
  54. </div>
  55. </div>
  56. <div class="layui-form-item">
  57. <label class="layui-form-label" style="color: coral;font-weight: bolder">goodId</label>
  58. <div class="layui-input-inline">
  59. <input type="text" id="goodId" required lay-verify="required" placeholder="请输入goodId" autocomplete="off" class="layui-input">
  60. </div>
  61. </div>
  62. <div class="layui-form-item">
  63. <div class="layui-input-inline">
  64. <button id="btn" class="layui-btn">提交</button>
  65. </div>
  66. </div>
  67. </div>
  68. </body>
  69. </html>

                           

                                               

                                   

使用测试

                        

localhost:8080/index

                         

                

 userId、goodId均为空,点击按钮

                   

                             

userId、goodId不为空,点击按钮

                   

                   

使用任意path,直接访问/{path}/hello 

                         

                         

                                          

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/618966
推荐阅读
相关标签
  

闽ICP备14008679号