关于apache+tomcat+ssl配置问题

haobaofufob 2008-12-08 08:40:14
我现在使用apache+tomcat+ssl配置项目,想要实现对项目登陆部分、发卡操作部分是想ssl验证,在项目的web.xml配置文件中做了如下配置:<security-constraint>
<web-resource-collection>
<web-resource-name>SSL</web-resource-name>
<url-pattern>/common/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>,已经能实现由首页进入登陆部分自动开启ssl验证了(这个我是通过url的变化判断的),然而登陆后我想实现其它操作不进行ssl验证,现在是登陆后再进行其它操作仍然开启ssl验证,不知道如何解决这个问题,实现http和https自由转换。谢谢大家了!!
...全文
353 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
haobaofufob 2008-12-08
  • 打赏
  • 举报
回复
紫竹:您好,
我现在的思路是,写一个方法判断请求的类型,如果是比如注册页面不需要ssl验证,那么在注册页面中获取请求类型,如果为https的则将页面的请求重定向到http的相应链接,这种方法是否可行呢?
我得代码如下:
String scheme = "http";
scheme = request.getScheme();
if(null != scheme && scheme.toLowerCase().equals("https"))
{
StringBuffer url = request.getRequestURL();
//替换协议
int schemeStart = 0;
int schemeEnd = url.indexOf(":");
url.replace(schemeStart,schemeEnd,"http");
//替换端口号
int portStart = url.indexOf(":",6);
int portEnd = url.indexOf("/",url.indexOf(":",6));
url.replace(portStart + 1,portEnd,"8010");
//重定向
response.sendRedirect(url.toString());
}
可是这样出现一个死循环问题,不知道如何解决,还有一个就是重定向的路径这样的方法获取是否正确呢?
老紫竹 2008-12-08
  • 打赏
  • 举报
回复
我不知道太正规的做法。我的做法如下
1 启动ssl 访问
2 login的连接为 https 的,
3 loing 的程序判断是否为https的,如果不是拒绝登录
4 其它的全部转向到 http

也就是说,
http的,除了login 其它的都能访问
https 的,原则上都能访问
xingchen228 2008-12-08
  • 打赏
  • 举报
回复
关注
無名VF 2008-12-08
  • 打赏
  • 举报
回复
up
haobaofufob 2008-12-08
  • 打赏
  • 举报
回复
<url-pattern>/common/* </url-pattern> ,我将登陆页面放在这个common文件夹下了,目前只想对这个文件夹下的文件做ssl验证
老程序员 2008-12-08
  • 打赏
  • 举报
回复
<url-pattern>/common/* </url-pattern> 是对所有验证的呀..应当有办法.呵呵
sunwei_07 2008-12-08
  • 打赏
  • 举报
回复
运行环境:Windows2003 Server SP4 J2SDK1.5.0 Apache2.0.54 Tomcat5.5.9



预备软件:



1.安装 Apache 2.0.54



2.Tomcat 5.5.14



3.Jk2连接器(mod_jk2.so)



一、配置Apache和tomcat



Apache安装在d:\Apache2 下,监听端口 80;



Tomcat在D:\Tomcat51 下,监听端口 8080;



两者都以windows 2000服务进行安装。



将mod_jk2.so复制到d:\Apache2\modules目录下:



1.编辑d:\Apache2\conf\httpd.conf文件,在LoadModule那一段代码中添加:



LoadModule jk2_module modules/mod_jk2.so



2.将AddDefaultCharset ISO-8859-1 改为 AddDefaultCharset GB2312 使Apache自动支持中文显示;



3.在“虚拟主机配置代码段”中添加:(此段配置可以根据你的实际情况进行修改)



<VirtualHost *:80>
ServerAdmin linvsfen◎163.com DocumentRoot "d:/ Tomcat51/webapps/jsp-examples
ServerName localhost ErrorLog logs/error_log CustomLog logs/access_log common
</VirtualHost>

编写workers2.properties文件并加入d:\Apache2\conf目录,内容如下:



# Usually commented out onproduction environments[logger.apache2]file=”
D:/Apache2/logs/error.log”level=ERROR# Provide the basic config needed[config]
file=D:/Apache2/conf/workers2.propertiesdebug=1# Provide the location of shm file
on the Apache web server[shm]file=D:/Apache2/conf/jk2.shmsize=1000000
[channel.socket:localhost:8009]port=8009host=localhost#define the worker
[ajp13:localhost:8009]channel=channel.socket:localhost:8009# Map the Tomcat
examples webapp to the Web server uri space [uri:/jsp-examples/*][uri:/*.jsp]

编写一个jsp文件(sessiontest.jsp),放在d:\Tomcat51\webapps\jsp-examples 下;重新启动以后,启动apache和tomcat,访问http://localhost/jsp-examples/sessiontest.jsp 可以访问。

二、Tomcat配置上ssl步骤:

1. 生成服务器证书server02.jks和客户端证书dwp.p12



2. 把server02.jks放到conf目录下,打开d:\ tomcat51\conf\server.xml 文件,



然后修改server.xml文件,将注释掉的内容的注释去掉和增加证书存放的位置:如下:



<Connector port="8443" maxHttpHeaderSize="8192"maxThreads="150" minSpareThreads="25"
maxSpareThreads="75"enableLookups="false" disableUploadTimeout="true"acceptCount="100"
scheme="https"secure="true"clientAuth="true" sslProtocol="TLS"
keystoreFile="conf\server02.jks"
keystorePass="123456" truststoreFile="conf\server02.jks"
truststorePass="123456"/>



注:server02.jks是证书存贮文件



安装客户端证书dwp.p12



3. 修改Tomcat配置,使其自动启用SSL



打开d:\test\tomcat51\webapps\jsp-examples\WEB_INF\web.xml 文件,在security-constraint 标签下,增加如下内容:

<user-data-constraint>
<description>
Constrain the user data transport for the whole application
</description>
<transport-guarantee> CONFIDENTIAL </transport-guarantee>
</user-data-constraint>
<!--注释掉<auth-constraint>标签下的内容-->
<auth-constraint>
<!-- Anyone with one of the listed roles may access this area -->
<role-name>tomcat</role-name>
<role-name>role1</role-name>
</auth-constraint>
<!--在web-resource-collection 标签下,增加如下内容:-->
<url-pattern>/*</url-pattern>



重新启动Apache和tomcat,访问 http://localhost/jsp-examples/sessiontest.jsp是可是访问的,这是系统提示用户出示证书,启用SSL加密通道,SSL建立后,返回结果。URL变为https://localhost:8443/jsp-examples/sessiontest.jsp
melack2008 2008-12-08
  • 打赏
  • 举报
回复
学习。。。。。。
老紫竹 2008-12-08
  • 打赏
  • 举报
回复
怎么会死循环呢?

你要处理的只有登录的那个action而已,别的不用处理!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧