2012-01-25 11 views
3

私はSpring Securityの新機能ですので、私はそれを試して、私が取り組んでいるプロジェクトに役立つ設定を見つけるために小さなwebappを作った。 私はHTTPSを介してアクセスするために私のログインページを強制しています、と私はログイン後に戻ってHTTPに切り替える必要があり、他の言葉では:。Spring Security 3.1.0 - HTTPSからHTTPに切り替えることはできません

  • ログインページ:HTTPSのみ
  • 他のページ:HTTPのみ

私はいくつかの方法を試みましたが、上記のように動作させることはできません。 私はSpring Security FAQを読んでいて、私が望むことをするのは自然な方法ではないが、そうするように求められているので、私は自分で見つけることができない回避策が必要です。

私はSpring Security 3.1.0を使用しています。 私のWebコンテナはTomcat 6.0.33です。

これは私の春のセキュリティの設定です:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns:sec="http://www.springframework.org/schema/security" 
    xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

    <sec:http auto-config="true" use-expressions="true"> 

     <sec:intercept-url pattern="/log*.htm" access="anonymous" 
      requires-channel="https" /> 
     <sec:intercept-url pattern="/admin/**" access="hasRole('admin')" 
      requires-channel="http" /> 
     <sec:intercept-url pattern="/**" 
      requires-channel="http" access="hasRole('authenticated')" /> 

     <sec:form-login login-page="/login.htm" 
      default-target-url="/index.htm" authentication-failure-url="/login.htm?error=true" 
      always-use-default-target="true" /> 
     <sec:logout logout-url="/logout.htm" delete-cookies="JSESSIONID" invalidate-session="true" /> 
     <sec:anonymous/> 
     <sec:remember-me use-secure-cookie="true" /> 
    </sec:http> 

    <sec:authentication-manager> 
     <sec:authentication-provider> 
      <sec:user-service> 
       <sec:user name="johnny" password="johnny" authorities="authenticated, admin" /> 
       <sec:user name="charlie" password="charlie" 
        authorities="authenticated" /> 
      </sec:user-service> 
     </sec:authentication-provider> 
    </sec:authentication-manager> 

</beans> 

任意の助けが理解されるであろう。 ありがとうございます!

+3

これはあまりお勧めできません。それは、セッションクッキーが盗まれるのを許します、Firesheep-スタイル。 –

+0

はい、それは正しい解決策ではないことは分かっていますが、構築しているアプリケーションの種類とサーバーの負荷によっても異なると思います。残念ながら、この場合、私はいくつかの(暗い)理由のためにそうするよう求められました。とにかくあなたのコメントをありがとう、私たちはこのアプローチが非常に脆弱であることを忘れるべきではありません。 – nomusicnolife

答えて

0

あなたの設定ファイルでは、あなたのクッキー

を変更するフィルター・チェーンにフィルタを追加することにより、フィルタを作成し、次のようにフィルター・チェーンにそれを追加することも可能である:

<bean name="httpsCookieFilter" class="bla.bla.bla.HttpsCookieFilter"/> 

<security:http auto-config="false" entry-point-ref="authenticationEntryPoint"> 
... 
    <security:custom-filter position="FIRST" ref="httpsCookieFilter" /> 
... 
</security:http> 

そして、あなたのフィルターコードは次のようになります

import java.io.IOException; 

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.Cookie; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 

/** 
* Sessions created under HTTPS, for which the session cookie is marked as “secure”, cannot subsequently be used under 
* HTTP. The browser will not send the cookie back to the server and any session state will be lost (including the 
* security context information) 
* 
* Tomcat tracks user sessions with the help of the JSESSIONID cookie. If you enter into HTTPS with Tomcat, the cookie 
* will come back with the secure property being set to true. Subsequently when the redirection to http occurs, the 
* browser will not transmit the JSESSIONID cookie and you'll get a new session. 
* 
* This filter overrides the default Tomcat JSESSIONID behaviour 
*/ 
public class HttpsCookieFilter implements Filter { 

    @Override 
    public void init(FilterConfig arg0) throws ServletException { 
    } 

    @Override 
    public void destroy() { 
    } 

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
      throws IOException, ServletException { 

     final HttpServletRequest httpRequest = (HttpServletRequest) request; 
     final HttpServletResponse httpResponse = (HttpServletResponse) response; 
     final HttpSession session = httpRequest.getSession(false); 

     if (session != null) { 
      final Cookie sessionCookie = new Cookie("JSESSIONID", session.getId()); 
      sessionCookie.setMaxAge(-1); 
      sessionCookie.setSecure(false); 
      sessionCookie.setPath(httpRequest.getContextPath()); 
      httpResponse.addCookie(sessionCookie); 
     } 

     chain.doFilter(request, response); 
    } 

} 
+0

ありがとうございました。とにかく、何らかの理由でこのフィルターのアプローチが私のためには機能しませんでした。その間、別の答えで説明する別の方法を見つけました。私は両方のアプローチが同じ問題を抱えているかもしれない開発者によって試されるべきだと思う。 – nomusicnolife

1

この問題の回避策は、Spring Securityのデフォルトのセッション固定保護を無効にすることです。最初に説明したXML設定に「セッション管理」要素を追加する必要がありました。

<sec:http auto-config="true"> 

    <!-- ... --> 

    <sec:session-management session-fixation-protection="none"/> 

    <!-- ... --> 
</sec:http> 

これに加えて、「アプリケーションURL」として提供する必要があるURLは、ログインURLではなくホームページのURLです。 NOT http://myapp/login.htm BUT http://myapp/index.htm。そうすることで、ユーザーがログインしている、またはRemember-me Cookieを持っている場合、ブラウザは問題なく入り、HTTPプロトコルを使用し続けることができます。そうでない場合、ユーザーはHTTPSを使用してログインページにリダイレクトされ、ログインに成功すると、ブラウザはHTTPに正しく切り替わります。これを考慮に入れてください。なぜなら、ログインURLを直接書き込む(またはクリックする)と、HTTPSは常に維持されるからです。

関連する問題