JAX-RS 2.0 と JPA を使用して Javae EE アプリを作成しました。現在のユーザー (ログイン中) をアプリケーション ユーザー データベースからのエンティティとして提供するために、(修飾子を使用して) User エンティティの特別なプロバイダーを作成しました。現在のユーザーを取得するには、使用します
@Context
private SecurityContext secContext;
問題は、これが null であることです。セキュリティは適切に設定されています (Wildfly 8.2) - アプリは認証 (基本) を要求しますが、SecurityContext
null です。コードは次のとおりです。
@RequestScoped
public class CurrentUserProducer implements Serializable {
/**
* Default
*/
private static final long serialVersionUID = 1L;
@Context
private SecurityContext secContext;
/**
* Tries to find logged in user in user db (by name) and returns it. If not
* found a new user with role {@link UserRole#USER} is created.
*
* @return found user a new user with role user
*/
@Produces
@CurrentUser
public User getCurrentUser() {
if (secContext == null) {
throw new IllegalStateException("Can't inject security context - security context is null.");
//... code to retrieve or create new user
return user;
}
}
ご覧のとおり、null をチェックsecContext
すると、注入するリソースに到達しようとするとすぐに例外が表示されます@CurrentUser
。
では、これを修正する方法は?なぜSecurityContext
ヌルです。