Как реализовать HTTPS для нужных страниц в приложении? - PullRequest
3 голосов
/ 17 ноября 2011

Мы пытаемся реализовать HTTPS для некоторых страниц в нашем приложении. Итак, мы изменили tomcat server.xml, чтобы выполнять вызовы HTTPS следующим образом:

<Connector
           port="8080"
           protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443"
           acceptCount="100"
           maxKeepAliveRequests="15"
           SSLEnabled="true"
           scheme="https"
           secure="true"
     clientAuth="false" sslProtocol="TLS"
     keystoreFile="/webapps/test.bin"
           keystorePass="test"/>

В приложении web.xml:

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

Итак, HTTPS применяется для всех страниц. Как ограничить HTTPS для нужных страниц.

Помощь будет оценена.

Ответы [ 3 ]

4 голосов
/ 17 ноября 2011

Spring Security Interceptor имеет параметр requires-channel. Установите для этого параметра значение https, чтобы применить его для шаблонов URL, соответствующих перехватчику.

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:security="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.4.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

   <security:http>
       <security:intercept-url pattern="/login" access="permitAll"
           requires-channel="https"/>
   </security:http> 

</bean>
2 голосов
/ 01 декабря 2011

Создайте следующий класс

public class RestHttpRequestFilter implements Filter {

   public void destroy() {

   }

   public void doFilter(ServletRequest servletRequest,
                ServletResponse servletResponse, FilterChain filterChain)
                throws IOException, ServletException {
     // if the ServletRequest is an instance of HttpServletRequest
     if (servletRequest instanceof HttpServletRequest) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
            System.out.println(httpServletRequest.getRequestURL());
            if (httpServletRequest.getRequestURL().toString().contains("/user/account")
                        && servletRequest.getProtocol().contains("HTTP")) {
                    throw new ResourceNotFoundException(
                            "The url should be HTTPS");
           }
       filterChain.doFilter(httpServletRequest, servletResponse);
     } else {
           // otherwise, continue on in the chain with the ServletRequest and
           // ServletResponse objects
           filterChain.doFilter(servletRequest, servletResponse);
     }  
     return;
   }

   public void init(FilterConfig filterConfig) throws ServletException {}

}

web.xml entry

    <filter>
        <filter-name>simpleFilter</filter-name>
        <filter-class>RestHttpRequestFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>simpleFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
1 голос
/ 17 ноября 2011

Простое решение - использование HttpFilter, который проверит протокол и шаблон URL и решит, следует ли переадресовать вызов в приложение или выдать исключение, которое приведет к тому, что пользователь увидит страницу ошибки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...