6

私は春と春のセキュリティに慣れていませんが、

Bean が xml ファイルでどのように作成および参照されるかを理解しました。Spring を使用してアプリケーションにセキュリティを提供する必要があります。

web.xml にカスタム applicationContext-security.xml ファイルを含めました: contextConfigLocation

このファイルでは、次を使用して URL パターンをインターセプトしました。

<intercept-url pattern='/**.something' access="IS_AUTHENTICATED_FULLY"/>

内部要素。

ログイン用のフォームを今のように設定しました。ページが承認されていない場合、カスタム Login.html ページが表示されます。

今私が直面している問題について:

  1. ログインフォームを指定してその値を spring に渡すにはどうすればよいですか?
  2. 独自の authentication-provider を使用するにはどうすればよいですか?

私はこれを試しました:

<authentication-provider user-service-ref="userDetailsService"/>
<beans:bean id = "userDetailsService" class ="com.somepath.CustomAuthenticationProvider">
        <custom-authentication-provider/>
    </beans:bean>

ここで CustomAuthenticationProvider は AuthenticationProvider を実装します

しかし、コードはエラーをスローします: '_filterChainProxy' という名前の Bean を作成中にエラーが発生しました .... UserDetailsS​​ervice が登録されていません

助けてください

4

2 に答える 2

7

1: ログインフォームを指定してその値を spring に渡すにはどうすればよいですか?

<http>タグによって構成されたデフォルト設定の一部を使用して、Spring Security の web.xml で標準の Spring フィルターをセットアップした後。のインスタンスがAuthenticationProcessingFilter フィルターのチェーンの一部として作成されます。

私のデフォルトはAuthenticationProcessingFilter、ユーザー名/パスワードトークンとして j_username と j_password を読み取るように設定されています。

これをオーバーライドするには、次のようにして、カスタマイズした AuthenticationProcessingFilter をデフォルトのものに置き換えます。

<bean id=“myAuthFilter” class=“org.springframework.security.ui.webapp.AuthenticationProcessingFilter” >
<security:custom-filter position=“AUTHENTICATION_PROCESSING_FILTER”/><!–-replace the default one-–&gt;
  <property name=“usernameParameter” value=“myUsername”/><!-- myUsername is the name of the input tag where user enter their username on the HTML page -->
  <property name=“passwordParameter” value=“myPassword” /><!–- myPassword is the name of the input tag where user enter their password on the HTML page -–&gt;
</bean>

詳細については、AuthenticationProcessingFilter の JavaDoc も参照してください: http://static.springsource.org/spring-security/site/apidocs/org/springframework/security/ui/webapp/AuthenticationProcessingFilter.html

2: 独自の認証プロバイダーを使用するにはどうすればよいですか?

次のコードを使用します。

<bean id="myAuthenticationProvider" class="com.something.MyAuthenticationProvider">
    <security:custom-authentication-provider />
</bean>

<security:custom-authentication-provider />は、これがカスタム プロバイダーであり、Authentication Manager がそのプロバイダー チェーンで使用する必要があることをスプリングに知らせるタグです。

ソース: http://static.springsource.org/spring-security/site/docs/2.0.x/reference/appendix-namespace.html#d4e3379

3: 「_filterChainProxy」をスローするコードの問題について.... UserDetailsS​​erviceが登録されていません...」

UserDetailServicecom.somepath.CustomAuthenticationProviderインターフェイスを実装していますか?

于 2009-10-21T13:42:30.757 に答える
1

私は春に少し慣れていませんが、あなたを助けようとします。インターセプト URL は正常に見えます。

認証プロバイダーが正しいとは思いません。私のコードを見てください:

  <beans:bean id="MyUserDetailsService" class="path.to.MyAuthenticationService"/>

<beans:bean id="userDetailsService" class="org.springframework.security.userdetails.hierarchicalroles.UserDetailsServiceWrapper" >
    <beans:property name="roleHierarchy" ref="roleHierarchy" />
    <beans:property name="userDetailsService">
      <beans:ref bean="MyUserDetailsService"/>
    </beans:property>
  </beans:bean>
 <authentication-provider user-service-ref="userDetailsService">
   <password-encoder hash="md5"/>
 </authentication-provider>

ロール階層は必要ないかもしれません。

jsp ページにログイン フォームがあります。フォームは次のように開始する必要があります。

<form:form modelAttribute="login">

また、適切なフィールドをマップする必要があります。

<form:input path="login">
<form:password path="password">

applicationContext-security.xml で、ログイン ページを設定します。

<form-login login-page="/login.jsp" default-target-url="/login.html" always-use-default-target="true" authentication-failure-url="/login.jsp?login_error=1"/>

login.html は、BaseController を拡張し、少なくとも HttpServletRequest と Model をパラメーターとして受け取る login メソッドを実装する LoginController.java にマップする必要があります。次に、次のSpringクラス/メソッドを呼び出すことで機能します。

String userlogin = SecurityContextHolder.getContext().getAuthentication().getName();

CustomAuthenticationProvider が正しく実装されている場合は、(うまくいけば) モデルからユーザーの詳細を取得でき、最後に:

return "redirect:homepage.html";

まだ問題がある場合は、コメントでお知らせください。

于 2009-10-21T13:26:30.320 に答える