当有人登录时,我想将 activeUser 对象存储在会话范围中,我将其转发用于用户特定的操作,但是当管理员尝试登录时出现问题,因为管理员不是默认用户,所以 @out 注释会导致错误错误代码熟悉 = @Out 属性需要非空值:authenticator.activeUser 我的逻辑代码如下;(注意:管理员和标准用户使用相同的登录面板)
@Out(scope = ScopeType.SESSION, required = true)
private User activeUser;
public boolean authenticate() throws Exception {
// SELECT OBJECT(user) FROM User user WHERE user.name = :name and
// user.password = :pass
try {
// create a query for getting a user with specified parameters if no
// user exist throw an exception
setActiveUser((User) entityManager
.createQuery(
"select u from User u where u.userName= :uname and u.password = :pass")
.setParameter("uname", credentials.getUsername())
.setParameter("pass", credentials.getPassword())
.getSingleResult());
identity.addRole("user");
return true;
} catch (Exception e) {
// so there is no any normal user now looking for any admin entry
System.out.println("there is no any member looking for admin");
try {
Admin admin = (Admin) entityManager
.createQuery(
"select a from Admin a where a.username = :uname and a.pass = :p")
.setParameter("uname", credentials.getUsername())
.setParameter("p", credentials.getPassword())
.getSingleResult();
identity.addRole("admin");
System.err.println("found admin");
return true;
} catch (Exception e2) {
System.err.println("admin bulunamadı");
throw e2;
}
}
}
in @out required 必须为真,因为在用户特定的 crud 操作中,我使用用户 ID 记录它们。我怎么解决这个问题。设置 required=false 就足够了吗?
另一方面,如果没有用户发现该entitymanager.resultlist()
方法将引发异常。所以我知道@out 注释可以工作,除非方法抛出任何异常?