【发布时间】:2015-08-16 10:43:52
【问题描述】:
我使用 JAX-RS 2.0 和 JPA 创建了一个 Javae EE 应用程序。我为我的用户实体(使用限定符)创建了一个特殊的提供者,以将当前用户(登录)作为应用程序用户数据库中的实体提供。 要获取当前用户,我使用
@Context
private SecurityContext secContext;
问题是这是空的。安全设置很好(Wildfly 8.2) - 应用程序要求身份验证(基本)但SecurityContext 为空。代码如下:
@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;
}
}
如您所见,我检查 secContext 是否为空,一旦我尝试访问注入 @CurrentUser 的资源,我就会看到我的异常。
那么如何解决这个问题?为什么SecurityContext 为空。
【问题讨论】:
标签: java dependency-injection jax-rs resteasy security-context