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 org.springframework.web.servlet.HandlerExceptionResolver;
|
||||||
import wtf.beatrice.releasehive.models.User;
|
import wtf.beatrice.releasehive.models.User;
|
||||||
import wtf.beatrice.releasehive.services.JWTService;
|
import wtf.beatrice.releasehive.services.JWTService;
|
||||||
import wtf.beatrice.releasehive.services.UserDetailsExtendedService;
|
import wtf.beatrice.releasehive.services.UserService;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@ -25,14 +25,14 @@ public class JWTAuthenticationFilter extends OncePerRequestFilter
|
|||||||
private final HandlerExceptionResolver handlerExceptionResolver;
|
private final HandlerExceptionResolver handlerExceptionResolver;
|
||||||
|
|
||||||
private final JWTService jwtService;
|
private final JWTService jwtService;
|
||||||
private final UserDetailsExtendedService userDetailsService;
|
private final UserService userService;
|
||||||
|
|
||||||
public JWTAuthenticationFilter(
|
public JWTAuthenticationFilter(
|
||||||
@Autowired JWTService jwtService,
|
@Autowired JWTService jwtService,
|
||||||
@Autowired UserDetailsExtendedService userDetailsService,
|
@Autowired UserService userService,
|
||||||
@Autowired HandlerExceptionResolver handlerExceptionResolver) {
|
@Autowired HandlerExceptionResolver handlerExceptionResolver) {
|
||||||
this.jwtService = jwtService;
|
this.jwtService = jwtService;
|
||||||
this.userDetailsService = userDetailsService;
|
this.userService = userService;
|
||||||
this.handlerExceptionResolver = handlerExceptionResolver;
|
this.handlerExceptionResolver = handlerExceptionResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ public class JWTAuthenticationFilter extends OncePerRequestFilter
|
|||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
if (email != null && authentication == null) {
|
if (email != null && authentication == null) {
|
||||||
User userDetails = this.userDetailsService.loadUserByEmail(email);
|
User userDetails = userService.loadUserByEmail(email);
|
||||||
|
|
||||||
if (jwtService.isTokenValid(jwt, userDetails)) {
|
if (jwtService.isTokenValid(jwt, userDetails)) {
|
||||||
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
|
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
|
||||||
|
@ -43,9 +43,7 @@ public class AccountServiceImpl implements AccountService {
|
|||||||
authenticationManager.authenticate(
|
authenticationManager.authenticate(
|
||||||
new UsernamePasswordAuthenticationToken(
|
new UsernamePasswordAuthenticationToken(
|
||||||
user.getEmail(),
|
user.getEmail(),
|
||||||
user.getPassword()
|
user.getPassword()));
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
return userRepository.findByEmail(user.getEmail())
|
return userRepository.findByEmail(user.getEmail())
|
||||||
.orElseThrow();
|
.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;
|
package wtf.beatrice.releasehive.services;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import wtf.beatrice.releasehive.models.User;
|
import wtf.beatrice.releasehive.models.User;
|
||||||
import wtf.beatrice.releasehive.repositories.UserRepository;
|
import wtf.beatrice.releasehive.repositories.UserRepository;
|
||||||
@ -20,4 +21,11 @@ public class UserService
|
|||||||
return userRepository.findAll();
|
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