struts实现https和http之间的转换

 本人已在tomcat上配置了ssl协议,测试后运行成功。问题是现在所有在该tomcat上跑的项目都要用https方式去访问,现本人想通过一些方法能使访问在https与http之间切换。 
    本人现在用的框架是struts2,在网上找了个struts2的ssl插件 struts2-ssl-plugin 但是尝试了很久还是未果,每次都是以http方式访问时就出现页面不存在,而且还不是tomcat的报错页面,本人在网上搜索相关资料,收获甚少,希望各位牛人能帮助本人解答下。 
    以下是部分代码, 
    struts.xml: 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.       
  6. <struts>  
  7.     <constant name="struts.custom.i18n.resources" value="Resource"/>  
  8.     <constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.Restful2ActionMapper"></constant>  
  9.     <constant name="struts.enable.SlashesInActionNames" value="true"></constant>  
  10.     <constant name="struts2.sslplugin.httpPort" value="8080"></constant>  
  11.     <constant name="struts2.sslplugin.httpsPort" value="8443"></constant>  
  12.     <constant name="struts2.sslplugin.annotations" value="true"></constant>  
  13.     <include file="helloworld.xml"></include>  
  14. </struts>  


------------------------------------------------------------------------------------------------------------------------

    helloworld.xml: 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <package name="SSL" extends="ssl-default">  
  8.         <interceptors>  
  9.             <interceptor-stack name="SecureStack">  
  10.             <interceptor-ref name="secure">  
  11.                 <param name="httpsPort">443</param>  
  12.              </interceptor-ref>  
  13.              <interceptor-ref name="defaultStack"/>  
  14.             </interceptor-stack>  
  15.         </interceptors>  
  16.         <action name="helloWorldSSL/*" class="com.product.action.HelloWorldSSL">  
  17.             <result>/example/methodShow.jsp</result>  
  18.         </action>  
  19.     </package>  
  20. </struts>  

  HelloWorldSSL.java: 

Java代码   收藏代码
  1. package com.product.action;  
  2.   
  3. import com.googlecode.sslplugin.annotation.Secured;  
  4.   
  5. public class HelloWorldSSL extends SupportAction {  
  6.   
  7.     /** 
  8.      *  
  9.      */  
  10.     private static final long serialVersionUID = -4121768069718140948L;  
  11.       
  12.     public String doIt(){  
  13.         super.request.setAttribute("result""It's not SSL.");  
  14.         return super.SUCCESS;  
  15.     }  
  16.       
  17.     @Secured  
  18.     public String doItSSL(){  
  19.         super.request.setAttribute("result""It's SSL.");  
  20.         return super.SUCCESS;  
  21.     }  
  22.   
  23. }  


-------------------------------------------------------------------------------------------------------------------- 

     ssl.jsp: 

Java代码   收藏代码
  1. <%@ page language="java" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.   
  11.   </head>  
  12.     
  13.   <body>  
  14.     <form action="<%= basePath %>helloWorldSSL/!doIt" method="get">  
  15.         <input type="submit" value="edit"/>  
  16.     </form>  
  17.     <form action="<%= basePath %>helloWorldSSL/!doItSSL" method="get">  
  18.         <input type="submit" value="edit"/>  
  19.     </form>  
  20.   </body>  
  21. </html>  


----------------------------------------------------------------------------------------------------------------------- 

     methodShow.jsp: 

Java代码   收藏代码
  1. <%@ page language="java" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.   </head>  
  11.     
  12.   <body>  
  13.     ${result }  
  14.   </body>  
  15. </html>  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值