Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nb-configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ Any value defined here will override the pom.xml file value but is only applicab
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>1.7-web</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>gfv3ee6</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>
<org-netbeans-modules-maven-jaxws.rest_2e_config_2e_type>ide</org-netbeans-modules-maven-jaxws.rest_2e_config_2e_type>
<org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
</properties>
</project-shared-configuration>
23 changes: 22 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
<version>5.1.41</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.bitbucket.b_c</groupId>
<artifactId>jose4j</artifactId>
<version>0.6.3</version>
<type>jar</type>
</dependency>


<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
Expand All @@ -68,34 +70,52 @@
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.12.Final</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>5.0.3.Final</version>
</dependency>

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
<type>jar</type>
</dependency>

<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.0.1</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>

<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey-jaxrs_2.10</artifactId>
Expand All @@ -107,6 +127,7 @@
</exclusion>
</exclusions>
</dependency>

</dependencies>

<build>
Expand Down
52 changes: 52 additions & 0 deletions src/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

Copyright (c) 2008, 2016 Oracle and/or its affiliates. All rights reserved.

Oracle and Java are registered trademarks of Oracle and/or its affiliates.
Other names may be trademarks of their respective owners.

The contents of this file are subject to the terms of either the GNU
General Public License Version 2 only ("GPL") or the Common
Development and Distribution License("CDDL") (collectively, the
"License"). You may not use this file except in compliance with the
License. You can obtain a copy of the License at
http://www.netbeans.org/cddl-gplv2.html
or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
specific language governing permissions and limitations under the
License. When distributing the software, include this License Header
Notice in each file and include the License file at
nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
particular file as subject to the "Classpath" exception as provided
by Oracle in the GPL Version 2 section of the License file that
accompanied this code. If applicable, add the following below the
License Header, with the fields enclosed by brackets [] replaced by
your own identifying information:
"Portions Copyrighted [year] [name of copyright owner]"

If you wish your version of this file to be governed by only the CDDL
or only the GPL Version 2, indicate your decision by adding
"[Contributor] elects to include this software in this distribution
under the [CDDL or GPL Version 2] license." If you do not indicate a
single choice of license, a recipient has the option to distribute
your version of this file under either the CDDL, the GPL Version 2 or
to extend the choice of license to its licensees as provided above.
However, if you add GPL Version 2 code and therefore, elected the GPL
Version 2 license, then the option applies only if the new code is
made subject to such option by the copyright holder.

Contributor(s):
-->
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">valdr</property>
<property name="hibernate.connection.password">nomad123</property>
<mapping class="xyz.somch.model.User"/>
<mapping class="xyz.somch.model.Rol"/>
</session-factory>
</hibernate-configuration>
18 changes: 7 additions & 11 deletions src/main/java/xyz/somch/db/ConexionBD.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@
*/
package xyz.somch.db;


import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
*
* @author dark_
*/
public class ConexionBD {
public static Connection crearConexion() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
Connection conexion;
conexion = DriverManager.getConnection(ConstantesBD.dbUrl, ConstantesBD.dbUser, ConstantesBD.dbPwd);
return conexion;
public static SessionFactory crearConexion() {
SessionFactory factory = new Configuration().configure().buildSessionFactory();
return factory;
}

public static void destruirConexion(Connection conexion) throws SQLException {
conexion.close();
public static void destruirConexion(SessionFactory factory){
factory.close();
}
}
13 changes: 5 additions & 8 deletions src/main/java/xyz/somch/filtro/FiltroAutorizacion.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.security.DenyAll;
import javax.annotation.security.PermitAll;
Expand All @@ -25,7 +23,6 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import static javax.ws.rs.core.Response.Status.FOUND;
import static javax.ws.rs.core.Response.Status.OK;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.jose4j.lang.JoseException;
Expand Down Expand Up @@ -80,7 +77,7 @@ public void filter(ContainerRequestContext requestContext) throws IOException {
String id = getClaimsJwtToken(jwt);
User userLogin = usuario.findByID(id).get(0);
String password = userLogin.getPassword();
if (!usuario.getSesion(userLogin)) {
if (!userLogin.getSesion()) {
requestContext.abortWith(ConstructorResponse.createResponse(Response.Status.UNAUTHORIZED, ACCESS_NO_SESSION));
return;
}
Expand All @@ -102,10 +99,10 @@ public void filter(ContainerRequestContext requestContext) throws IOException {
usuario = new UserBD();
String id = (String) ex.getJwtContext().getJwtClaims().getClaimValue("id");
User user = usuario.findByID(id).get(0);
if (usuario.getSesion(usuario.findByID(id).get(0)) && ex.hasExpired()) {
if (user.getSesion() && ex.hasExpired()) {
try {
String jwt = ex.getJwtContext().getJwt();
if (usuario.getToken(user).equals(jwt)) {
if (user.getToken().equals(jwt)) {
jwt = TokenSecurity.refreshJwtToken(user);
user.setToken(jwt);
user.setRefreshToken((new SimpleDateFormat("HHmmssddMMyyyy")).format((new Date())));
Expand All @@ -128,9 +125,9 @@ public void filter(ContainerRequestContext requestContext) throws IOException {
}
}

private boolean isUserAllowed(final String userRole, final Set<String> rolesSet) {
private boolean isUserAllowed(final List userRole, final Set<String> rolesSet) {
boolean isAllowed = false;
if (rolesSet.contains(userRole)) {
if (rolesSet.contains(userRole.get(0))) {
isAllowed = true;
}
return isAllowed;
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/xyz/somch/hibernate/HibernateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package xyz.somch.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
*
* @author dark_
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static{
try{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (HibernateException he){
System.err.println("Ocurrió un error en la inicialización de la SessionFactory: " + he);
throw new ExceptionInInitializerError(he);
}
}

public static SessionFactory getSessionFactory()
{
return sessionFactory;
}

}
46 changes: 46 additions & 0 deletions src/main/java/xyz/somch/model/Rol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package xyz.somch.model;

import javax.persistence.*;

/**
*
* @author dark_
*/

@Entity
@Table(name = "ROL")
public class Rol {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ID_ROL")
private int id;
@Column(name = "NOMBRE")
private String nombre;

public Rol(){}
public Rol(String nombre){
this.id = 1;
this.nombre = "User";
}
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getNombre() {
return nombre;
}

public void setNombre(String nombre) {
this.nombre = nombre;
}

}
Loading