• Nginx ngx_http_auth_request_module模块鉴权【下】携带账号密码登录


    优化上篇文章

    点我查看上篇

    本篇改动

    1. 增加账号密码进行登录认证

    nginx配置文件

    server {
            listen       8082;
            server_name  localhost;
    
            location /private {
                # 传递参数
                set $auth_request_uri "http://127.0.0.1:8002/auth/token?name=$arg_name&pwd=$arg_pwd";
                auth_request /auth;
                # 鉴权通过后的处理方式
                proxy_pass http://127.0.0.1:8002/auth/success;
            }
    
            location = /auth {
                internal;
                # 鉴权服务器的地址
                proxy_pass $auth_request_uri;
                proxy_pass_request_body off;
                proxy_set_header Content-Length "";
                proxy_set_header X-Original-URI $request_uri;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    注意看:name=KaTeX parse error: Expected 'EOF', got '&' at position 9: arg_name&̲pwd=arg_pwd
    $arg_name以及 $arg_pwd 参数。稍后有说明

    java代码

    package com.task.controller;
    
    import cn.hutool.http.server.HttpServerRequest;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.time.LocalDateTime;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author wuzhenyong
     * ClassName:NginxAuthRequestController.java
     * date:2022-11-23 09:38
     * Description: 认证服务器
     */
    @RestController
    @RequestMapping("/auth")
    public class NginxAuthRequestController {
        @GetMapping("/token")
        public Map<String, Object> token(@RequestParam(value = "name", required = false) String name,
                                         @RequestParam(value = "pwd", required = false) String pwd,
                                         HttpServerRequest request) {
            System.out.println("请求认证服务器接口" + LocalDateTime.now());
            System.out.println(String.format("用户名:%s,密码:%s", name, pwd));
            if ("pitewu".equals(name) && "123456".equals(pwd)) {
                Map<String, Object> result = new HashMap<String, Object>();
                result.put("code", 200);
                result.put("msg", "成功");
                return result;
            }
            throw new RuntimeException("认证失败");
        }
        @GetMapping("/success")
        public Map<String, Object> success() {
            System.out.println("认证成功" + LocalDateTime.now());
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("code", 200);
            result.put("msg", "成功");
            return result;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    浏览器测试

    localhost:8082/private?name=pitewu&pwd=12345666

    nginx配置文件中的$arg_name就是路径name的值哦

    在这里插入图片描述
    在这里插入图片描述

    我们再把路径改为:localhost:8082/private?name=pitewu&pwd=123456

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    路由过滤与引入
    基于51单片机NEC协议红外遥控发送接收仿真设计( proteus仿真+程序+原理图+报告+讲解视频)
    el-select 多选模式下嵌套el-tree 删除tag时能去掉el-tree对应节点的勾
    [java进阶]——HashMap的底层实现原理和源码分析,另附几个高频面试题
    企业怎样做好工厂生产人员管理?
    《最新出炉》系列入门篇-Python+Playwright自动化测试-47-自动滚动到元素出现的位置
    【JAVA基础】专题课(综合案例下)
    求简单微分方程
    〖Python 数据库开发实战 - MySQL篇⑲〗- Having子句的使用
    docker配置镜像代理
  • 原文地址:https://blog.csdn.net/A_yonga/article/details/127996311