3

リクエストからの情報を必要とする Spring Bean がありますが、コントローラーから直接呼び出されません (可能性はありますが、それなしでこれを試してみたいと思います)。

基本的に、私の API は倹約を介して他のサービスにリクエストを行います。リクエストを行うと、次のようなサービス コールがあります。

authenticationService.authenticate(null, "username", "password");

最初のパラメータ (the null) は通常、リクエスト コンテキストの「プレースホルダ」インスタンスです。リクエスト コンテキストには、リクエストを行ったユーザー、元の IP などに関する情報が含まれています。このようにして、API インフラストラクチャがバックエンドに漏れることなく、元の呼び出し元に関するすべての詳細を取得できます。

ただし、これを行うにInvocationHandlerは、サービス インターフェイスのプロキシに対して行われたメソッド呼び出しをインターセプトする があります。そのプロキシ ハンドラーの内部にRequestContextFactoryは、RequestContext. このファクトリー内で、リクエストから情報を取得する必要があります。特に、SecurityContextですので、電話をかけているユーザーを特定できます。

今、私は持っています:

@Provider
@Component
public class WebRequestContextFactory implements RequestContextFactory {
    @Context private ContainerRequest containerRequest;

    public RequestContext createRequestContext() {

    }
}

残念ながら、containerRequest常にnullです。

4

4 に答える 4

2

を使用ServletRequestAttributesして から情報を取得requestできます。 は から取得ServletRequestAttributesできますRequestContextHolder

ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes();

リクエストが Spring によって処理される場合、DispatcherServlet特別な設定は必要ありません。DispatcherServlet関連するすべての状態をすでに公開しています。ただし、リクエストが Spring の外部で処理される場合は、アプリケーションの web.xml ファイルDispatcherServletに追加する必要があります。javax.servlet.ServletRequestListener

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

これにより、リクエストが現在のスレッドに関連付けられ、関連するリクエスト属性が 経由で取得できるようになりますRequestContextHolder

于 2013-09-24T08:25:18.550 に答える
0

IP アドレス、ユーザー名などの必要な情報を含むアクセス トークンを作成できます。認証段階で、カスタム トークンを作成し、このトークンをスプリング セキュリティ コンテキストに配置します。後で、プロキシ クラスなどの他の場所からこのトークンを抽出できます。トークンを抽出した後、検証するか、必要なものを何でも。

カスタム オブジェクトとトークンの作成:

public class CustomAuthentication {

  private String userId;

  private String password;

  private String ipAddress;
}


public class CustomAuthenticationToken extends AbstractAuthenticationToken {

    private CustomAuthentication customAuthentication;

    public CustomAuthenticationToken(MobiLabAuthentication authentication,
      Collection<? extends GrantedAuthority> authorities) {
      super(authorities);
      this.customAuthentication = authentication;
      setAuthenticated(true);
    }

    public CustomAuthenticationToken() {
      super(null);
      setAuthenticated(false);
    }

    @Override
    public Object getCredentials() {
     return customAuthentication.getPassword();
    }

    @Override
    public Object getPrincipal() {
      return customAuthentication.getUserId();
    }

}

トークンを Spring セキュリティ コンテキストに格納する

List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new RestUserAuthrity("YOUR_APP_ROLE"));

//Extract IP , user and pass etc and construct CustomAuthentication instance
CustomAuthentication authentication = new CustomAuthentication(.....)

CustomAuthenticationToken authenticationToken = new CustomAuthenticationToken(
     authentication, authorities);

SecurityContextHolder.getContext().setAuthentication(authenticationToken);

Proxy Bean からのセキュリティー情報の検証

SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();

if (authentication instanceof CustomAuthenticationToken) {
  CustomAuthenticationToken token = (CustomAuthenticationToken) authentication;
 //now you can get your ip address from token
}
于 2013-09-24T19:25:50.807 に答える