7

私はこのコードを持っています

UserDetails userDetails = userDetailsServiceImpl.loadUserByUsername(email);
Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
SecurityContext securityContext = SecurityContextHolder.getContext();
securityContext.setAuthentication(authentication);
HttpSession session = request.getSession(true);
session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);

これは、春のセキュリティでユーザーを手動で認証することです。私の質問は、このコードをどこに配置すればよいですか? これをサービスレイヤーに配置すると、HttpSessionオブジェクトをサービスレイヤーに持ち込む必要がありますが、これはAFAIKが悪いです。認証ロジックをプレゼンテーション層に配置することがどれほど良いかについてもわかりません。洞察力のある人はいますか??

前もって感謝します。

4

1 に答える 1

14

アクティブユーザーのUserDetailsを取得するためのベストプラクティスの質問に対するLukeTaylorの回答を参照してください。コードをSpringSecurityから切り離したまま、このタイプのことを行うためのカスタムインターフェイスを作成するための設計根拠。たとえば、と呼ばれるインターフェイスMyAuthenticatorを作成し、実装を作成してアプリケーションに挿入できます。

また、Springセキュリティフィルターが標準の場合は、HttpSessionオブジェクトにアクセスする必要はありません。フレームワークフィルターがそれを処理します。実装には次のように書く必要があります。

UserDetails userDetails = userDetailsServiceImpl.loadUserByUsername(email);

Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());

SecurityContextHolder.getContext().setAuthentication(authentication);

HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEYフレームワークの将来のバージョンで変更される可能性があるため、「SPRING_SECURITY_CONTEXT」()の使用はお勧めしません。

于 2013-03-18T22:43:12.340 に答える