mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FEATURE] Реализация HibernateAuthHandler
This commit is contained in:
parent
439b3f41c1
commit
868f5295d2
6 changed files with 84 additions and 2 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
import pro.gravit.launchserver.auth.provider.AuthProviderResult;
|
||||
import pro.gravit.launchserver.auth.AuthException;
|
||||
import pro.gravit.launchserver.auth.provider.HibernateAuthProvider;
|
||||
import pro.gravit.utils.ProviderMap;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -23,6 +24,7 @@ public static void registerHandlers() {
|
|||
providers.register("memory", MemoryAuthHandler.class);
|
||||
providers.register("mysql", MySQLAuthHandler.class);
|
||||
providers.register("request", RequestAuthHandler.class);
|
||||
providers.register("hibernate", HibernateAuthHandler.class);
|
||||
registredHandl = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@
|
|||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
|
@ -17,8 +18,12 @@ public class User {
|
|||
private long id;
|
||||
@Column(unique = true)
|
||||
public String username;
|
||||
@Column(unique = true)
|
||||
public UUID uuid;
|
||||
@Column(name = "password")
|
||||
private byte[] password;
|
||||
private String accessToken;
|
||||
public String serverID;
|
||||
private String password_salt;
|
||||
public void setPassword(String password)
|
||||
{
|
||||
|
@ -46,4 +51,12 @@ public boolean verifyPassword(String password)
|
|||
LogHelper.info(Arrays.toString(this.password));
|
||||
return Arrays.equals(enpassword, this.password);
|
||||
}
|
||||
|
||||
public String getAccessToken() {
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
public void setAccessToken(String accessToken) {
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package pro.gravit.launchserver.hibernate;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface UserDAO {
|
||||
User findById(int id);
|
||||
User findByUsername(String username);
|
||||
User findByUUID(UUID uuid);
|
||||
void save(User user);
|
||||
void update(User user);
|
||||
void delete(User user);
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class UserDAOImpl implements UserDAO {
|
||||
|
||||
|
@ -22,9 +23,21 @@ public User findByUsername(String username) {
|
|||
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
||||
Root<User> rootUser = personCriteria.from(User.class);
|
||||
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();
|
||||
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) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package pro.gravit.launchserver.hibernate;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class UserService {
|
||||
|
||||
|
@ -16,6 +17,9 @@ public User findUser(int id) {
|
|||
public User findUserByUsername(String username) {
|
||||
return usersDao.findByUsername(username);
|
||||
}
|
||||
public User findUserByUUID(UUID uuid) {
|
||||
return usersDao.findByUUID(uuid);
|
||||
}
|
||||
|
||||
public void saveUser(User user) {
|
||||
usersDao.save(user);
|
||||
|
|
Loading…
Reference in a new issue