最近开发的应用需要外协人员实现登录认证,外协人员的密码等信息已经录入到ldap, 需要连接ldap进行登录认证。下面先介绍一下登录的网络旅程图。

一.nginx实现AES加密
nginx请求处理入口(前端请求为json格式)
location /aes {
default_type text/html;
content_by_lua_block{
local access_filter = require 'resty.aes_auth'
local r = access_filter.aes_auth()
ngx.header.content_type = "application/json; charset=UTF-8"
if r == true then
ngx.say([[{"code":200,"message":"Certification successful!","data":true,"logCode":null}]])
else
ngx.say([[{"code":401,"message":"Authentication failed!","data":false,"logCode":null }]])
end
ngx.exit(200)
}
}
openresty请求认证接口脚本
local aes = require "resty.aes"
local cjson = require("cjson.safe")
local http = require("resty.http")
local key = "abcdefmJTNn}8Z#2`"
local iv = "1234567890123456"
local _M = {}
function _M.aes_auth()
ngx.req.read_body()
local args,err = ngx.req.get_body_data()
if (not args) or (err) then
return false
end
local arg_json = cjson.decode(args)
local username = arg_json.username
local password = arg_json.password
if (not username) or (not password) then
return false
end
local cript = aes:new(key, nil, aes.cipher(128, "cbc"), {iv=iv, method=nil})
local pwd = cript:encrypt(password)
if pwd then
pwd = ngx.encode_base64(pwd)
else
return false
end
local httpc = http.new()
local requestBody = {
username = username,
password = pwd
}
local json_body = cjson.encode(requestBody)
local resp,err = httpc:request_uri("http://10.1.1.1:8080", {
method = "POST",
path = "/ldap/authUser",
body = json_body,
headers = { ---header参数
["Content-Type"] = "application/json;charset=UTF-8"
}
})
if err then
return false
end
local result = false
if resp then
local data = cjson.decode(resp.body).data
if data then
result = data
end
end
return result
end
return _M
二.应用服务调用ldap服务
引入依赖
<!--ldap-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<!--aes对称加密-->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.56&l

本文介绍了如何在使用OpenResty和SpringBoot的应用中,通过AES加密处理用户密码并与LDAP进行安全登录认证的过程,包括Nginx的AES加密入口、OpenResty的认证接口以及SpringBoot的LDAP配置和认证服务实现。
最低0.47元/天 解锁文章
4947

被折叠的 条评论
为什么被折叠?



