Nginx ngx_http_auth_request_module模块鉴权

这篇具有很好参考价值的文章主要介绍了Nginx ngx_http_auth_request_module模块鉴权。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

ngx_http_auth_request_module是什么?

ngx_http_auth_request_module模块 实现了基于一子请求的结果的客户端的授权。如果子请求返回2xx响应码,则允许访问。如果它返回401或403,则访问被拒绝并显示相应的错误代码。子请求返回的任何其他响应代码都被认为是错误的。
auth_request使用的也是subrequest进行子请求。

ngx_http_auth_request_module模块用途

当我们访问一个资源需要进行鉴权时,可以使用Nginxhttp_auth_request_module模块进行处理

ngx_http_auth_request_module使用

nginx配置文件

server {
        listen       8082;
        server_name  localhost;

        location /private {
            auth_request /auth;
            # 鉴权通过后的处理方式
            proxy_pass http://127.0.0.1:8002/auth/success;
        }

        location = /auth {
            # 鉴权服务器的地址
            proxy_pass http://127.0.0.1:8002/auth/token;
            proxy_pass_request_body off;
            proxy_set_header Content-Length "";
            proxy_set_header X-Original-URI $request_uri;
         }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
}

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() {
        System.out.println("请求认证服务器接口" + LocalDateTime.now());
        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;
    }
}

测试模拟认证成功

浏览器访问地址:http://localhost:8082/private

nginx auth_request,中间件服务,nginx,http,服务器

控制台打印:
nginx auth_request,中间件服务,nginx,http,服务器

模拟认证失败,抛出异常

代码变动:
nginx auth_request,中间件服务,nginx,http,服务器

重启项目访问测试:
nginx auth_request,中间件服务,nginx,http,服务器
返回的是nginx的错误页面哦,也可以自定义处理文章来源地址https://www.toymoban.com/news/detail-584773.html

到了这里,关于Nginx ngx_http_auth_request_module模块鉴权的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • nginx配置SSL数字证书、报错nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in

    nginx一般默认安装目录是 /usr/local/nginx,请按自己实际安装目录操作 名称 命令 查看版本 /usr/local/nginx/sbin/nginx -v 启动服务 /usr/local/nginx/sbin/nginx 重启服务 /usr/local/nginx/sbin/nginx-s restart 停止服务 /usr/local/nginx/sbin/nginx -s stop 是否启动 ps -ef | grep nginx 强制结束 kill 进程ID (如:kil

    2024年02月15日
    浏览(38)
  • Nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf

      出现上面标题中的问题是因为我当初在安装Nginx的时候没有安装SSL模块,但是现在我在Nginx配置文件(nginx.cnf)中配置了SSL的相关配置信息。当我再次启动Nginx的时候就提示我 nginx: [emerg] the \\\"ssl\\\" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:111 了。下面我将我的解

    2024年02月07日
    浏览(30)
  • [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:35

    这个错误提示表明在Nginx配置文件(通常是nginx.conf)中使用了SSL(Secure Sockets Layer)相关的配置,但是Nginx没有加载相应的SSL模块。 1.检查Nginx是否编译了SSL模块: /usr/local/nginx/sbin/nginx -V 21 | grep --color=auto ssl /usr/local/nginx/sbin/nginx:安装nginx的绝对路径 输出以下结果 则为安装已

    2024年03月16日
    浏览(42)
  • ngx_http_request_s

    ngx_http_headers_out_t  ngx_list_t

    2024年02月06日
    浏览(22)
  • 解决Nginx启动报错“nginx: [emerg] unknown directive “ngx_fastdfs_module“ in /xxx/nginx/conf/nginx.conf:43“

    centos7操作系统,nginx被人安装过多次,重启服务器,发现nignx未设置开机自启,启动报错nginx: [emerg] unknown directive “ngx_fastdfs_module” in /usr/local/nginx/conf/nginx.conf:43 1、尝试启动,查看报错日志 上述命令如果报nginx.service找不到: Failed to start nginx.service: Unit not found. ,则进入ngi

    2024年02月07日
    浏览(63)
  • /usr/lib64/nginx/modules/ngx_stream_module.so: undefined symbol: SSL_CTX_set_options

    概述 系统是centos 7 问题是加入stream出现问题。 加入stream模块 重新编译 nginx.conf 第一行 ./nginx -t 如果出现以下异常 则直接拷贝源码objs中的文件

    2024年02月13日
    浏览(27)
  • nginx http rewrite module 详解

    大家好,我是 17。 今天和大家聊聊 nginx http rewrite module 。 简单来说, ngx_http_rewrite_module module 用正则匹配请求,改写请求,然后做跳转。可以是内部跳转,也可以是外部跳转。 学习这个模块的时候,把 rewrite_log 打开,可以在 error log 里查看跳转信息 注意 notice 是必须的 直接

    2023年04月08日
    浏览(26)
  • Linux安装Nginx并集成nginx-http-flv-module

    下载 Nginx: https://nginx.org/download nginx-http-flv-module: https://github.com/winshining/nginx-http-flv-module nginx可自行选择版本下载 ,nginx-http-flv-module可以通过git下载 解压安装 查找nginx安装目录 修改成如下配置 启动 cd ../sbin ./nginx

    2024年02月15日
    浏览(27)
  • Nginx + RTMP + nginx-http-flv-module 环境搭建(CentOS 7)

    🎉Nginx + RTMP + nginx-http-flv-module 环境搭建(CentOS 7) ☆* o(≧▽≦)o *☆嗨~我是IT·陈寒🍹 ✨博客主页:IT·陈寒的博客 🎈该系列文章专栏:Java学习路线 📜其他专栏:Java学习路线 Java面试技巧 Java实战项目 AIGC人工智能 数据结构学习 🍹文章作者技术和水平有限,如果文中出现

    2024年02月05日
    浏览(68)
  • python Requests添加Auth和Cert

    最近通过postman发起另外一个项目的请求,鉴权较为复杂,首先需要设置cert,postman中设置如下: 需要设置basic auth的参数 然后将接口返回的token放入auth的bearer token中。 实际使用时发现使用postman的数据驱动功能较为麻烦,而且不利于对测试结果日志采集,于是通过requests实现该

    2024年02月12日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包