Guest User

Untitled

a guest
Dec 19th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.32 KB | None | 0 0
  1. **class Bean**
  2.  
  3.  
  4.  
  5. package delphos.bean;
  6.  
  7. import java.util.Date;
  8. import java.util.List;
  9.  
  10. import javax.persistence.CascadeType;
  11. import javax.persistence.Column;
  12. import javax.persistence.Entity;
  13. import javax.persistence.GeneratedValue;
  14. import javax.persistence.Id;
  15. import javax.persistence.JoinColumn;
  16. import javax.persistence.JoinColumns;
  17. import javax.persistence.JoinTable;
  18. import javax.persistence.ManyToMany;
  19. import javax.persistence.ManyToOne;
  20. import javax.persistence.Table;
  21.  
  22.  
  23. @Entity
  24. @Table(name="user")
  25. //@Scope(value="session")
  26. public class DbUser {
  27. @Id
  28. @GeneratedValue
  29. @Column(name="ID_USER")
  30. private int idUser;
  31.  
  32. @Column(name="USERNAME")
  33. private String username;
  34.  
  35. @Column(name="PASSWORD")
  36. private String password;
  37.  
  38. @Column(name="NAME")
  39. private String name;
  40.  
  41. @Column(name="APELLIDO_PAT")
  42. private String apellido;
  43.  
  44. @Column(name="APELLIDO_MAT")
  45. private String apellidoMat;
  46.  
  47. @Column(name="TELEFONO")
  48. private String telefono;
  49.  
  50. @Column(name="AUTORIZA")
  51. private String autoriza;
  52.  
  53. @Column(name="CORREO")
  54. private String correo;
  55.  
  56. @Column(name="ESTADO")
  57. private String estado;
  58.  
  59. @Column(name="FECHA_REG")
  60. private Date fechaReg;
  61.  
  62. @Column(name="CODUSER")
  63. private String codUser;
  64.  
  65. @Column(name="ID_AREA_SOIT")
  66. private String area_soit;
  67.  
  68. @ManyToOne(targetEntity=Area.class)
  69. @JoinColumn(name="ID_AREA")
  70. private Area area;
  71.  
  72. @ManyToMany(targetEntity=Rol.class)
  73. @JoinTable(name = "user_rol", joinColumns = { @JoinColumn(name = "ID_USER") }, inverseJoinColumns = { @JoinColumn(name = "ID_ROL") })
  74. private List<Rol> listRol;
  75.  
  76. //GET Y SET
  77.  
  78. public String getUsername() {
  79. return username;
  80. }
  81. public String getArea_soit() {
  82. return area_soit;
  83. }
  84. public void setArea_soit(String area_soit) {
  85. this.area_soit = area_soit;
  86. }
  87. public String getApellidoMat() {
  88. return apellidoMat;
  89. }
  90. public void setApellidoMat(String apellidoMat) {
  91. this.apellidoMat = apellidoMat;
  92. }
  93. public String getCodUser() {
  94. return codUser;
  95. }
  96. public void setCodUser(String codUser) {
  97. this.codUser = codUser;
  98. }
  99. public List<Rol> getListRol() {
  100. return listRol;
  101. }
  102. public void setListRol(List<Rol> listRol) {
  103. this.listRol = listRol;
  104. }
  105. public void setUsername(String username) {
  106. this.username = username;
  107. }
  108. public String getPassword() {
  109. return password;
  110. }
  111. public void setPassword(String password) {
  112. this.password = password;
  113. }
  114. public int getIdUser() {
  115. return idUser;
  116. }
  117. public void setIdUser(int idUser) {
  118. this.idUser = idUser;
  119. }
  120. public String getName() {
  121. return name;
  122. }
  123. public void setName(String name) {
  124. this.name = name;
  125. }
  126. public String getApellido() {
  127. return apellido;
  128. }
  129. public void setApellido(String apellido) {
  130. this.apellido = apellido;
  131. }
  132. public String getTelefono() {
  133. return telefono;
  134. }
  135. public void setTelefono(String telefono) {
  136. this.telefono = telefono;
  137. }
  138. public String getAutoriza() {
  139. return autoriza;
  140. }
  141. public void setAutoriza(String autoriza) {
  142. this.autoriza = autoriza;
  143. }
  144. public String getCorreo() {
  145. return correo;
  146. }
  147. public void setCorreo(String correo) {
  148. this.correo = correo;
  149. }
  150. public String getEstado() {
  151. return estado;
  152. }
  153. public void setEstado(String estado) {
  154. this.estado = estado;
  155. }
  156. public Date getFechaReg() {
  157. return fechaReg;
  158. }
  159. public void setFechaReg(Date fechaReg) {
  160. this.fechaReg = fechaReg;
  161. }
  162. public Area getArea() {
  163. return area;
  164. }
  165. public void setArea(Area area) {
  166. this.area = area;
  167. }
  168. }
  169.  
  170. package delphos.web;
  171.  
  172. import org.apache.log4j.Logger;
  173. import org.springframework.stereotype.Controller;
  174. import org.springframework.ui.ModelMap;
  175. import org.springframework.web.bind.annotation.RequestMapping;
  176. import org.springframework.web.bind.annotation.RequestMethod;
  177. import org.springframework.web.bind.annotation.RequestParam;
  178.  
  179. import delphos.util.ParametrosDelphos;
  180.  
  181. @Controller
  182. @RequestMapping("/auth")
  183. public class LoginLogoutController {
  184.  
  185. Logger log = Logger.getLogger(ParametrosDelphos.LOG_CONTROLLER);
  186.  
  187. @RequestMapping(value = "/login", method = RequestMethod.GET)
  188. public String getLoginPage( @RequestParam(value = "error", required = false) boolean error,
  189. ModelMap model) {
  190. log.debug("Received request to show login page");
  191. // Add an error message to the model if login is unsuccessful
  192. // The 'error' parameter is set to true based on the when the
  193. // authentication has failed.
  194. // We declared this under the authentication-failure-url attribute
  195. // inside the spring-security.xml
  196. /*
  197. * See below: <form-login login-page="/krams/auth/login"
  198. * authentication-failure-url="/krams/auth/login?error=true"
  199. * default-target-url="/krams/main/common"/>
  200. */
  201. if (error == true) {
  202. // Assign an error message
  203. model.put("error", "El nombre del usuario o la contraseña es incorrecto.");
  204. } else {
  205. model.put("error", "");
  206. }
  207.  
  208. // This will resolve to /WEB-INF/jsp/loginpage.jsp
  209. return "login";
  210. }
  211.  
  212. /**
  213. * Handles and retrieves the denied JSP page. This is shown whenever a
  214. * regular user tries to access an admin only page.
  215. *
  216. * @return the name of the JSP page
  217. */
  218. @RequestMapping(value = "/denied", method = RequestMethod.GET)
  219. public String getDeniedPage() {
  220. log.debug("Received request to show denied page");
  221. // This will resolve to /WEB-INF/jsp/deniedpage.jsp
  222. return "deniedpage";
  223. }
  224. }
  225.  
  226. package delphos.service;
  227.  
  228. import java.util.ArrayList;
  229. import java.util.Collection;
  230. import java.util.List;
  231.  
  232. import org.apache.log4j.Logger;
  233. import org.hibernate.Session;
  234. import org.hibernate.SessionFactory;
  235. import org.springframework.beans.factory.annotation.Autowired;
  236. import org.springframework.dao.DataAccessException;
  237. import org.springframework.security.core.GrantedAuthority;
  238. import org.springframework.security.core.authority.GrantedAuthorityImpl;
  239. import org.springframework.security.core.context.SecurityContextHolder;
  240. import org.springframework.security.core.userdetails.User;
  241. import org.springframework.security.core.userdetails.UserDetails;
  242. import org.springframework.security.core.userdetails.UserDetailsService;
  243. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  244. import org.springframework.transaction.annotation.Transactional;
  245.  
  246. import delphos.bean.DbUser;
  247. import delphos.bean.Rol;
  248. import delphos.dao.UserDAO;
  249. import delphos.util.ParametrosDelphos;
  250.  
  251. //@Service
  252. @Transactional(readOnly=true)
  253. public class CustomUserDetailsService implements UserDetailsService{
  254.  
  255. Logger log = Logger.getLogger(ParametrosDelphos.LOG_SERVICE);
  256.  
  257. private SessionFactory sessionFactory;
  258.  
  259. // @Resource(name="userDao")
  260. // private UserDAO userDao;
  261. //
  262. @Autowired
  263. public void setSessionFactory(SessionFactory sessionFactory){
  264. this.sessionFactory = sessionFactory;
  265. }
  266.  
  267. private UserDAO userDao = new UserDAO();
  268.  
  269. public UserDetails loadUserByUsername(String username)
  270. throws UsernameNotFoundException, DataAccessException {
  271.  
  272. // Declare a null Spring User
  273. UserDetails user = null;
  274. Session session = sessionFactory.openSession();
  275. try {
  276.  
  277. // Search database for a user that matches the specified username
  278. // You can provide a custom DAO to access your persistence layer
  279. // Or use JDBC to access your database
  280. // DbUser is our custom domain user. This is not the same as Spring's User
  281. DbUser dbUser = userDao.searchDatabase(username,session);
  282.  
  283. // Populate the Spring User object with details from the dbUser
  284. // Here we just pass the username, password, and access level
  285. // getAuthorities() will translate the access level to the correct role type
  286. log.error("Error in retrieving user --- "+ dbUser.getName());
  287. user = new User(
  288. dbUser.getUsername(),
  289. dbUser.getPassword().toLowerCase(),
  290. true,
  291. true,
  292. true,
  293. true,
  294. getAuthorities(dbUser.getListRol()) );
  295.  
  296. session.close();
  297. } catch (Exception e) {
  298. e.printStackTrace();
  299. session.close();
  300. log.error("Error in retrieving user");
  301. throw new UsernameNotFoundException("Error in retrieving user");
  302. }
  303.  
  304. // Return user to Spring for processing.
  305. // Take note we're not the one evaluating whether this user is authenticated or valid
  306. // We just merely retrieve a user that matches the specified username
  307. return user;
  308. }
  309.  
  310. public DbUser currentUserDetails(){
  311. SecurityContextHolder.getContext().getAuthentication().getName();
  312. return null;
  313. }
  314. /**
  315. * Retrieves the correct ROLE type depending on the access level, where access level is an Integer.
  316. * Basically, this interprets the access value whether it's for a regular user or admin.
  317. *
  318. * @param access an integer value representing the access of the user
  319. * @return collection of granted authorities
  320. */
  321. public Collection<GrantedAuthority> getAuthorities(List<Rol> roles) {
  322. // Create a list of grants for this user
  323. List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
  324.  
  325.  
  326. // All users are granted with ROLE_USER access
  327. // Therefore this user gets a ROLE_USER by default
  328. // log.debug("Grant ROLE_USER to this user");
  329. // authList.add(new GrantedAuthorityImpl("ROLE_USER"));
  330.  
  331. for (int i = 0; i < roles.size(); i++) {
  332. Rol rol = roles.get(i);
  333. log.debug("Grant ROLE_USER to this user "+rol.getName());
  334. authList.add(new GrantedAuthorityImpl(rol.getName()));
  335. }
  336. // Check if this user has admin access
  337. // We interpret Integer(1) as an admin user
  338. // if ( access.compareTo(1) == 0) {
  339. // User has admin access
  340. // log.debug("Grant ROLE_ADMIN to this user");
  341. // authList.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
  342. // }
  343.  
  344. // Return list of granted authorities
  345. return authList;
  346. }
  347. }
  348.  
  349. package delphos.dao;
  350.  
  351. import java.util.ArrayList;
  352. import java.util.List;
  353.  
  354. import org.apache.log4j.Logger;
  355. import org.hibernate.Session;
  356. import org.springframework.stereotype.Repository;
  357. import org.springframework.transaction.annotation.Transactional;
  358.  
  359. import delphos.bean.DbUser;
  360. import delphos.util.ParametrosDelphos;
  361.  
  362. @Repository("userDao")
  363. public class UserDAO {
  364.  
  365. Logger log = Logger.getLogger(ParametrosDelphos.LOG_DAO);
  366. /**
  367. * Simulates retrieval of data from a database.
  368. */
  369. public DbUser searchDatabase(String username, Session session) {
  370. return (DbUser) session.createQuery("from DbUser u where u.username = :username")
  371. .setParameter("username", username).uniqueResult();
  372. // Retrieve all users from the database
  373. // List<DbUser> users = internalDatabase();
  374. //
  375. // // Search user based on the parameters
  376. // for(DbUser dbUser:users) {
  377. // if ( dbUser.getUsername().equals(username) == true ) {
  378. // log.debug("User found");
  379. // // return matching user
  380. // return dbUser
  381. // }
  382. // }
  383. //
  384. // log.error("User does not exist!");
  385. // throw new RuntimeException("User does not exist!");
  386. }
  387.  
  388. /**
  389. * Our fake database. Here we populate an ArrayList with a dummy list of users.
  390. */
  391. // private List<DbUser> internalDatabase() {
  392. // // Dummy database
  393. //
  394. // // Create a dummy array list
  395. // List<DbUser> users = new ArrayList<DbUser>();
  396. // DbUser user = null;
  397. //
  398. // // Create a new dummy user
  399. // user = new DbUser();
  400. // user.setUsername("john");
  401. // // Actual password: admin
  402. // user.setPassword("21232f297a57a5a743894a0e4a801fc3");
  403. // // Admin user
  404. // user.setAccess(1);
  405. //
  406. // // Add to array list
  407. // users.add(user);
  408. //
  409. // // Create a new dummy user
  410. // user = new DbUser();
  411. // user.setUsername("jane");
  412. // // Actual password: user
  413. // user.setPassword("ee11cbb19052e40b07aac0ca060c23ee");
  414. // // Regular user
  415. // user.setAccess(2);
  416. //
  417. // // Add to array list
  418. // users.add(user);
  419. //
  420. // return users;
  421. // }
  422. }
  423.  
  424. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  425. <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
  426. <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
  427.  
  428. <%@ page language="java" contentType="text/html; charset=UTF-8"
  429. pageEncoding="UTF-8"%>
  430. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  431. <html>
  432. <head>
  433. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  434. <title><spring:message code="nomApp"/></title>
  435.  
  436. <link href="<spring:url value="/static/css/estilos.css" htmlEscape="true" />" rel="stylesheet" type="text/css"/>
  437. <link rel="shortcut icon" href="<spring:url value="/static/images/Letter_D.ico" htmlEscape="true" />" type="image/x-icon"/>
  438. <!-- <link href="<spring:url value="/static/css/960.css" htmlEscape="true" />" rel="stylesheet" type="text/css" media="all" />-->
  439. <!-- <link href="<spring:url value="/static/css/reset.css" htmlEscape="true" />" rel="stylesheet" type="text/css" media="all" />-->
  440. <!-- <link href="<spring:url value="/static/css/text.css" htmlEscape="true" />" rel="stylesheet" type="text/css" media="all" />-->
  441. <!-- <link href="<spring:url value="/static/css/login.css" htmlEscape="true" />" rel="stylesheet" type="text/css" media="all" />-->
  442.  
  443. <style type="text/css" media="screen">
  444. #slider {
  445. width: 535px; /* important to be same as image width */
  446. height: 356px; /* important to be same as image height */
  447. position: relative; /* important */
  448. overflow: hidden; /* important */
  449. }
  450. #sliderContent {
  451. width: 410px; /* important to be same as image width or wider */
  452. position: absolute;
  453. top: 0;
  454. margin-left: 0;
  455. }
  456. .sliderImage {
  457. float: left;
  458. position: relative;
  459. display: none;
  460. }
  461. .sliderImage span {
  462. position: absolute;
  463. font: 10px/15px Arial, Helvetica, sans-serif;
  464. padding: 10px 13px;
  465. width: 384px;
  466. background-color: #000;
  467. filter: alpha(opacity=70);
  468. -moz-opacity: 0.7;
  469. -khtml-opacity: 0.7;
  470. opacity: 0.7;
  471. color: #fff;
  472. display: none;
  473. }
  474. .clear {
  475. clear: both;
  476. }
  477. .sliderImage span strong {
  478. font-size: 14px;
  479. }
  480.  
  481. ul { list-style-type: none;}
  482. </style>
  483. <!-- JavaScripts-->
  484. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  485.  
  486. <script type="text/javascript" src="<spring:url value="/static/js/s3Slider.js" htmlEscape="true" />"></script>
  487.  
  488. <script type="text/javascript">
  489. $(document).ready(function() {
  490. $('#slider').s3Slider({
  491. timeOut: 7000
  492. });
  493.  
  494.  
  495. });
  496. </script>
  497. </head>
  498.  
  499. <body>
  500.  
  501.  
  502.  
  503. <h1>Login</h1>
  504. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  505. <tr>
  506. <td valign="top">
  507. <table align="left" width="572" height="390" border="0"
  508. cellspacing="0" cellpadding="0"
  509. style="background-repeat: no-repeat; background-image: url(<spring:url value="/static/images/fondo_login.jpg" htmlEscape="true" />)">
  510. <tr>
  511. <td colspan="2" class="titulo_login" align="left">
  512. DELPHOS 3.0
  513. </td>
  514. </tr>
  515. <tr>
  516. <td width="200" class="texto_login_error">
  517. ${error}
  518. </td>
  519. <td width="372" rowspan="3" valign="top">
  520. <table width="340" height="200" border="0" cellspacing="0"
  521. cellpadding="0"
  522. style="background-repeat: no-repeat; background-image: url(<spring:url value="/static/images/fondo_login_chico.jpg" htmlEscape="true" />) ">
  523. <tr>
  524. <td colspan="2" valign="top">
  525. <form name="formLogin" id="formLogin" action="../j_spring_security_check" method="post">
  526. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  527. <tr>
  528. <td colspan="2" class="texto_login_azul">
  529. <spring:message code="textLogin" /></td>
  530. </tr>
  531. <tr>
  532. <td width="41%" class="cabecera_link" style="padding-left: 15px">
  533. <spring:message code="username" />:
  534. </td>
  535. <td width="59%" height="40">
  536. <input id="j_username" name="j_username" type="text" maxlength="30" class="inputText"/></td>
  537. </tr>
  538. <tr>
  539. <td class="cabecera_link" style="padding-left: 15px">
  540. <spring:message code="password" />:
  541. </td>
  542. <td height="40"><input id="j_password" name="j_password"
  543. type="password" maxlength="30" class="inputText"/></td>
  544. </tr>
  545. <!-- <tr>-->
  546. <!-- <td colspan="2" class="texto_login_azul">-->
  547. <!-- Don't ask for my password for two weeks <input type="checkbox" name="_spring_security_remember_me"></td>-->
  548. <!-- </tr>-->
  549. <tr>
  550. <td>&nbsp;</td>
  551. <td height="50"><input type="image"
  552. src="<spring:url value="/static/images/boton_login.jpg" htmlEscape="true" />"
  553. width="97" height="28" onclick="document['formLogin'].submit()" />
  554. </td>
  555. </tr>
  556.  
  557. </table>
  558. </form>
  559. </td>
  560. </tr>
  561. </table>
  562. </td>
  563. </tr>
  564. <tr>
  565. <td class="texto_login" align="left"></td>
  566. </tr>
  567. <tr>
  568. <td align="center"><img
  569. src="<spring:url value="/static/images/j_login_lock.jpg" htmlEscape="true" />"
  570. width="152" height="137" /></td>
  571. </tr>
  572. <tr>
  573. <td colspan="2" align="center" class="texto_login"><spring:message
  574. code="powered" /></td>
  575. </tr>
  576.  
  577.  
  578. <tr>
  579. <td colspan="2" class="texto_login_azul2"> Navegador recomendado:
  580. <img src="<spring:url value="/static/images/Firefox-32.png"/>" alt="Firefox" style="margin-right: 10px;margin-left: 10px;margin-bottom: -5px" height="22px" width="22px"/>
  581. <img src="<spring:url value="/static/images/Chrome-32.png"/>" alt="Chrome" style="margin-right: 10px;margin-bottom: -5px" height="22px" width="22px"/>
  582. <img src="<spring:url value="/static/images/Opera-32.png"/>" alt="Opera" style="margin-right: 10px;margin-bottom: -5px" height="22px" width="22px"/>
  583. </td>
  584. </tr>
  585. </table>
  586. </td>
  587. <td style="border-left: 4px solid #0072e7; padding: 35px;"></td>
  588. <td valign="top">
  589. <table align="right" cellspacing="0" cellpadding="0" width="100%" border="0">
  590. <tr>
  591. <td>&nbsp;</td>
  592. <td>&nbsp;</td>
  593. </tr>
  594. <tr>
  595. <td colspan="2" align="center"><img src="<spring:url value="/static/images/login/titulo.jpg"/>" alt="Opera" style="margin-right: 10px;margin-bottom: -5px" /> </td>
  596. </tr>
  597. <tr>
  598. <td height="250"><img src="<spring:url value="/static/images/login/texto_chrome.jpg"/>" alt="Opera" style="margin-right: 10px;margin-bottom: -5px" /></td>
  599. <td rowspan="2">
  600. <div id="slider">
  601. <ul id="sliderContent">
  602. <li class="sliderImage">
  603. <img src="<spring:url value="/static/images/login/considerar1.jpg"/>" alt="1" />
  604. <span style="background-color: white;"></span>
  605. </li>
  606. <li class="sliderImage">
  607. <img src="<spring:url value="/static/images/login/considerar2.jpg"/>" alt="1" />
  608. <span style="background-color: white;"></span>
  609. </li>
  610.  
  611. <div class="clear sliderImage"></div>
  612. </ul>
  613. </div>
  614. </td>
  615. </tr>
  616. <tr>
  617. <td height="250"><img src="<spring:url value="/static/images/login/texto_mozilla.jpg"/>" alt="Opera" style="margin-right: 10px;margin-bottom: -5px" /></td>
  618. </tr>
  619. <tr>
  620. <td><img src="<spring:url value="/static/images/login/texto_dash.jpg"/>" alt="Opera" style="margin-right: 10px;margin-bottom: -5px" /></td>
  621. <td style="padding-left: 50px;"><img src="<spring:url value="/static/images/login/img_dash.jpg"/>" alt="Opera" style="margin-right: 10px;margin-bottom: -5px" /></td>
  622. </tr>
  623. </table>
  624. </td>
  625. </tr>
  626. </table>
  627.  
  628.  
  629.  
  630. </body>
  631. </html>
Add Comment
Please, Sign In to add comment