繁体   English   中英

我怎么知道HttpServletRequest是否受制于 <security-constraint> 或不?

[英]How do I know whether HttpServletRequest is subject to <security-constraint> or not?

我有一个带有安全性约束的servlet,它的web.xml如下所示:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Admin</web-resource-name>
        <url-pattern>/admin/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>

以上强制切换到https协议并正常工作。 但是在安全页面上有一些与不安全页面相关的链接。 当用户点击它们时,它们通过https打开,我想避免。 将相对链接转换为绝对链接不是一种选择。 Servlet规范没有提供强制不安全连接的方法,因此我将实现一个过滤器,将用户重定向到http:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    if(!isSubjectToAuthConstraint(request)) {
        // Check protocol and redirect to http if https
        // ....
    } else {
        // Do nothing, managed by servlet spec
        filterChain.doFilter(request, response);
    }
}

所以我需要知道请求是否受到安全约束。 我如何以编程方式知道它? 有可能吗?

Hy,正常情况下https端口是443.通过在浏览器中输入https://www.example.com:443/welcome.html ,浏览器将其扩展到https://www.example.com/welcome.html

也许这就是你需要的:

String serverAddress = "";    
String serverName = request.getServerName( );
String serverPort = "" + request.getServerPort( );

if( request.isSecure( ) ) {
  serverAddress = "https://" + serverName + ":" + serverPort;
} else {
  serverAddress = "http://" + serverName + ":" + serverPort;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM