daily pastebin goal
38%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  2.     <property name="dataSource" ref="dataSource" />
  3.     <property name="hibernateProperties">
  4.         <props>
  5.             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  6.             <prop key="hibernate.show_sql">true</prop>
  7.         </props>
  8.     </property>
  9.     <property name="annotatedClasses">
  10.         <list>
  11.               <value>com.fabbydesign.model.Useradmin</value>
  12.         </list>
  13.     </property>
  14. </bean>
  15.    
  16. @RequestMapping(value = "/do.html", method = RequestMethod.POST)
  17. public String doLogin(@Valid @ModelAttribute("userLogin") LoginForm lf, BindingResult bindingResult, Map<String, Object> model, HttpServletRequest request){
  18.  
  19.     //bindingResult.rejectValue("username", "label.title"); - to add new message error
  20.     logger.trace("showLogin - post");
  21.  
  22.     //check fields if the filds are filled and respects the size from beans
  23.     if(bindingResult.hasErrors())
  24.         return "login";
  25.  
  26.     boolean userExists = loginService.checkLogin(lf.getUsername(), lf.getPassword());
  27.  
  28.     if(!userExists){
  29.         bindingResult.rejectValue("username", "login.username.wrongUserOrPassword");
  30.         return "login";
  31.     }
  32.     else{//set session
  33.         request.getSession().setAttribute(adminSesName, true);
  34.     }
  35.  
  36.     return "redirect:/admin/dashboard.html";
  37. }
  38.    
  39. @Service("loginService")
  40.    
  41. @Autowired
  42. private LoginDAO loginDAO;
  43.  
  44. public void setLoginDAO(LoginDAO loginDAO) {
  45.     this.loginDAO = loginDAO;
  46. }
  47.  
  48. public Boolean checkLogin(String userName, String userPassword) {
  49.     return loginDAO.checkLogin(userName, userPassword);
  50. }
  51.    
  52. @Repository("loginDAO")
  53.    
  54. @Resource(name = "sessionFactory")
  55. protected SessionFactory sessionFactory;
  56.  
  57. public void setSessionFactory(SessionFactory sessionFactory) {
  58.     this.sessionFactory = sessionFactory;
  59. }
  60.  
  61. protected Session getSession() {
  62.     return sessionFactory.openSession();
  63. }
  64.  
  65. final static Logger logger = Logger.getLogger(LoginDAOImpl.class);
  66.  
  67. public boolean checkLogin(String username, String password) {
  68.  
  69.     Boolean userFound = false;
  70.     Session session = sessionFactory.openSession();
  71.     String sql = "from Useradmin where username = ?";
  72.     Query<?> query = session.createQuery(sql);
  73.     query.setParameter(0, username);
  74.  
  75.     List<?> list = query.list();
  76.     if ((list != null) && (list.size() == 1)) {
  77.         Useradmin ua = (Useradmin) list.get(0);
  78.  
  79.         if (BCrypt.checkpw(password, ua.getPassword())) {
  80.             userFound = true;
  81.         }
  82.  
  83.     }
  84.     return userFound;
  85. }
  86.    
  87. package com.pechen.dao;
  88.  
  89. import java.util.List;
  90.  
  91. import com.pechen.entity.Person;
  92.  
  93. public interface PersonDao {
  94.  
  95.     public void save(Person p);
  96.  
  97.     public List<Person> list();
  98.  
  99. }
  100.    
  101. package com.pechen.dao;
  102.  
  103. import java.util.List;
  104.  
  105. import org.hibernate.Session;
  106. import org.hibernate.SessionFactory;
  107. import org.hibernate.Transaction;
  108. import org.springframework.beans.factory.annotation.Autowired;
  109.  
  110. import com.pechen.entity.Person;
  111.  
  112. public class PersonDaoImpl implements PersonDao {
  113.  
  114.     @Autowired
  115.     private SessionFactory sessionFactory;
  116.  
  117.     public void setSessionFactory(SessionFactory sessionFactory) {
  118.         this.sessionFactory = sessionFactory;
  119.     }
  120.  
  121.     public void save(Person p) {
  122.         Session session = sessionFactory.openSession();
  123.         Transaction tx = session.beginTransaction();
  124.         session.save(p);
  125.         tx.commit();
  126.         session.close();
  127.     }
  128.  
  129.     @SuppressWarnings("unchecked")
  130.     public List<Person> list() {
  131.         Session session = sessionFactory.openSession();
  132.         List<Person> rst = session.createQuery("from person").getResultList();
  133.         session.close();
  134.         return rst;
  135.     }
  136.  
  137. }
  138.    
  139. package com.pechen.service;
  140.  
  141. import java.util.List;
  142.  
  143. import org.springframework.beans.factory.annotation.Autowired;
  144.  
  145. import com.pechen.dao.PersonDao;
  146. import com.pechen.dao.PersonDaoImpl;
  147. import com.pechen.entity.Person;
  148.  
  149. public class PersonService {
  150.  
  151.     @Autowired
  152.     private PersonDao personDao;
  153.  
  154.     public void setPersonDao(PersonDaoImpl personImpl) {
  155.         this.personDao = personImpl;
  156.     }
  157.  
  158.     public void savePerson(Person person) {
  159.         // other business logic
  160.         this.personDao.save(person);
  161.         System.out.println("Save person successfully: " + person);
  162.     }
  163.  
  164.     public List<Person> getPersons() {
  165.         // other business logic
  166.         return this.personDao.list();
  167.     }
  168.  
  169. }
RAW Paste Data
Top