本人已在tomcat上配置了ssl协议,测试后运行成功。问题是现在所有在该tomcat上跑的项目都要用https方式去访问,现本人想通过一些方法能使访问在https与http之间切换。
本人现在用的框架是struts2,在网上找了个struts2的ssl插件 struts2-ssl-plugin 但是尝试了很久还是未果,每次都是以http方式访问时就出现页面不存在,而且还不是tomcat的报错页面,本人在网上搜索相关资料,收获甚少,希望各位牛人能帮助本人解答下。
以下是部分代码,
struts.xml:
------------------------------------------------------------------------------------------------------------------------
helloworld.xml:
本人现在用的框架是struts2,在网上找了个struts2的ssl插件 struts2-ssl-plugin 但是尝试了很久还是未果,每次都是以http方式访问时就出现页面不存在,而且还不是tomcat的报错页面,本人在网上搜索相关资料,收获甚少,希望各位牛人能帮助本人解答下。
以下是部分代码,
struts.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.custom.i18n.resources" value="Resource"/>
- <constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.Restful2ActionMapper"></constant>
- <constant name="struts.enable.SlashesInActionNames" value="true"></constant>
- <constant name="struts2.sslplugin.httpPort" value="8080"></constant>
- <constant name="struts2.sslplugin.httpsPort" value="8443"></constant>
- <constant name="struts2.sslplugin.annotations" value="true"></constant>
- <include file="helloworld.xml"></include>
- </struts>
------------------------------------------------------------------------------------------------------------------------
helloworld.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="SSL" extends="ssl-default">
- <interceptors>
- <interceptor-stack name="SecureStack">
- <interceptor-ref name="secure">
- <param name="httpsPort">443</param>
- </interceptor-ref>
- <interceptor-ref name="defaultStack"/>
- </interceptor-stack>
- </interceptors>
- <action name="helloWorldSSL/*" class="com.product.action.HelloWorldSSL">
- <result>/example/methodShow.jsp</result>
- </action>
- </package>
- </struts>
HelloWorldSSL.java:
--------------------------------------------------------------------------------------------------------------------
ssl.jsp:
-----------------------------------------------------------------------------------------------------------------------
methodShow.jsp:
- package com.product.action;
- import com.googlecode.sslplugin.annotation.Secured;
- public class HelloWorldSSL extends SupportAction {
- /**
- *
- */
- private static final long serialVersionUID = -4121768069718140948L;
- public String doIt(){
- super.request.setAttribute("result", "It's not SSL.");
- return super.SUCCESS;
- }
- @Secured
- public String doItSSL(){
- super.request.setAttribute("result", "It's SSL.");
- return super.SUCCESS;
- }
- }
--------------------------------------------------------------------------------------------------------------------
ssl.jsp:
- <%@ page language="java" pageEncoding="utf-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- </head>
- <body>
- <form action="<%= basePath %>helloWorldSSL/!doIt" method="get">
- <input type="submit" value="edit"/>
- </form>
- <form action="<%= basePath %>helloWorldSSL/!doItSSL" method="get">
- <input type="submit" value="edit"/>
- </form>
- </body>
- </html>
-----------------------------------------------------------------------------------------------------------------------
methodShow.jsp:
- <%@ page language="java" pageEncoding="utf-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- </head>
- <body>
- ${result }
- </body>
- </html>