18

Spring 2.5.6 と Spring Security 2.0.4 を使用する Web アプリケーションがあります。Web サービスに対してユーザーを認証するログイン ページを実装しました。認証は、次のようにカスタム認証マネージャーを定義することによって行われます。


<beans:bean id="customizedFormLoginFilter"
    class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter">
    <custom-filter position="AUTHENTICATION_PROCESSING_FILTER" />
    <beans:property name="defaultTargetUrl" value="/index.do" />
    <beans:property name="authenticationFailureUrl" value="/login.do?error=true" />
    <beans:property name="authenticationManager" ref="customAuthenticationManager" />
    <beans:property name="allowSessionCreation" value="true" />
</beans:bean>

<beans:bean id="customAuthenticationManager"
    class="com.sevenp.mobile.samplemgmt.web.security.CustomAuthenticationManager">
    <beans:property name="authenticateUrlWs" value="${WS_ENDPOINT_ADDRESS}" />
</beans:bean>

認証マネージャ クラス:


public class CustomAuthenticationManager implements AuthenticationManager, ApplicationContextAware {

    @Transactional
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

                //authentication logic

        return new UsernamePasswordAuthenticationToken(principal, authentication.getCredentials(),
                grantedAuthorityArray);
    }

ログイン JSP の重要な部分は次のようになります。


<c:url value="/j_spring_security_check" var="formUrlSecurityCheck"/>
<form method="post" action="${formUrlSecurityCheck}">
    <div id="errorArea" class="errorBox"> 
       <c:if test="${not empty param.error}">
          ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
      </c:if>
  </div>
    <label for="loginName">
        Username:           
        <input style="width:125px;" tabindex="1" id="login" name="j_username" />
    </label>

    <label for="password">
        Password:           
        <input style="width:125px;" tabindex="2" id="password" name="j_password" type="password" />
    </label>
    <input type="submit" tabindex="3" name="login" class="formButton" value="Login" />
</form>

問題は、アプリケーションがSpring Web Flowを使用する必要があることです。アプリケーションが Spring Web Flow を使用するように構成された後、ログインが機能しなくなります。「/j_spring_security_check」へのフォーム アクションにより、エラー メッセージのない空白のページが表示されます。Spring Web Flow で動作するように既存のログイン プロセスを適応させる最良の方法は何ですか?

編集:エラー メッセージは表示されず、空白のページが表示されました。現在は動作しています - 問題は、web.xml にエントリがないことでした


<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>

問題は解決しましたが、報奨金をキャンセルすることはできないため、Spring Security と Web Flow を連携するように他の人が構成するのに役立つ、最も洞察に満ちた回答またはリンクに報奨金が授与されます。

4

4 に答える 4

2

「springSecurityFilterChain」が必要な理由を説明するドキュメントを次に示します。

springSecurityFilterChain フィルターは明示的に作成されませんが<http>、アプリケーション コンテキストのスプリング セキュリティ スキーマ要素によって暗黙的に作成されます。

<http auto-config="false" session-fixation-protection="none">
    <form-login login-page="/login.jsp" default-target-url="/home.htm" />
</http>

作成された Bean には認証プロバイダーが必要なので、1 つ提供します。

<authentication-provider>
   <user-service id="userDetailsService">
      <user password="password" name="username" authorities="ROLE_USER" />
   </user-service>
</authentication-provider

編集された質問で説明されているように、セキュリティ フィルターの構成を含むこれらの変更を行うと、ページが正しくレンダリングされ、構成されたセキュリティ スタックが提供される各ページに配置されます。

これらの詳細と単純なスプリング セキュリティ アプリケーションのウォークスルーは、Spring セキュリティを使用した目的の Web アプリケーション - パート 13 で説明されています。

リファレンス ドキュメントではspringSecurityFilterChain、web.xml の設定の最初の部分として を設定する方法について説明しています: Security Namespace Configuration - Getting Started。(OP で使用される 2.0.x バージョンにリンクされています。最新バージョン 3.1 はこちらです。)

于 2010-05-30T10:52:08.263 に答える
1

コードから、SpringSecurity構成があるかどうかは明確ではありません。私はあなたがそうしないと思います。

  • 構成にセキュリティ名前空間を追加する必要があります。このようなもの

    
    <beans xmlns="http://www.springframework.org/schema/beans" ... skipped ...
        xmlns:security="http://www.springframework.org/schema/security"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        ... skipped ...
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
    

  • SpringSecurityを構成する必要があります。例えば:

    
    <security:http once-per-request="false" auto-config="false">
        <security:form-login login-page="path/to/your/login.jsp"/>
    </security:http>
    

  • これで「正常に機能」するのに十分かもしれませんが、 AuthenticationManagerではなくAuthenticationProviderの実装を検討する必要があると思います。この場合、次のような構成が必要になります。

    
    <bean class="my.company.project.MyAuthProvider"
          autowire="byType">
        <security:custom-authentication-provider/>
    </bean>
    

于 2010-05-31T11:37:40.177 に答える
0

Spring Web Flow リファレンス ガイドのフローの保護の章に従って、Spring Web Flow を使用するようにアプリを構成しましたか?

于 2010-05-26T12:04:50.063 に答える
0

そのURL自体を解釈するように見えるため、フォーム名をj_security_checkからglassfishの別のものに変更する必要がありました。

于 2010-05-27T03:42:55.487 に答える