Spring Security の servletApi() サポートは素晴らしいです。
カスタムプリンシパルを次のように挿入したい:
public interface UserPrincipal extends Principal {
public Integer getId();
}
@RequestMapping(value = "/")
public ResponseEntity<List<Conversation>> listAfter(UserPrincipal user){
// implementation
}
or
@RequestMapping(value = "/")
public ResponseEntity<List<Conversation>> listAfter(UserPrincipalImpl user){
// implementation
}
Principal
Spring は、 の助けを借りてインスタンスの注入をサポートしていますServletRequestMethodArgumentResolver
。
次のようにプリンシパルを注入しています。
else if (Principal.class.isAssignableFrom(paramType)) {
return request.getUserPrincipal();
}
ここから問題が始まります。request
のインスタンスですSecurityContextHolderAwareRequestWrapper
。次の実装があります。
@Override
public Principal getUserPrincipal() {
Authentication auth = getAuthentication();
if ((auth == null) || (auth.getPrincipal() == null)) {
return null;
}
return auth;
}
Authentication
も であるからPrincipal
です。(Spring Security で唯一気に入らなかった部分。これも別の質問にします。)
これが問題を引き起こしています。Authentication
は ではPrincipal
ないからですUserPrincipal
。
この問題を解決するにはどうすればよいですか? UserPrincipal である認証も実装する必要がありますか? または、HandlerMethodArgumentResolver オーダーを変更して、カスタム リゾルバーを作成する必要がありますか? (Spring MVC では、内部ハンドラーの優先度が高いため、これは簡単ではありません。)
追加情報として:
私はSpring Security M2を使用しており、私の構成AuthenticationManagerBuilder
は単純です:
@Override
protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(detailsService);
}
何か助けはありますか?