当前位置: 移动技术网 > IT编程>开发语言>Java > Java+Nginx实现POP、IMAP、SMTP邮箱代理服务

Java+Nginx实现POP、IMAP、SMTP邮箱代理服务

2019年07月22日  | 移动技术网IT编程  | 我要评论

这篇文章介绍了java+nginx实现pop、imap、smtp邮箱代理服务,我们本次使用的环境为centos7下,java程序我们通过eclipse导出的war包运行在linux下的tomcat下执行的,具体见下:

环境介绍:

hostname:java.iternalsoft.com

ip:192.168.2.163

roles: proxy server os:centos7

我们通过以下命令来修改新安装的服务器信息:

hostnamectl set-hostname customname 修改计算机名

systemctl stop firewalld.serice 停止防火墙服务

systemctl disable firewall.service 禁止防火墙服务随机启动

systemctl stop postfix 停止postfix服务

systemctl disable postfix 禁用postfix服务随机启动

vim /etc/selinux/config 修改selinux的状态为disabled

我们首先是安装nginx服务,在安装nginx前我们需要安装nginx的仓库

yum install

http://nginx.org/packages/centos/7/noarch/rpms/

nginx-release-centos-7-0.el7.ngx.noarch.rpm

repo仓库安装好后,我们就开始安装nginx了

yum install nginx

接下来查看安装默认路径

/etc/nginx/nginx.conf

find -name nginx

接下来我们要配置nginx的配置文件

vim /etc/nginx/nginx.conf 默认配置文件

我们为了方便需要将该配置文件内的内容全部清空,使用以下命令来完成

echo >/etc/nginx/nginx.conf 清空nginx.conf内的内容

但是我们为了配置完全,建议首先备份一下

cp /etc/nginx/nginx.conf /etc/nginx/cginx.conf.bak

然后再运行

echo > /etc/nginx/nginx.conf 清空配置文件

然后再次编辑该配置文件

vim /etc/nginx/nginx.conf

添加以下代码,然后根据自己的环境修改代码内容保存即可

user nginx;

worker_processes 4;

pid /var/run/nginx.pid;

events{

worker_connections 1024;

} error_log /var/log/nginx/error.log info;

mail {

server_name java.abc.com;

auth_http http://localhost:8080/imail/index.jsp;

imap_capabilities imap4rev1 uidplus idle literal+ quota;

pop3_auth plain apop cram-md5;

pop3_capabilities last top user pipelining uidl;

smtp_auth login plain cram-md5;

smtp_capabilities "size 10485760" enhancedstatuscodes 8bitmime dsn;

xclient off;

server {

listen 110;

protocol pop3;

proxy on;

proxy_pass_error_message on;

}

server {

listen 143;

protocol imap;

proxy on;

}

server {

listen 25;

protocol smtp;

proxy on;

}

}

粘贴保存

接下来设置nginx服务

systemctl enable nginx.service nginx服务开启随机启动

systemctl start nginx.service nginx服务启动

systemctl status nginx.service nginx服务运行状态

接下来就是安装java运行环境了(jdk),java运行环境,在此安装tomcat

首先确认的是linux一般都是自带java环境的jdk

接下来就是准备安装新的jdk文件,首先呢我们运行一下命令

uname -a

查看系统的版本及位数是32还是64位

如果有x86_64的是64位操作系统,如果没有那就是32位的

下载安装jdk

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

wget

我们通过ls查看下载的文件,然后我们需要解压

tar -zxvf jdk1.8.0……..

解压完成,无需安装

然后我们通过cd命令进入该路径

在/usr/创建java的目录,然后将解压的jdk移动到该目录

cd /usr/ 进入usr目录

mkdir java 创建java文件夹

然后我们将当前目录的jdk1.8….解压文件移动到java目录下即可
1
mv jdk1.8.xx /usr/java

vim /etc/profile 编辑环境变量
添加环境变量
java_home=/usr/java/jdk1.8.0_45
jre_home=/usr/java/jdk1.8.0_45/jre
path=$path:$java_home/bin:$jre_home/bin
classpath=.:$java_home/lib/dt.jar:$java_home/lib/tools.jar:$jre_home/lib
export java_home jre_home path classpath

我们保存后退出,然后重启系统,发现jdk的版本已经是我们新安装的版本了

接下来是我们需要安装tomcat了
首先是下载

我们通过打开tomcat官网找到合适的安装包后,右击复制下载链接,通过wget在线下载
wget

下载完成


下载后,我们就可以开始解压了
tar -zxvf apache_tomcat1.8.-0 tar.gz

解压完成

我们需要将apache-tomcat文件移动到 /usr/即上

mv apache-tomcat /usr

接下来启动tomcat服务

cd /usr/apache-tomcat18.0.23/bin

./startup.sh

然后我们查看tomcat相关服务端口

netstat -anlpt

centos7下需要单独安装net-tools 服务

yum install -y net-tools

netstat -anlpt

然后我们使用ie浏览器进行访问

我们也可以通过修改apache-tomcat默认的服务端口

在apache-tomcat/conf/server.xml下

hashenv.put(context.security_authentication, "simple"); // ldap访问安全级别(none,simple,strong)
hashenv.put(context.security_principal, "cn="+username+",o=beyondsoft");
hashenv.put(context.security_principal, username); //ad的用户名
hashenv.put(context.security_credentials, password); //ad的密码
hashenv.put(context.initial_context_factory,"com.sun.jndi.ldap.ldapctxfactory");
// hashenv.put(com.sun.jndi.ldap.connect.timeout, 3000);//连接超时设置为3秒
hashenv.put(context.provider_url, "ldap://"+host+":"+port);// 默认端口389
try {
ctx = new initialdircontext(hashenv);// 初始化上下文
attributes attrs = ctx.getattributes("cn="+username+",o=beyondsoft"
);
system.out.println("mailserver: " + attrs.get("mailserver").get());
string attdomain=attrs.get("mail").get().tostring();
string attserver=attcn.split(",")[0].split("=")[1]+"." + attdomain.substring(attdomain.indexof("@")+1) ;
system.out.println(inet4address.getbyname(attserver).gethostaddress());
return attcn.split(",")[0].split("=")[1];

然后我们需要定制java运行程序,其实就是.war包

定义好程序后,我们就将程序的代码导出为.war包,然后通过winscp或者mount的方式拷贝到tomcat的安装目录下的webapp下,启动tomcat服务器系统会自动将war包解压

tomcat服务启动后,我们通过浏览器访问,提示500页面,其实500的页面是正常的,因为我们在程序中是post请求传输,所以是无法打开页面的,

接下来我们我了方便测试代码是否有问题,我们就通过以下命令来测试一下:
curl -i -h 'auth-user: iiosoft' -h 'auth-pass: 123' -h 'auth-protocol: pop3'

我们测试你可以正常返回auth-sever,auth-status,auth-ports,auth-user,auth-pass等信息,所以没有问题,我们这样可以通过telnet mailserver 110的方式进行测试验证,但是在验证 过程中发现无法验证,代码确实是没有问题的,那怎么会验证失败呢

我们经过查找资料确认,nginx需要通过ldap返回的是auth-server必须是服务器的ip地址,如果是服务器名称的话无法解析,那我们只能通过在代码中进行转化了,默认返回的是服务器名称

hashenv.put(context.security_authentication, "simple"); // ldap访问安全级别(none,simple,strong)
hashenv.put(context.security_principal, "cn="+username+",o=beyondsoft");
hashenv.put(context.security_principal, username); //ad的用户名
hashenv.put(context.security_credentials, password); //ad的密码
hashenv.put(context.initial_context_factory,"com.sun.jndi.ldap.ldapctxfactory");
// hashenv.put(com.sun.jndi.ldap.connect.timeout, 3000);//连接超时设置为3秒
hashenv.put(context.provider_url, "ldap://"+host+":"+port);// 默认端口389
try {
ctx = new initialdircontext(hashenv);// 初始化上下文
attributes attrs = ctx.getattributes("cn="+username+",o=beyondsoft"
);
system.out.println("mailserver: " + attrs.get("mailserver").get());
system.out.println("mailserver: " + attrs.get("mail").get());
string attdomain=attrs.get("mail").get().tostring();
string attcn=attrs.get("mailserver").get().tostring();
string attserver=attcn.split(",")[0].split("=")[1]+"." + attdomain.substring(attdomain.indexof("@")+1) ;
// system.out.println(inet4address.getbyname(attserver).gethostaddress());
//return attcn.split(",")[0].split("=")[1];
return inet4address.getbyname(attserver).gethostaddress().tostring();


接下来我们通过修改代码来解决该问题:
怎么解决的呢,我们在初始化前,想到ldap上取到用户的mailserver和mailaddress值,得到的格式为a1/server,user@domain.com,然后我们可以在左侧取a1,右侧取@之后的内容,然后再次拼接就是一个服务器地址。a1.domain.com;最后我们通过再用inetaddress.getbyname 转换成ip 就ok了

转换成ip后,我们再次测试;

我们同样也通过outlook进行测试

测试成功后,我们通过在dns中添加一个解析指向192.168.2.163,然后我们通过域名再次测试
如果要让nginx代理smtp的话,我们需要编辑nginx.conf添加配置文件
server {
listen 25;
protocol smtp;
smtp_auth login plain;
xclient off;
}
注:我们因为在nginx下添加smtp代理,所以我们添加smtp的server区域

我们加上后重启发现提示nginx重启失败

我们想到提示端口已使用,想到系统自带的postfix是启动的
systemctl stop postfix
systemctl disable postfix
然后我们就重启启动nginx

我们通过outlook配置进行测试

以上就是本文的全部内容,希望大家能够喜欢。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网