我正在使用Spring安全性进行登录。我有User.java
,其中包含用户详细信息。
@Entity(name = "user_table")
//@Table(name = "user_table")
public class User {
@Id
@Column(name = "id")
private String userId;
@Column(name = "email" ,unique = true)
private String userEmail;
@Column(name = "password")
private String userPassword;
//getter and setters
}
我使用spring security从表中获取当前用户的全部数据。这是代码:
public User findUserByEmail(String email) {
List<User> users = new ArrayList<User>();
try{
users = sessionFactory.getCurrentSession().createQuery("from user_table where email= ?").setParameter(0, email).list();
System.out.println("user is " +users);
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
if (users.size() > 0) {
return users.get(0);
} else {
return null;
}
}
@Override
public User getCurrentUser() {
Authentication auth = SecurityContextHolder.getContext()
.getAuthentication();
User currentUser = new User();
if (!(auth instanceof AnonymousAuthenticationToken)) {
UserDetails userDetails = (UserDetails) auth.getPrincipal();
System.out.println("User has authorities: "
+ userDetails.getAuthorities());
System.out.println("USERNAME:: "+userDetails.getUsername());
currentUser = findUserByEmail(userDetails
.getUsername());
System.out.println("currentUser "+currentUser);
System.out.println("currentUser "+currentUser.getUserId());
return currentUser;
}
return null;
}
我想要的是将我从currentUser.getUserId()
获得的用户ID发送到其他方法。在那个方法中,我正在映射到其他表,如user_detail
表,其中id是主键。通过发送id,我将获得user_table中不存在的其他user_details。
这是我的UserDetail
:
@Entity(name = "user_detail")
@Table(name = "user_detail")
public class UserDetail {
@Id
@GeneratedValue
@Column(name = "id")
private String userId;
//some other details like Address .
//getter and setter.
}
从控制器我正在调用上面的方法:
UserService userService = new UserService();
User user=userDao.getCurrentUser();
String userId = user.getUserId();
System.out.println(userId);
UserDetail u=userDao.findUserById(userId);
这是我传递当前用户ID的方法:
public UserDetail findUserById(String id) {
// TODO Auto-generated method stub
List<String> users = new ArrayList<String>();
try{
users = sessionFactory.getCurrentSession().createQuery("from user_detail where id= ?").setParameter(0, id).list();
System.out.println("user is " +users);
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
if (users.size() > 0) {
return null;
} else {
return null;
}
}
现在我得到的结果是null。就像用户是空的。我在这里做错了什么?
答案 0 :(得分:0)
您的代码中存在几个问题。只是指出其中一些:
UserService userService = new UserService();
- 您手动创建服务对象,不让Spring-MVC将其注入您的控制器,即:
@Autowired
private UserService userService ;
UserDAO
应该在您的服务中注入,而不是从您的控制器调用:
class UserServiceImpl implements UserService{
@Autowired
private UserDAO userDAO;
}
控制器的所有操作都应该调用服务方法而不是DAO的方法。该服务应使用DAO进行数据库访问。即。
UserDetail u=userDao.findUserById(userId);
应该成为
UserDetail u = userService.findUserById(userId);
并在您的服务中:
class UserServiceImpl implements UserService{
@Autowire
private UserDAO userDAO;
@Override
public UserDetail findUserById(Long userId){
return userDAO.findUserById(userId);
}
}
if (users.size() > 0) {
return null;
} else {
return null;
}
总是返回null
。应该是:
if (`users.isEmpty()){
return users.get(0);
}else { return null;}
users = sessionFactory.getCurrentSession().createQuery("from user_detail where id= ?").setParameter(0, id).list();
您的查询错误。您应该在查询中使用当前的bean类名而不是表名,即createQuery("FROM UserDetail WHERE id = ?")