Skip to content

Commit

Permalink
Spring Security Autenticacion con JPA
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaiasMorochi committed Sep 11, 2019
1 parent 111fa98 commit 993093e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 32 deletions.
17 changes: 9 additions & 8 deletions src/main/java/com/config/SpringSecurityConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.config;

import com.auth.handler.LoginSuccesHandler;
import com.models.service.impl.JpaUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
Expand All @@ -19,27 +20,27 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private LoginSuccesHandler succesHandler;

@Autowired
private DataSource dataSource;

// @Autowired
// private JpaUserDetailsService userDetailsService;
// private DataSource dataSource;

@Autowired
private JpaUserDetailsService userDetailsService;

@Autowired
private BCryptPasswordEncoder passwordEncoder;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder build) throws Exception {

// build.userDetailsService(userDetailsService)
// .passwordEncoder(passwordEncoder);
build.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder);

build.jdbcAuthentication()
/* build.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(passwordEncoder)
.usersByUsernameQuery("select username, password, enabled from users where username=?")
.authoritiesByUsernameQuery("select u.username, a.authority from authorities a inner join users u on (a.user_id=u.id) where u.username=?");

*/

/*
// Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
@Service("jpaUserDetailsService")
public class JpaUserDetailsService implements UserDetailsService {

@Autowired
private IUsuarioDao usuarioDao;

private Logger logger = LoggerFactory.getLogger(JpaUserDetailsService.class);

@Autowired
private IUsuarioDao usuarioDao;

@Override
@Transactional(readOnly = true)
Expand All @@ -38,6 +37,7 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
}

List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

for (Role role: usuario.getRoles()){
logger.info("Role: ".concat(role.getAuthority()));
authorities.add(new SimpleGrantedAuthority(role.getAuthority()));
Expand Down
23 changes: 2 additions & 21 deletions src/main/resources/import.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,11 @@ INSERT INTO facturas_items (cantidad, factura_id, producto_id) VALUES(1, 1, 7);
INSERT INTO facturas (descripcion, observacion, cliente_id, create_at) VALUES('Factura Bicicleta', 'Alguna nota importante!', 1, NOW());
INSERT INTO facturas_items (cantidad, factura_id, producto_id) VALUES(3, 2, 6);



-- create table users (
-- id bigint not null,
-- username varchar(45) not null,
-- password varchar(60) not null,
-- enabled tinyint not null default 1,
-- unique key (username),
-- primary key (id)
-- );
--
-- create table authorities (
-- id bigint not null ,
-- user_id bigint not null,
-- authority varchar(45) not null,
-- unique key unique_user_id_authority (user_id, authority),
-- primary key (id)
-- );

-- alter table authorities add constraint fk_authorities_user foreign key (user_id) references users (id);

/* Creamos los usuarios */
INSERT INTO users (username, password, enabled) VALUES('isaias', '$2a$10$O9wxmH/AeyZZzIS09Wp8YOEMvFnbRVJ8B4dmAMVSGloR62lj.yqXG', 1);
INSERT INTO users (username, password, enabled) VALUES('admin', '$2a$10$DOMDxjYyfZ/e7RcBfUpzqeaCs8pLgcizuiQWXPkU35nOhZlFcE9MS', 1);

/* Creamos authorities */
INSERT INTO authorities (user_id, authority) VALUES(1, 'ROLE_USER');
INSERT INTO authorities (user_id, authority) VALUES(2, 'ROLE_USER');
INSERT INTO authorities (user_id, authority) VALUES(2, 'ROLE_ADMIN');
Expand Down

0 comments on commit 993093e

Please sign in to comment.