Nginx 轉發鑑權

nginx 配置

 location /live {
        auth_request    /auth;
        proxy_pass      http://live_address;
    }
    
  # authentication URL
    location = /auth {
        proxy_pass      http://back_server/echo;
    }


需要nginx 安裝auth_request 模塊

後臺編寫接口

編寫接口echo 判斷用戶是否登錄,如果未登錄

@RestController
public class EchoController {

    @RequestMapping("echo")
    public void echo() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth instanceof AnonymousAuthenticationToken) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }
    }
}

nginx 安裝auth_request 模塊

–with-http_auth_request_module

一鍵安裝編譯腳本

Link: Nginx 常見配置

發佈了316 篇原創文章 · 獲贊 122 · 訪問量 47萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章