2013-06-10 7 views
6

ログイン成功後、「index.php」にリダイレクトされません。それは "login.php"と同じページをリダイレクトします。私のspring-security.xmlページに何か問題がありますか?ログイン後の春のセキュリティリダイレクト

ところで、私がアプリケーションを実行すると、それは良い "login.php"にリダイレクトされます。しかし、primefacesコンポーネントではなく、htmlコンポーネントを表示します。私はうまくログインした後、同じページをリダイレクトしますが、今回はhtmlコンポーネントの代わりにPrimefacesコンポーネントを表示します。

<beans:beans xmlns="http://www.springframework.org/schema/security" 
     xmlns:beans="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.2.xsd 
http://www.springframework.org/schema/security 
http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

<http auto-config="true" use-expressions="true"> 
    <intercept-url pattern="/pages/login.xhtml*" access="permitAll"/> 
    <intercept-url pattern="/**" access="hasRole('admin')" /> 
    <form-login login-page='/pages/login.xhtml' default-target-url="/pages/index.xhtml"      
       authentication-failure-url="/pages/login.xhtml"/> 
    <logout logout-success-url="/pages/logout.xhtml" /> 

</http> 
<!--Authentication Manager Details -->  
<authentication-manager alias="authenticationManager"> 
    <authentication-provider user-service-ref="customUserDetailsService"> 
<!--   <password-encoder hash="md5"/>--> 
    </authentication-provider> 
</authentication-manager> 

私のweb.xml

<welcome-file-list> 
    <welcome-file>pages/index.xhtml</welcome-file> 
</welcome-file-list> 

私のログインページ

<p:outputPanel id="loginOutputPanelId" style="border: navy"> 
         <p:panelGrid id="loginInformationPanel" columns="2"> 
          <h:outputText value="Username: "/> 
          <p:inputText value="#{loginController.userName}"/> 
          <h:outputText value="Password: "/> 
          <p:inputText value="#{loginController.password}"/> 
         </p:panelGrid> 
         <p:commandButton value="Login" actionListener="#{loginController.login()}"/> 
        </p:outputPanel> 

私loginController.login()メソッドが返す "インデックス" の文字列と私のfaces.config。

<navigation-rule> 
     <from-view-id>/pages/login.xhtml</from-view-id> 
     <navigation-case> 
      <from-outcome>index</from-outcome> 
      <to-view-id>/pages/index.xhtml</to-view-id> 
      <redirect /> 
     </navigation-case> 
    </navigation-rule> 

EDIT: 成分を含まない、それは問題なく動作します。私がフォームログインを追加すると、「http://localhost:8080/myApplication/pages/login.xhtmlのWebページではリダイレクトが多すぎます」と表示されます。

<http auto-config='true' use-expressions="true"> 
<intercept-url pattern="/**" access="hasRole('admin')" /> 
<logout logout-success-url="/pages/logout.xhtml" /> 
<form-login login-page="/pages/login.xhtml" 
       login-processing-url="/j_spring_security_check"              
       default-target-url="/pages/index.xhtml"               
       authentication-failure-url="/pages/login.xhtml"/> 
</http> 

マイログインページ

<p:outputPanel id="loginOutputPanelId" style="border: navy"> 
         <p:panelGrid id="loginInformationPanel" columns="2"> 
          <h:outputText value="Kullanıcı Adı: "/> 
          <p:inputText id="j_username" required="true" value="#{loginController.userName}"/> 
          <h:outputText value="Şifre: "/> 
          <p:inputText id="j_password" required="true" value="#{loginController.password}"/> 
         </p:panelGrid> 
         <p:commandButton id="login" type="submit" ajax="false" value="Login" actionListener="#{loginController.login()}"/> 
        </p:outputPanel> 

私の新しいloginController.login()メソッド。

ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); 

     RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()) 
     .getRequestDispatcher("/j_spring_security_check"); 

     dispatcher.forward((ServletRequest) context.getRequest(), 
     (ServletResponse) context.getResponse()); 

     FacesContext.getCurrentInstance().responseComplete(); 

答えて

16

あなたはこのようプロパティalways-use-default-targetを使用することができ、/pages/index.xhtmlに行くために、バネのセキュリティを強制する:

<form-login login-page='/pages/login.xhtml' 
      default-target-url="/pages/index.xhtml" 
      always-use-default-target="true"      
      authentication-failure-url="/pages/login.xhtml"/> 

ユーザーがセキュリティで保護されたを呼び出したときにそうでない場合は、ログインページにはスプリングセキュリティによって自動的に表示されなければなりません一度ログインすると、最初に要求された保護されたリソースに進みます。

あなたの場合、春のセキュリティでログインを処理し、jsfのactionListenerとナビゲーションルールを使って自分で処理しようとしているという混乱があるようです。

"<form-login [...]"を設定すると、基本的にspringが/j_spring_security_checkのリクエストを聞くフィルタ(UsernamePasswordAuthenticationFilter)をアクティブにするように指示します。 springがログインを処理するようにするには、デフォルトでフォームログインが2つのパラメータ:j_usernamej_passwordを渡してこのURLを要求しなければなりません。

このように、春のUsernamePasswordAuthenticationFilterは、AuthenticationProviderで設定したUserDetailsS​​erviceを使用して、提供された資格情報を取得して認証しようとします。

ログイン用にjsfコントローラを削除し、認証を処理するためにスプリングセキュリティを使用する必要があると思います。

これが役に立ちます。

PS:ウェブを確認してください。XMLは、他のすべてのサーブレット・フィルターの前にDelegatingFilterProxyを定義しています

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
+0

<servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> 

を、あなたは次のように春-のsecurity.xmlでコードを更新する必要があります。それは何も変わらない。 –

+0

あなたが最初に呼ぶURLは何ですか?あなたは直接ログインページに電話をするか、別の保護されたURLを呼び出しますか? – baraber

+0

index.xhtmlは "web.xml"で定義された私の歓迎のファイルです。私はあなたが意味することを理解していません。 spring-security.xmlにはlogin-page = '/ pages/login.xhtml'、login.xhtmlには春セキュリティが最初に呼び出されます。 always-use-default-target = "true"行を追加した後は、どのページもリダイレクトしません。それはlogin.xhtmlページにとどまります。次に、ブラウザのアドレスバーにインデックスアドレスを手動で書きます。 –

0

あなたはweb.xmlのURLパターン「サーブレット顔」をご確認ください。それは.jsfのが好き含まれている場合:

同じそれでも
<form-login login-page="/pages/login.jsf" 
       login-processing-url="/j_spring_security_check"              
       default-target-url="/pages/index.jsf"               
       authentication-failure-url="/pages/login.jsf"/> 
関連する問題