【问题标题】:How to develop https site with Spring 3.x?如何使用 Spring 3.x 开发 https 站点?
【发布时间】:2011-10-22 20:43:26
【问题描述】:

我是基于 Spring 的 Web 开发的新手。

我们的网站是基于 Spring 的,目前是基于 http 的(非常不安全)。 由于该网站尚未上线,我们也通过普通的 JSON 请求向服务器发送登录名/密码,并且主要关注 JSP、UI 设计、SQL 查询等。

现在,我们希望将重点转移到安全性上,并首先转向 https。

我读过没有。网页和一些春季书籍,但似乎没有一个提供关于如何使用 Spring 提供 https 安全性的明确答案。 有人可以帮我实现上述目标吗?

如果我的问题不清楚,请告诉我。我会尽快添加更多细节。

我们的web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
  http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
"
id="WebApp_ID" version="2.5">

<display-name>Spring3MVC</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

<!--> Mapping for serving static web-content <-->
<!--> The resources folder must be in parallel to WEB-INF <-->
<!--> The mvc:resources gives "not bound" exception unless bound to a namespace as above for xmlns:mvc <-->
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/scripts/**" location="/scripts/" />

</web-app>

目前只有一个控制器,spring-servlet.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan
    base-package="console.controllerpkg" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

</beans>

提前非常感谢!

附:如果您可以向我推荐一个很好的基于示例的春季网站/书籍,将不胜感激。我看到的大多数网站/书籍都非常强调理论,但很少有例子。这让我有点困惑。

【问题讨论】:

    标签: security spring https


    【解决方案1】:

    正如 Dave 所说,您需要配置您的容器以提供 SSL,然后将您的 spring 应用程序部署到该容器中。了解如何配置Tomcat for SSL.

    您可以更灵活地交替使用front your container using Apacheenable SSL there.

    【讨论】:

    • 感谢您提供的链接。明天我将通过它们,因为它对我来说是深夜。只是为了重新确认到目前为止已经回答的内容,我了解到:1)Spring与SSL无关。 2) SSL 将在 Tomcat 中启用。请确认这两点。再次非常感谢你们的回答。如果你们没有帮助我,我会继续挖掘 Spring。
    • 正确,spring 不直接处理 HTTP 或 HTTPS。它被部署到像 tomcat 或 jboss 这样的容器中,该容器用于处理 SSL 或常规 HTTP。然后将这些请求传递给 Spring 进行处理。所以,是的,如果你想启用 SSL,你应该在你的 tomcat 中做。顺便说一句,如果有帮助,请投票和/或将此答案标记为正确。这是人们喜欢在这里回答问题的一个重要原因!
    • 答案中的链接是针对 Tomcat 5.5,这是针对 Tomcat 7 的链接:tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html
    【解决方案2】:

    Spring 并非 100% 负责配置 SSL。为此,您需要配置容器(jetty、tomcat 等)来处理 SSL。

    【讨论】:

      【解决方案3】:

      感谢所有帮助的人。 我将重申我所做的只是为了我自己的记录目的。

      首先,nont 提供的关于“Tomcat for SSL”的链接非常有帮助。 我在那里阅读了有关 SSL 和 Tomcat 的所有信息,这就是我所做的:

      在命令提示符下,输入: keytool -genkey -alias tomcat -keyalg RSA 上面的命令问了我一些证书所需的简单问题。我在询问时使用了密码“changeit”(因为这是默认密码)。

      完成上述命令后,它在 C:/Documents and Settings//.keystore 中生成了一个密钥库文件 我将此 .keystore 文件复制到 tomcat/conf/myKeyStore.jks

      然后我将以下内容添加到 conf/server.xml :

      <Connector protocol="org.apache.coyote.http11.Http11Protocol"
          port="8443" minSpareThreads="5" 
         maxSpareThreads="75"
         enableLookups="true" 
         disableUploadTimeout="true"
         acceptCount="100" 
         maxThreads="200" debug="5"
         scheme="https" secure="true" SSLEnabled="true"
         keystoreFile="${catalina.home}/conf/myKeyStore.jks"
         keystoreType="JKS" keystorePass="changeit"
         truststoreFile="${catalina.home}/conf/cacerts"
         truststoreType="JKS" truststorePass="changeit"
         SSLVerifyClient="require" SSLEngine="on" SSLVerifyDepth="2" 
         sslProtocol="TLS" />
      

      就是这样!! 下一次,我运行 tomcat 我的旧 http 链接不起作用。 然后我尝试使用端口号 8443 将 sweet 's' 添加到 http 和 lo!一切都恢复正常了。

      谢谢你的精彩链接!!

      【讨论】:

      • 最后一件事,在为我的网络服务器启用 SSL 后,我可以确定我的网站的安全性是完整的吗?还是我也需要做其他事情?例如,我进行了很多 AJAX 调用来获取我所有页面的数据。服务器向每个 ajax 调用返回 JSON 响应。那样行吗?同样,在登录期间,AJAX 发布请求也使用 JSON 格式的用户名和密码。那安全吗?
      • 如果 url 以“https”开头,任何通过启用 ssl 的 tomcat 的内容都将被加密。它是否是ajax并不重要。听起来您可能生成了自己的证书?如果是这样,您可能想在生产时购买“真正的”证书。并不是说它会更安全,只是因为您的用户浏览器会警告他们不要使用自签名证书的网站。
      【解决方案4】:

      配置两个不同的网站,一个用于 http,一个用于 https,一个用于 http 将只重定向到 https 站点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-24
        • 1970-01-01
        • 1970-01-01
        • 2022-12-22
        • 1970-01-01
        • 2012-08-14
        相关资源
        最近更新 更多