0

シンプルな spring mvc / spring security webapp をセットアップしようとしていますが、これを達成する方法が見つからないようです:

  1. 通常の @Secured アノテーションを使用したいのですが、ユーザーがログインしていない場合は、ログイン ページにリダイレクトして元の場所に戻してほしいです (これは私が管理した通常の動作です)。達成するために)
  2. ログインフォームを自分のコントローラー/テンプレートのペアにしたいです(これも一般的で完成しています)。
  3. 上記のログイン フォームを自分のコントローラーに送信して、バックエンドの RESTful サービスに対してユーザーの資格情報を認証したいと思います。次に、サービスから返されたセキュリティ トークンを受け取ります。この時点で、セッションに認証済みのフラグを手動で付けて、トークンをアタッチしたいと思います。

最後のステージを実装するにはどうすればよいですか?

4

2 に答える 2

1

あなたの質問を完全に理解しているかどうかはわかりませんが、正しく理解していれば、AbstractPreAuthenticatedProcessingFilterを拡張し、getPreAuthenticatedPrincipalとgetPreAuthenticatedCredentialsをRESTfulサービス/コントローラーなどへの呼び出しでオーバーライドできます。AuthenticationUserDetailsS​​erviceをオーバーライドして単純なサービスを提供し、それを追加します次のようなセキュリティコンテキスト:

<beans:bean id="preauthAuthProvider"
            class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
    <beans:property name="preAuthenticatedUserDetailsService">
        <beans:bean class="com.YourCompany.YourPreAuthenticatedGrantedAuthoritiesUserDetailsService"></beans:bean>
    </beans:property>
    <beans:property name="order" value="1"/>
</beans:bean>


<authentication-manager alias="authenticationManager" >
    <authentication-provider ref="preauthAuthProvider" ></authentication-provider>
</authentication-manager>
于 2012-04-23T09:13:13.873 に答える
0

答えは基本的に次のとおりです。

SecurityContextHolder.getContext().setAuthentication(...)

ただし、Spring MVC コントローラーが認証プロセスを制御する上記のシナリオで使用できるようにするには、他にいくつかのことを行う必要があります。

  1. 使用可能な認証実装のいずれかを使用するか、作成する必要があります。AbstractAuthenticationToken をサブクラス化するのが最善であることがわかりました。
  2. Spring セキュリティは、このシナリオで使用されていない認証マネージャーがないと起動しないため、null 認証マネージャーを作成しました。

    @Service("nullAuthenticationProvider")
    public class NullAuthenticationProvider implements AuthenticationProvider
    {
        @Override
        public Authentication authenticate(Authentication authentication) throws AuthenticationException
        {
            return authentication;
        }
    
        @Override
        public boolean supports(Class<?> authentication)
        {
            return true;
        }
    }
    
  3. そして最後に、春の context.xml:

    <security:global-method-security secured-annotations="enabled" />
    
    <security:http disable-url-rewriting="true">
        <security:access-denied-handler error-page="/login" />
        <security:form-login login-page="/login" />
    </security:http>
    
    <security:authentication-manager>
        <security:authentication-provider ref='nullAuthenticationProvider'/>
    </security:authentication-manager>
    
于 2012-04-23T23:40:47.160 に答える