如何仅在Spring上启用HTTPS

时间:2016-08-18 18:20:14

标签: spring https

我发现了这个:

<security:http auto-config="true">
    <security:form-login .../>
    <security:logout .../>

    <security:intercept-url pattern="/reports" access="ROLE_ADMIN" requires-channel="https"/>
    <security:intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY" requires-channel="https"/>
    <security:intercept-url pattern="/**" access="ROLE_USER" requires-channel="https"/>
</security:http>

根据我的理解,我们必须将它放在web.xml中,但是我们没有使用web.xml,我们使用的是java配置。我怎样才能做到这一点?我可以在application.properties中添加任何内容吗?

1 个答案:

答案 0 :(得分:0)

您展示的是一个弹簧安全文件。可以使用XML文件(如您正在显示的文件)或Java配置(请参阅此处:http://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html)配置Spring安全性。

但是您的问题是:我是否可以仅启用HTTPS。

你也可以通过其他方式做到这一点。 例如,在Tomcat中,您可以通过配置server.xml(http://www.itworld.com/article/2769041/development/how-to-configure-tomcat-to-always-require-https.htmlhttps://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html)来实现此目的。

其他JavaEE服务器也有类似的方法。

您还可以在web.xml(或基于Java的Web配置)中使用SecurityConstraint,以便它定义类似于CONFIDENTIAL或INTEGRAL(XML片段,但您可以通过基于Java的配置来完成):

<user-data-constraint>
    <transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>