通过可审核或会话审核Spring Boot

时间:2018-09-23 13:36:21

标签: spring spring-boot

我想在每个实体中保存createdBy,updatedBy,createdDate,updatedDate。我们的项目没有使用spring安全性。我正在使用弹簧靴,休眠。

有什么方法可以在会话中存储用户实体并进行检索。

这是我的代码

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<User> {


    @CreatedBy
    protected User createdBy;


    @LastModifiedBy
    protected User lastModifiedBy;

    @CreatedDate
    @Temporal(TemporalType.DATE)
    protected Date creationDate = new Date();

    @LastModifiedDate
    @Temporal(TemporalType.DATE)
    protected Date lastModifiedDate= new Date();

@Entity
@Table(name = "data_source")
public class DataSource extends Auditable<String>{

@Service
@Transactional
public class UserServiceImpl implements UserService {
    boolean flag = false;
    @Autowired
    private UserRepository userRepo;

    @Autowired
    public HttpSession httpSession;

    @Override
    public User login(String userName, String Pwd) {
        boolean auth = false;       
        User loggedinUser = null;
        PasswordEncoder passwordEnocder = new BCryptPasswordEncoder();
        List<User> userList = userRepo.findByName(userName);

        if (userList.size() > 0) {
            for (User user : userList) {
                if (passwordEnocder.matches(Pwd, user.getPassword())) {
                    loggedinUser = user;
                    auth = true;
                }
            }
        }       
        httpSession.setAttribute("user", loggedinUser);         
        return loggedinUser;
    }

我试图从Auditable java类的会话中读取用户,但获取null。我没有使用控制器。

0 个答案:

没有答案