[FEATURE] Реализация HibernateAuthHandler

This commit is contained in:
Gravit 2019-06-03 13:26:20 +07:00
parent 439b3f41c1
commit 868f5295d2
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
6 changed files with 84 additions and 2 deletions

View file

@ -2,6 +2,7 @@
import pro.gravit.launchserver.auth.provider.AuthProviderResult; import pro.gravit.launchserver.auth.provider.AuthProviderResult;
import pro.gravit.launchserver.auth.AuthException; import pro.gravit.launchserver.auth.AuthException;
import pro.gravit.launchserver.auth.provider.HibernateAuthProvider;
import pro.gravit.utils.ProviderMap; import pro.gravit.utils.ProviderMap;
import java.io.IOException; import java.io.IOException;
@ -23,6 +24,7 @@ public static void registerHandlers() {
providers.register("memory", MemoryAuthHandler.class); providers.register("memory", MemoryAuthHandler.class);
providers.register("mysql", MySQLAuthHandler.class); providers.register("mysql", MySQLAuthHandler.class);
providers.register("request", RequestAuthHandler.class); providers.register("request", RequestAuthHandler.class);
providers.register("hibernate", HibernateAuthHandler.class);
registredHandl = true; registredHandl = true;
} }
} }

View file

@ -0,0 +1,48 @@
package pro.gravit.launchserver.auth.handler;
import pro.gravit.launchserver.hibernate.User;
import pro.gravit.launchserver.hibernate.UserService;
import java.io.IOException;
import java.util.UUID;
public class HibernateAuthHandler extends CachedAuthHandler {
@Override
protected Entry fetchEntry(String username) throws IOException {
UserService service = new UserService();
User user = service.findUserByUsername(username);
if(user == null) return null;
return new Entry(user.uuid, username, user.getAccessToken(), user.serverID);
}
@Override
protected Entry fetchEntry(UUID uuid) throws IOException {
UserService service = new UserService();
User user = service.findUserByUUID(uuid);
if(user == null) return null;
return new Entry(user.uuid, user.username, user.getAccessToken(), user.serverID);
}
@Override
protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException {
UserService service = new UserService();
User user = service.findUserByUUID(uuid);
user.setAccessToken(accessToken);
service.updateUser(user);
return true;
}
@Override
protected boolean updateServerID(UUID uuid, String serverID) throws IOException {
UserService service = new UserService();
User user = service.findUserByUUID(uuid);
user.serverID = serverID;
service.updateUser(user);
return true;
}
@Override
public void close() throws IOException {
}
}

View file

@ -8,6 +8,7 @@
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
import java.util.UUID;
@Entity @Entity
@Table(name = "users") @Table(name = "users")
@ -17,8 +18,12 @@ public class User {
private long id; private long id;
@Column(unique = true) @Column(unique = true)
public String username; public String username;
@Column(unique = true)
public UUID uuid;
@Column(name = "password") @Column(name = "password")
private byte[] password; private byte[] password;
private String accessToken;
public String serverID;
private String password_salt; private String password_salt;
public void setPassword(String password) public void setPassword(String password)
{ {
@ -46,4 +51,12 @@ public boolean verifyPassword(String password)
LogHelper.info(Arrays.toString(this.password)); LogHelper.info(Arrays.toString(this.password));
return Arrays.equals(enpassword, this.password); return Arrays.equals(enpassword, this.password);
} }
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
} }

View file

@ -1,10 +1,12 @@
package pro.gravit.launchserver.hibernate; package pro.gravit.launchserver.hibernate;
import java.util.List; import java.util.List;
import java.util.UUID;
public interface UserDAO { public interface UserDAO {
User findById(int id); User findById(int id);
User findByUsername(String username); User findByUsername(String username);
User findByUUID(UUID uuid);
void save(User user); void save(User user);
void update(User user); void update(User user);
void delete(User user); void delete(User user);

View file

@ -8,6 +8,7 @@
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import java.util.List; import java.util.List;
import java.util.UUID;
public class UserDAOImpl implements UserDAO { public class UserDAOImpl implements UserDAO {
@ -22,9 +23,21 @@ public User findByUsername(String username) {
CriteriaQuery<User> personCriteria = cb.createQuery(User.class); CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
Root<User> rootUser = personCriteria.from(User.class); Root<User> rootUser = personCriteria.from(User.class);
personCriteria.select(rootUser).where(cb.equal(rootUser.get("username"), username)); personCriteria.select(rootUser).where(cb.equal(rootUser.get("username"), username));
User ret = em.createQuery(personCriteria).getSingleResult(); List<User> ret = em.createQuery(personCriteria).getResultList();
em.close(); em.close();
return ret; return ret.size() == 0 ? null : ret.get(0);
}
public User findByUUID(UUID uuid) {
EntityManager em = HibernateManager.sessionFactory.createEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
Root<User> rootUser = personCriteria.from(User.class);
personCriteria.select(rootUser).where(cb.equal(rootUser.get("uuid"), uuid));
List<User> ret = em.createQuery(personCriteria).getResultList();
em.close();
return ret.size() == 0 ? null : ret.get(0);
} }
public void save(User user) { public void save(User user) {

View file

@ -1,6 +1,7 @@
package pro.gravit.launchserver.hibernate; package pro.gravit.launchserver.hibernate;
import java.util.List; import java.util.List;
import java.util.UUID;
public class UserService { public class UserService {
@ -16,6 +17,9 @@ public User findUser(int id) {
public User findUserByUsername(String username) { public User findUserByUsername(String username) {
return usersDao.findByUsername(username); return usersDao.findByUsername(username);
} }
public User findUserByUUID(UUID uuid) {
return usersDao.findByUUID(uuid);
}
public void saveUser(User user) { public void saveUser(User user) {
usersDao.save(user); usersDao.save(user);