Báze pro DAO a business logiku, nastavení Hibernate, nastavení security.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package info.bukova.isspst;
|
||||
|
||||
import info.bukova.isspst.data.User;
|
||||
import info.bukova.isspst.services.UserService;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
/**
|
||||
* Handles requests for the application home page.
|
||||
*/
|
||||
@Controller
|
||||
public class HomeController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* Simply selects the home view to render by returning its name.
|
||||
*/
|
||||
@RequestMapping(value = "/", method = RequestMethod.GET)
|
||||
public String home(Locale locale, Model model) {
|
||||
logger.info("Welcome home! The client locale is {}.", locale);
|
||||
|
||||
Date date = new Date();
|
||||
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
|
||||
|
||||
String formattedDate = dateFormat.format(date);
|
||||
|
||||
model.addAttribute("serverTime", formattedDate );
|
||||
|
||||
User user = new User();
|
||||
user.setUsername("pokus");
|
||||
|
||||
userService.add(user);
|
||||
|
||||
return "home";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package info.bukova.isspst.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Query;
|
||||
|
||||
public interface BaseDao<T> {
|
||||
|
||||
public void add(T entity);
|
||||
public void delete(T entity);
|
||||
public void modify(T entity);
|
||||
public T getById(int id);
|
||||
public List<T> getAll();
|
||||
public Query getQuery(String query);
|
||||
public List<T> execQuery(String query);
|
||||
public String getTableName();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package info.bukova.isspst.dao;
|
||||
|
||||
import info.bukova.isspst.data.Role;
|
||||
|
||||
public interface RoleDao extends BaseDao<Role> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package info.bukova.isspst.dao;
|
||||
|
||||
import info.bukova.isspst.data.User;
|
||||
|
||||
public interface UserDao extends BaseDao<User> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package info.bukova.isspst.dao.jpa;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import info.bukova.isspst.dao.BaseDao;
|
||||
|
||||
public abstract class BaseDaoJPA<T> implements BaseDao<T> {
|
||||
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
public void setSessionFactory(SessionFactory factory) {
|
||||
this.sessionFactory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(T entity) {
|
||||
sessionFactory.getCurrentSession().save(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(T entity) {
|
||||
sessionFactory.getCurrentSession().delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modify(T entity) {
|
||||
sessionFactory.getCurrentSession().update(entity);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T getById(int id) {
|
||||
Query q = sessionFactory.getCurrentSession().createQuery("from " + getTableName() + " e where ID = :id");
|
||||
q.setInteger("id", id);
|
||||
return (T) q.uniqueResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query getQuery(String query) {
|
||||
return sessionFactory.getCurrentSession().createQuery(query);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<T> execQuery(String query) {
|
||||
return sessionFactory.getCurrentSession().createQuery(query).list();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<T> getAll() {
|
||||
return sessionFactory.getCurrentSession().createQuery("from " + getTableName()).list();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package info.bukova.isspst.dao.jpa;
|
||||
|
||||
import info.bukova.isspst.dao.RoleDao;
|
||||
import info.bukova.isspst.data.Role;
|
||||
|
||||
public class RoleDaoJPA extends BaseDaoJPA<Role> implements RoleDao {
|
||||
|
||||
@Override
|
||||
public String getTableName() {
|
||||
return "ROLE";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package info.bukova.isspst.dao.jpa;
|
||||
|
||||
import info.bukova.isspst.dao.UserDao;
|
||||
import info.bukova.isspst.data.User;
|
||||
|
||||
public class UserDaoJPA extends BaseDaoJPA<User> implements UserDao {
|
||||
|
||||
|
||||
@Override
|
||||
public String getTableName() {
|
||||
return "USER";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package info.bukova.isspst.data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public interface DataModel {
|
||||
|
||||
public void setId(int id);
|
||||
public int getId();
|
||||
public void setCreated(Date created);
|
||||
public Date getCreated();
|
||||
public void setModified(Date modified);
|
||||
public Date getModified();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package info.bukova.isspst.data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
|
||||
@Entity
|
||||
@Table(name="ROLE")
|
||||
public class Role implements GrantedAuthority, DataModel {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 5677876140880991163L;
|
||||
|
||||
@Id
|
||||
@Column(name="ID")
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
@Column(name="AUTHORITY")
|
||||
private String authority;
|
||||
@Column(name="DESCRIPTION")
|
||||
private String description;
|
||||
@Column(name="CREATED")
|
||||
private Date created;
|
||||
@Column(name="MODIFIED")
|
||||
private Date modified;
|
||||
|
||||
@Override
|
||||
public String getAuthority() {
|
||||
return authority;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public void setAuthority(String authority) {
|
||||
this.authority = authority;
|
||||
}
|
||||
|
||||
public Date getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(Date created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
public Date getModified() {
|
||||
return modified;
|
||||
}
|
||||
|
||||
public void setModified(Date modified) {
|
||||
this.modified = modified;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package info.bukova.isspst.data;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.MapKeyColumn;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
@Entity
|
||||
@Table(name="USER")
|
||||
public class User implements UserDetails, DataModel {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 5421234421833765433L;
|
||||
|
||||
@Id
|
||||
@Column(name="ID")
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
@Column(name="USERNAME")
|
||||
private String username;
|
||||
@Column(name="PASSWORD")
|
||||
private String password;
|
||||
@Column(name="ENABLED")
|
||||
private boolean enabled;
|
||||
@OneToMany @JoinTable(name="USER_ROLE")
|
||||
@MapKeyColumn(name="ROLE_ID")
|
||||
private List<Role> authorities;
|
||||
@Column(name="CREATED")
|
||||
private Date created;
|
||||
@Column(name="MODIFIED")
|
||||
private Date modified;
|
||||
|
||||
@Override
|
||||
public List<Role> getAuthorities() {
|
||||
return authorities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccountNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccountNonLocked() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCredentialsNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public void setAuthorities(List<Role> authorities) {
|
||||
this.authorities = authorities;
|
||||
}
|
||||
|
||||
public void addAuthority(Role role) {
|
||||
this.authorities.add(role);
|
||||
}
|
||||
|
||||
public Date getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(Date created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
public Date getModified() {
|
||||
return modified;
|
||||
}
|
||||
|
||||
public void setModified(Date modified) {
|
||||
this.modified = modified;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package info.bukova.isspst.services;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import info.bukova.isspst.dao.BaseDao;
|
||||
import info.bukova.isspst.data.DataModel;
|
||||
|
||||
public abstract class AbstractService<T extends DataModel> implements Service<T> {
|
||||
|
||||
protected BaseDao<T> dao;
|
||||
|
||||
public void setDao(BaseDao<T> dao) {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void add(T entity) {
|
||||
entity.setCreated(new Date());
|
||||
dao.add(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void update(T entity) {
|
||||
entity.setModified(new Date());
|
||||
dao.modify(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void delete(T entity) {
|
||||
dao.delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> getAll() {
|
||||
return dao.getAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> execQuery(String query) {
|
||||
return dao.execQuery(query);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T selectSingle(String query) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package info.bukova.isspst.services;
|
||||
|
||||
import info.bukova.isspst.data.Role;
|
||||
|
||||
public interface RoleService extends Service<Role> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package info.bukova.isspst.services;
|
||||
|
||||
import info.bukova.isspst.data.Role;
|
||||
|
||||
public class RoleServiceImpl extends AbstractService<Role> implements RoleService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package info.bukova.isspst.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Service<T> {
|
||||
|
||||
public void add(T entity);
|
||||
public void update(T entity);
|
||||
public void delete(T entity);
|
||||
public List<T> getAll();
|
||||
public List<T> execQuery(String query);
|
||||
public T selectSingle(String query);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package info.bukova.isspst.services;
|
||||
|
||||
import info.bukova.isspst.data.User;
|
||||
|
||||
public interface UserService extends Service<User> {
|
||||
|
||||
public void setPassword(User user, String password);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package info.bukova.isspst.services;
|
||||
|
||||
import org.hibernate.Query;
|
||||
import org.springframework.security.authentication.encoding.PasswordEncoder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import info.bukova.isspst.data.User;
|
||||
|
||||
public class UserServiceImpl extends AbstractService<User> implements UserService, UserDetailsService {
|
||||
|
||||
private PasswordEncoder encoder;
|
||||
|
||||
public void setEncoder(PasswordEncoder encoder) {
|
||||
this.encoder = encoder;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public UserDetails loadUserByUsername(String username)
|
||||
throws UsernameNotFoundException {
|
||||
|
||||
Query q = dao.getQuery("from User user where user.username = :username");
|
||||
q.setString("username", username);
|
||||
User u = (User) q.uniqueResult();
|
||||
|
||||
if (u == null)
|
||||
throw new UsernameNotFoundException("User " + username + " not found");
|
||||
|
||||
return u;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPassword(User user, String password) {
|
||||
user.setPassword(encoder.encodePassword(password, user.getUsername()));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user