This commit is contained in:
parent
4c79d502e3
commit
9eea30b23e
@ -15,7 +15,7 @@ import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
import wtf.beatrice.releasehive.models.User;
|
||||
import wtf.beatrice.releasehive.services.JWTService;
|
||||
import wtf.beatrice.releasehive.services.UserDetailsExtendedService;
|
||||
import wtf.beatrice.releasehive.services.UserService;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@ -25,14 +25,14 @@ public class JWTAuthenticationFilter extends OncePerRequestFilter
|
||||
private final HandlerExceptionResolver handlerExceptionResolver;
|
||||
|
||||
private final JWTService jwtService;
|
||||
private final UserDetailsExtendedService userDetailsService;
|
||||
private final UserService userService;
|
||||
|
||||
public JWTAuthenticationFilter(
|
||||
@Autowired JWTService jwtService,
|
||||
@Autowired UserDetailsExtendedService userDetailsService,
|
||||
@Autowired UserService userService,
|
||||
@Autowired HandlerExceptionResolver handlerExceptionResolver) {
|
||||
this.jwtService = jwtService;
|
||||
this.userDetailsService = userDetailsService;
|
||||
this.userService = userService;
|
||||
this.handlerExceptionResolver = handlerExceptionResolver;
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ public class JWTAuthenticationFilter extends OncePerRequestFilter
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (email != null && authentication == null) {
|
||||
User userDetails = this.userDetailsService.loadUserByEmail(email);
|
||||
User userDetails = userService.loadUserByEmail(email);
|
||||
|
||||
if (jwtService.isTokenValid(jwt, userDetails)) {
|
||||
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
|
||||
|
@ -43,9 +43,7 @@ public class AccountServiceImpl implements AccountService {
|
||||
authenticationManager.authenticate(
|
||||
new UsernamePasswordAuthenticationToken(
|
||||
user.getEmail(),
|
||||
user.getPassword()
|
||||
)
|
||||
);
|
||||
user.getPassword()));
|
||||
|
||||
return userRepository.findByEmail(user.getEmail())
|
||||
.orElseThrow();
|
||||
|
@ -1,25 +0,0 @@
|
||||
package wtf.beatrice.releasehive.services;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import wtf.beatrice.releasehive.models.User;
|
||||
import wtf.beatrice.releasehive.repositories.UserRepository;
|
||||
|
||||
@Service
|
||||
public class UserDetailsExtendedService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public UserDetailsExtendedService(@Autowired UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public User loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
return userRepository.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException(username));
|
||||
}
|
||||
|
||||
public User loadUserByEmail(String email) throws UsernameNotFoundException {
|
||||
return userRepository.findByEmail(email).orElseThrow(() -> new UsernameNotFoundException(email));
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package wtf.beatrice.releasehive.services;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import wtf.beatrice.releasehive.models.User;
|
||||
import wtf.beatrice.releasehive.repositories.UserRepository;
|
||||
@ -20,4 +21,11 @@ public class UserService
|
||||
return userRepository.findAll();
|
||||
}
|
||||
|
||||
public User loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
return userRepository.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException(username));
|
||||
}
|
||||
|
||||
public User loadUserByEmail(String email) throws UsernameNotFoundException {
|
||||
return userRepository.findByEmail(email).orElseThrow(() -> new UsernameNotFoundException(email));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user