Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **class Bean**
- package delphos.bean;
- import java.util.Date;
- import java.util.List;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.JoinColumns;
- import javax.persistence.JoinTable;
- import javax.persistence.ManyToMany;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- @Entity
- @Table(name="user")
- //@Scope(value="session")
- public class DbUser {
- @Id
- @GeneratedValue
- @Column(name="ID_USER")
- private int idUser;
- @Column(name="USERNAME")
- private String username;
- @Column(name="PASSWORD")
- private String password;
- @Column(name="NAME")
- private String name;
- @Column(name="APELLIDO_PAT")
- private String apellido;
- @Column(name="APELLIDO_MAT")
- private String apellidoMat;
- @Column(name="TELEFONO")
- private String telefono;
- @Column(name="AUTORIZA")
- private String autoriza;
- @Column(name="CORREO")
- private String correo;
- @Column(name="ESTADO")
- private String estado;
- @Column(name="FECHA_REG")
- private Date fechaReg;
- @Column(name="CODUSER")
- private String codUser;
- @Column(name="ID_AREA_SOIT")
- private String area_soit;
- @ManyToOne(targetEntity=Area.class)
- @JoinColumn(name="ID_AREA")
- private Area area;
- @ManyToMany(targetEntity=Rol.class)
- @JoinTable(name = "user_rol", joinColumns = { @JoinColumn(name = "ID_USER") }, inverseJoinColumns = { @JoinColumn(name = "ID_ROL") })
- private List<Rol> listRol;
- //GET Y SET
- public String getUsername() {
- return username;
- }
- public String getArea_soit() {
- return area_soit;
- }
- public void setArea_soit(String area_soit) {
- this.area_soit = area_soit;
- }
- public String getApellidoMat() {
- return apellidoMat;
- }
- public void setApellidoMat(String apellidoMat) {
- this.apellidoMat = apellidoMat;
- }
- public String getCodUser() {
- return codUser;
- }
- public void setCodUser(String codUser) {
- this.codUser = codUser;
- }
- public List<Rol> getListRol() {
- return listRol;
- }
- public void setListRol(List<Rol> listRol) {
- this.listRol = listRol;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public int getIdUser() {
- return idUser;
- }
- public void setIdUser(int idUser) {
- this.idUser = idUser;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getApellido() {
- return apellido;
- }
- public void setApellido(String apellido) {
- this.apellido = apellido;
- }
- public String getTelefono() {
- return telefono;
- }
- public void setTelefono(String telefono) {
- this.telefono = telefono;
- }
- public String getAutoriza() {
- return autoriza;
- }
- public void setAutoriza(String autoriza) {
- this.autoriza = autoriza;
- }
- public String getCorreo() {
- return correo;
- }
- public void setCorreo(String correo) {
- this.correo = correo;
- }
- public String getEstado() {
- return estado;
- }
- public void setEstado(String estado) {
- this.estado = estado;
- }
- public Date getFechaReg() {
- return fechaReg;
- }
- public void setFechaReg(Date fechaReg) {
- this.fechaReg = fechaReg;
- }
- public Area getArea() {
- return area;
- }
- public void setArea(Area area) {
- this.area = area;
- }
- }
- package delphos.web;
- import org.apache.log4j.Logger;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import delphos.util.ParametrosDelphos;
- @Controller
- @RequestMapping("/auth")
- public class LoginLogoutController {
- Logger log = Logger.getLogger(ParametrosDelphos.LOG_CONTROLLER);
- @RequestMapping(value = "/login", method = RequestMethod.GET)
- public String getLoginPage( @RequestParam(value = "error", required = false) boolean error,
- ModelMap model) {
- log.debug("Received request to show login page");
- // Add an error message to the model if login is unsuccessful
- // The 'error' parameter is set to true based on the when the
- // authentication has failed.
- // We declared this under the authentication-failure-url attribute
- // inside the spring-security.xml
- /*
- * See below: <form-login login-page="/krams/auth/login"
- * authentication-failure-url="/krams/auth/login?error=true"
- * default-target-url="/krams/main/common"/>
- */
- if (error == true) {
- // Assign an error message
- model.put("error", "El nombre del usuario o la contraseña es incorrecto.");
- } else {
- model.put("error", "");
- }
- // This will resolve to /WEB-INF/jsp/loginpage.jsp
- return "login";
- }
- /**
- * Handles and retrieves the denied JSP page. This is shown whenever a
- * regular user tries to access an admin only page.
- *
- * @return the name of the JSP page
- */
- @RequestMapping(value = "/denied", method = RequestMethod.GET)
- public String getDeniedPage() {
- log.debug("Received request to show denied page");
- // This will resolve to /WEB-INF/jsp/deniedpage.jsp
- return "deniedpage";
- }
- }
- package delphos.service;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- import org.apache.log4j.Logger;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.dao.DataAccessException;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.authority.GrantedAuthorityImpl;
- import org.springframework.security.core.context.SecurityContextHolder;
- import org.springframework.security.core.userdetails.User;
- 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 delphos.bean.DbUser;
- import delphos.bean.Rol;
- import delphos.dao.UserDAO;
- import delphos.util.ParametrosDelphos;
- //@Service
- @Transactional(readOnly=true)
- public class CustomUserDetailsService implements UserDetailsService{
- Logger log = Logger.getLogger(ParametrosDelphos.LOG_SERVICE);
- private SessionFactory sessionFactory;
- // @Resource(name="userDao")
- // private UserDAO userDao;
- //
- @Autowired
- public void setSessionFactory(SessionFactory sessionFactory){
- this.sessionFactory = sessionFactory;
- }
- private UserDAO userDao = new UserDAO();
- public UserDetails loadUserByUsername(String username)
- throws UsernameNotFoundException, DataAccessException {
- // Declare a null Spring User
- UserDetails user = null;
- Session session = sessionFactory.openSession();
- try {
- // Search database for a user that matches the specified username
- // You can provide a custom DAO to access your persistence layer
- // Or use JDBC to access your database
- // DbUser is our custom domain user. This is not the same as Spring's User
- DbUser dbUser = userDao.searchDatabase(username,session);
- // Populate the Spring User object with details from the dbUser
- // Here we just pass the username, password, and access level
- // getAuthorities() will translate the access level to the correct role type
- log.error("Error in retrieving user --- "+ dbUser.getName());
- user = new User(
- dbUser.getUsername(),
- dbUser.getPassword().toLowerCase(),
- true,
- true,
- true,
- true,
- getAuthorities(dbUser.getListRol()) );
- session.close();
- } catch (Exception e) {
- e.printStackTrace();
- session.close();
- log.error("Error in retrieving user");
- throw new UsernameNotFoundException("Error in retrieving user");
- }
- // Return user to Spring for processing.
- // Take note we're not the one evaluating whether this user is authenticated or valid
- // We just merely retrieve a user that matches the specified username
- return user;
- }
- public DbUser currentUserDetails(){
- SecurityContextHolder.getContext().getAuthentication().getName();
- return null;
- }
- /**
- * Retrieves the correct ROLE type depending on the access level, where access level is an Integer.
- * Basically, this interprets the access value whether it's for a regular user or admin.
- *
- * @param access an integer value representing the access of the user
- * @return collection of granted authorities
- */
- public Collection<GrantedAuthority> getAuthorities(List<Rol> roles) {
- // Create a list of grants for this user
- List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
- // All users are granted with ROLE_USER access
- // Therefore this user gets a ROLE_USER by default
- // log.debug("Grant ROLE_USER to this user");
- // authList.add(new GrantedAuthorityImpl("ROLE_USER"));
- for (int i = 0; i < roles.size(); i++) {
- Rol rol = roles.get(i);
- log.debug("Grant ROLE_USER to this user "+rol.getName());
- authList.add(new GrantedAuthorityImpl(rol.getName()));
- }
- // Check if this user has admin access
- // We interpret Integer(1) as an admin user
- // if ( access.compareTo(1) == 0) {
- // User has admin access
- // log.debug("Grant ROLE_ADMIN to this user");
- // authList.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
- // }
- // Return list of granted authorities
- return authList;
- }
- }
- package delphos.dao;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.log4j.Logger;
- import org.hibernate.Session;
- import org.springframework.stereotype.Repository;
- import org.springframework.transaction.annotation.Transactional;
- import delphos.bean.DbUser;
- import delphos.util.ParametrosDelphos;
- @Repository("userDao")
- public class UserDAO {
- Logger log = Logger.getLogger(ParametrosDelphos.LOG_DAO);
- /**
- * Simulates retrieval of data from a database.
- */
- public DbUser searchDatabase(String username, Session session) {
- return (DbUser) session.createQuery("from DbUser u where u.username = :username")
- .setParameter("username", username).uniqueResult();
- // Retrieve all users from the database
- // List<DbUser> users = internalDatabase();
- //
- // // Search user based on the parameters
- // for(DbUser dbUser:users) {
- // if ( dbUser.getUsername().equals(username) == true ) {
- // log.debug("User found");
- // // return matching user
- // return dbUser
- // }
- // }
- //
- // log.error("User does not exist!");
- // throw new RuntimeException("User does not exist!");
- }
- /**
- * Our fake database. Here we populate an ArrayList with a dummy list of users.
- */
- // private List<DbUser> internalDatabase() {
- // // Dummy database
- //
- // // Create a dummy array list
- // List<DbUser> users = new ArrayList<DbUser>();
- // DbUser user = null;
- //
- // // Create a new dummy user
- // user = new DbUser();
- // user.setUsername("john");
- // // Actual password: admin
- // user.setPassword("21232f297a57a5a743894a0e4a801fc3");
- // // Admin user
- // user.setAccess(1);
- //
- // // Add to array list
- // users.add(user);
- //
- // // Create a new dummy user
- // user = new DbUser();
- // user.setUsername("jane");
- // // Actual password: user
- // user.setPassword("ee11cbb19052e40b07aac0ca060c23ee");
- // // Regular user
- // user.setAccess(2);
- //
- // // Add to array list
- // users.add(user);
- //
- // return users;
- // }
- }
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
- <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title><spring:message code="nomApp"/></title>
- <link href="<spring:url value="/static/css/estilos.css" htmlEscape="true" />" rel="stylesheet" type="text/css"/>
- <link rel="shortcut icon" href="<spring:url value="/static/images/Letter_D.ico" htmlEscape="true" />" type="image/x-icon"/>
- <!-- <link href="<spring:url value="/static/css/960.css" htmlEscape="true" />" rel="stylesheet" type="text/css" media="all" />-->
- <!-- <link href="<spring:url value="/static/css/reset.css" htmlEscape="true" />" rel="stylesheet" type="text/css" media="all" />-->
- <!-- <link href="<spring:url value="/static/css/text.css" htmlEscape="true" />" rel="stylesheet" type="text/css" media="all" />-->
- <!-- <link href="<spring:url value="/static/css/login.css" htmlEscape="true" />" rel="stylesheet" type="text/css" media="all" />-->
- <style type="text/css" media="screen">
- #slider {
- width: 535px; /* important to be same as image width */
- height: 356px; /* important to be same as image height */
- position: relative; /* important */
- overflow: hidden; /* important */
- }
- #sliderContent {
- width: 410px; /* important to be same as image width or wider */
- position: absolute;
- top: 0;
- margin-left: 0;
- }
- .sliderImage {
- float: left;
- position: relative;
- display: none;
- }
- .sliderImage span {
- position: absolute;
- font: 10px/15px Arial, Helvetica, sans-serif;
- padding: 10px 13px;
- width: 384px;
- background-color: #000;
- filter: alpha(opacity=70);
- -moz-opacity: 0.7;
- -khtml-opacity: 0.7;
- opacity: 0.7;
- color: #fff;
- display: none;
- }
- .clear {
- clear: both;
- }
- .sliderImage span strong {
- font-size: 14px;
- }
- ul { list-style-type: none;}
- </style>
- <!-- JavaScripts-->
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
- <script type="text/javascript" src="<spring:url value="/static/js/s3Slider.js" htmlEscape="true" />"></script>
- <script type="text/javascript">
- $(document).ready(function() {
- $('#slider').s3Slider({
- timeOut: 7000
- });
- });
- </script>
- </head>
- <body>
- <h1>Login</h1>
- <table width="100%" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td valign="top">
- <table align="left" width="572" height="390" border="0"
- cellspacing="0" cellpadding="0"
- style="background-repeat: no-repeat; background-image: url(<spring:url value="/static/images/fondo_login.jpg" htmlEscape="true" />)">
- <tr>
- <td colspan="2" class="titulo_login" align="left">
- DELPHOS 3.0
- </td>
- </tr>
- <tr>
- <td width="200" class="texto_login_error">
- ${error}
- </td>
- <td width="372" rowspan="3" valign="top">
- <table width="340" height="200" border="0" cellspacing="0"
- cellpadding="0"
- style="background-repeat: no-repeat; background-image: url(<spring:url value="/static/images/fondo_login_chico.jpg" htmlEscape="true" />) ">
- <tr>
- <td colspan="2" valign="top">
- <form name="formLogin" id="formLogin" action="../j_spring_security_check" method="post">
- <table width="100%" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td colspan="2" class="texto_login_azul">
- <spring:message code="textLogin" /></td>
- </tr>
- <tr>
- <td width="41%" class="cabecera_link" style="padding-left: 15px">
- <spring:message code="username" />:
- </td>
- <td width="59%" height="40">
- <input id="j_username" name="j_username" type="text" maxlength="30" class="inputText"/></td>
- </tr>
- <tr>
- <td class="cabecera_link" style="padding-left: 15px">
- <spring:message code="password" />:
- </td>
- <td height="40"><input id="j_password" name="j_password"
- type="password" maxlength="30" class="inputText"/></td>
- </tr>
- <!-- <tr>-->
- <!-- <td colspan="2" class="texto_login_azul">-->
- <!-- Don't ask for my password for two weeks <input type="checkbox" name="_spring_security_remember_me"></td>-->
- <!-- </tr>-->
- <tr>
- <td> </td>
- <td height="50"><input type="image"
- src="<spring:url value="/static/images/boton_login.jpg" htmlEscape="true" />"
- width="97" height="28" onclick="document['formLogin'].submit()" />
- </td>
- </tr>
- </table>
- </form>
- </td>
- </tr>
- </table>
- </td>
- </tr>
- <tr>
- <td class="texto_login" align="left"></td>
- </tr>
- <tr>
- <td align="center"><img
- src="<spring:url value="/static/images/j_login_lock.jpg" htmlEscape="true" />"
- width="152" height="137" /></td>
- </tr>
- <tr>
- <td colspan="2" align="center" class="texto_login"><spring:message
- code="powered" /></td>
- </tr>
- <tr>
- <td colspan="2" class="texto_login_azul2"> Navegador recomendado:
- <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"/>
- <img src="<spring:url value="/static/images/Chrome-32.png"/>" alt="Chrome" style="margin-right: 10px;margin-bottom: -5px" height="22px" width="22px"/>
- <img src="<spring:url value="/static/images/Opera-32.png"/>" alt="Opera" style="margin-right: 10px;margin-bottom: -5px" height="22px" width="22px"/>
- </td>
- </tr>
- </table>
- </td>
- <td style="border-left: 4px solid #0072e7; padding: 35px;"></td>
- <td valign="top">
- <table align="right" cellspacing="0" cellpadding="0" width="100%" border="0">
- <tr>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <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>
- </tr>
- <tr>
- <td height="250"><img src="<spring:url value="/static/images/login/texto_chrome.jpg"/>" alt="Opera" style="margin-right: 10px;margin-bottom: -5px" /></td>
- <td rowspan="2">
- <div id="slider">
- <ul id="sliderContent">
- <li class="sliderImage">
- <img src="<spring:url value="/static/images/login/considerar1.jpg"/>" alt="1" />
- <span style="background-color: white;"></span>
- </li>
- <li class="sliderImage">
- <img src="<spring:url value="/static/images/login/considerar2.jpg"/>" alt="1" />
- <span style="background-color: white;"></span>
- </li>
- <div class="clear sliderImage"></div>
- </ul>
- </div>
- </td>
- </tr>
- <tr>
- <td height="250"><img src="<spring:url value="/static/images/login/texto_mozilla.jpg"/>" alt="Opera" style="margin-right: 10px;margin-bottom: -5px" /></td>
- </tr>
- <tr>
- <td><img src="<spring:url value="/static/images/login/texto_dash.jpg"/>" alt="Opera" style="margin-right: 10px;margin-bottom: -5px" /></td>
- <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>
- </tr>
- </table>
- </td>
- </tr>
- </table>
- </body>
- </html>
Add Comment
Please, Sign In to add comment