0
我有安全約束的servlet的,在它的web.xml中象下面這樣:如何知道HttpServletRequest是否受到<security-constraint>的約束?
<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);
}
}
所以我需要知道的請求是否是在安全性約束或沒有。我怎麼知道它以編程方式?它有可能嗎?