Skip to content

Commit 208acbc

Browse files
committed
second part of authentication developed
1 parent dd34d6b commit 208acbc

4 files changed

Lines changed: 46 additions & 1 deletion

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.apiRest.repository;
2+
3+
import com.example.apiRest.model.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
import java.util.Optional;
7+
8+
public interface UserRepository extends JpaRepository<User, Long> {
9+
Optional<User> findByEmail(String email);
10+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.apiRest.security;
2+
3+
import com.example.apiRest.model.User;
4+
import com.example.apiRest.repository.UserRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.security.core.userdetails.UserDetails;
7+
import org.springframework.security.core.userdetails.UserDetailsService;
8+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
9+
import org.springframework.stereotype.Service;
10+
11+
import java.util.Optional;
12+
13+
@Service
14+
public class AutenticationService implements UserDetailsService {
15+
16+
@Autowired
17+
private UserRepository userRepository;
18+
19+
@Override
20+
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
21+
Optional<User> user = userRepository.findByEmail(username);
22+
if (user.isPresent()) {
23+
return user.get();
24+
}
25+
throw new UsernameNotFoundException("Invalid data");
26+
}
27+
}

src/main/java/com/example/apiRest/security/SecurityConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
package com.example.apiRest.security;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
34
import org.springframework.context.annotation.Configuration;
45
import org.springframework.http.HttpMethod;
56
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
67
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
78
import org.springframework.security.config.annotation.web.builders.WebSecurity;
89
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
910
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
11+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1012

1113
@EnableWebSecurity
1214
@Configuration
1315
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
1416

17+
@Autowired
18+
private AutenticationService autenticationService;
19+
1520
//configuracoes de autenticacao
1621
@Override
1722
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
18-
23+
auth.userDetailsService(autenticationService).passwordEncoder(new BCryptPasswordEncoder());
1924
}
2025

2126
//configuracoes de autorizacao
@@ -33,4 +38,5 @@ protected void configure(HttpSecurity http) throws Exception {
3338
public void configure(WebSecurity web) throws Exception {
3439

3540
}
41+
3642
}

src/main/resources/data.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ INSERT INTO TB_FILMS(name, genre, release_year) VALUES('forrest gump', 'drama',
33
INSERT INTO TB_FILMS(name, genre, release_year) VALUES('enter the dragon', 'adventure', 1973);
44
INSERT INTO TB_FILMS(name, genre, release_year) VALUES('enter the dragon 2', 'adventure', 1974);
55
INSERT INTO TB_FILMS(name, genre, release_year) VALUES('enter the dragon 3', 'adventure', 1977);
6+
7+
INSERT INTO USER(name, email, password) VALUES('Admin', '[email protected]', '$2a$10$XAzYDwFxPJNAshMotBsHU.tyAYICXeGZWfqkG6hu.pSc7RlLVQA1S');

0 commit comments

Comments
 (0)