Java 如何测试URL是否受SSL保护
我是IT行业的新手。测试场景就像我需要测试我的应用程序的登录页面是否受SSL保护一样 一般来说,有时我们会访问一些网站,其中会显示一个用于SSL安全的弹出窗口。所以我需要在我的应用程序中测试相同的场景 我有一个小型web应用程序,其中有Java 如何测试URL是否受SSL保护,java,maven,tomcat,ssl,web-applications,Java,Maven,Tomcat,Ssl,Web Applications,我是IT行业的新手。测试场景就像我需要测试我的应用程序的登录页面是否受SSL保护一样 一般来说,有时我们会访问一些网站,其中会显示一个用于SSL安全的弹出窗口。所以我需要在我的应用程序中测试相同的场景 我有一个小型web应用程序,其中有login.htmlpage。基本上,我可以使用Maven启动我的web应用程序,使用的服务器是Tomcat。我用来启动的命令是mvntomcat7:run和URL使用http://localhost:8080/login.html。它工作得很好 <pre&
login.html
page。基本上,我可以使用Maven启动我的web应用程序,使用的服务器是Tomcat。我用来启动的命令是mvntomcat7:run
和URL使用http://localhost:8080/login.html
。它工作得很好
<pre><code><!DOCTYPE web-app PUBLIC <span style="color: red;">"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"</span> <span style="color: red;">"http://java.sun.com/dtd/web-app_2_3.dtd"</span>>
<web-app>
<!-- <security-constraint>
<web-resource-collection>
<web-resource-name>MyEducationApp</web-resource-name>
<url-pattern>/login.html</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Non-SecureResource</web-resource-name>
<url-pattern>/login.html</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint> -->
<display-name>Login WebApp</display-name>
</web-app>
</span></code></pre>
但是我想将我的URL从http
更改为https
,当我访问我的URL时,即更改为https://localhost:8080/login.html
,然后它会弹出SSL安全警报,我应该接受它
如果我的问题仍然不清楚,请随意评论
在网上搜索后,我做了一些变通办法,但没有奏效。我所尝试的:
我的HTML页面
登录应用程序
名字:
姓氏:
电子邮件Id:
web.xml
登录WebApp
使用Maven插件
org.apache.tomcat.maven
tomcat7 maven插件
2.2
https://localhost:8080/manager/text
本地服务器
/
管理
阿法拉
tomcat7跑步
只管打仗
预集成测试
真的
tomcat7关机
关闭
整合后测试
在web应用程序中,您无法对网站进行SSL/TLS加密。这是通过web服务器的配置完成的
看
其他信息(从我对OQ的评论中重复,因为评论没有那么突出和可编辑): 您不必从(CA)之一购买证书即可获得证书
https://
站点相关联。如果您的访问者在浏览器中弹出的对话框中接受您的证书,该对话框将存储在浏览器的证书存储中,并且在证书过期之前,该对话框不会再次出现
通常的做法是通过request.isSecure()检查请求是否通过https传入。如果没有,则向浏览器发送重定向到相同的URL,但前缀为https协议 下面是执行此操作的示例servlet筛选器:
<filter>
<filter-name>securityFilter</filter-name>
<filter-class>SecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>securityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
要全局启用筛选器,请将以下内容添加到web.xml:
<!-- Maven Tomcat Plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<httpsPort>8443</httpsPort>
. . .
您还需要在SecurityFilter代码中为重定向目标添加正确的协议(或使其成为参数):
端口8080和8443仅用于实验性本地web服务器,实际应用程序应位于端口80和443上
就这样。祝你玩得开心,好运 要要求HTTPS并让servlet引擎自动重定向到HTTPS,您的传输保证是正确的 所以你可能想要
受保护上下文
/login.html
保密的
以上内容只会将您的webapp的/login.html重定向到https。根据需要添加更多url模式
更多详情:
及
这是您需要做的:
https://local_host:8443/login.html
(Tomcat的默认SSL端口)如果您希望仅通过SSL访问此页面,请查看Tim Funk的帖子并编辑应用程序的
web.xml
。来自上面原始问题:
我用来启动的命令是mvntomcat7:run
和URL
http://localhost:8080/login.html
。它工作得很好。但是我想
将我的URL从http
更改为https
,当我访问我的URL时,即
https://localhost:8080/login.html
你确定<代码>'http://localhost:8080“和”https://localhost:8080“
这基本上意味着您正在从同一端口请求SSL和非SSL通信。通常Tomcat从8080执行HTTP,从8443执行HTTPS
这里的大多数答案都适用于您,但首先请确保您是否已在
server.xml
中启用SSL连接器浏览器只接受有效的证书,即
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SecurityFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse servletResponse = (HttpServletResponse) response;
if (!request.isSecure()) {
HttpServletRequest servletRequest = (HttpServletRequest) request;
String target = "https://" + request.getLocalName() + servletRequest.getRequestURI();
servletResponse.sendRedirect(target);
return;
}
// tell the browser to use only https for accessing this domain for the next 30 days
servletResponse.addHeader("Strict-Transport-Security", "max-age=" + (30 * 24 * 60 * 60));
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// not needed
}
@Override
public void destroy() {
// not needed
}
}
<filter>
<filter-name>securityFilter</filter-name>
<filter-class>SecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>securityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Maven Tomcat Plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<httpsPort>8443</httpsPort>
. . .
String target = "https://" + request.getLocalName() + ":8443" + servletRequest.getRequestURI();
<security-constraint>
<web-resource-collection>
<web-resource-name>Protected Context</web-resource-name>
<url-pattern>/login.html</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>