Security branch #38
@@ -62,7 +62,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||||||
response.setContentType("application/json");
|
response.setContentType("application/json");
|
||||||
|
|
||||||
ErrorResponse errorResponse = new ErrorResponse("Your session has expired. Refresh your token.");
|
ErrorResponse errorResponse = new ErrorResponse("Your session has expired. Refresh your token.");
|
||||||
ObjectMapper objectMapper = new ObjectMapper(); // You may need to import ObjectMapper
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
response.getWriter().write(objectMapper.writeValueAsString(errorResponse));
|
response.getWriter().write(objectMapper.writeValueAsString(errorResponse));
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ public class JwtService {
|
|||||||
private long jwtExpiration;
|
private long jwtExpiration;
|
||||||
@Value("${application.security.jwt.refresh-token.expiration}")
|
@Value("${application.security.jwt.refresh-token.expiration}")
|
||||||
private long refreshExpiration;
|
private long refreshExpiration;
|
||||||
// private final AuthenticationService authenticationService;
|
|
||||||
|
|
||||||
|
|
||||||
public String extractUsername(String token) {
|
public String extractUsername(String token) {
|
||||||
|
|||||||
@@ -45,22 +45,9 @@ public class SecurityConfiguration {
|
|||||||
.cors(Customizer.withDefaults())
|
.cors(Customizer.withDefaults())
|
||||||
.csrf(AbstractHttpConfigurer::disable)
|
.csrf(AbstractHttpConfigurer::disable)
|
||||||
.authorizeHttpRequests(auth -> auth
|
.authorizeHttpRequests(auth -> auth
|
||||||
// .requestMatchers("/api/v1/auth/**").permitAll()
|
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
)
|
)
|
||||||
.oauth2Login(withDefaults());
|
.oauth2Login(withDefaults());
|
||||||
// .exceptionHandling(exceptionHandling ->
|
|
||||||
// exceptionHandling
|
|
||||||
// .authenticationEntryPoint(authenticationEntryPoint())
|
|
||||||
// )
|
|
||||||
// .oauth2Login(oauth2Login ->
|
|
||||||
// oauth2Login
|
|
||||||
// .loginPage("/login")
|
|
||||||
// .clientRegistrationRepository(clientRegistrationRepository)
|
|
||||||
// .userInfoEndpoint(userInfoEndpoint ->
|
|
||||||
// userInfoEndpoint.userService(oAuth2UserService())
|
|
||||||
// )
|
|
||||||
// .successHandler(jwtAuthenticationSuccessHandler()));
|
|
||||||
|
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,16 +71,15 @@ public class AuthenticationService {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract user details from OAuth2User
|
|
||||||
String givenName = oAuth2User.getAttribute("given_name");
|
String givenName = oAuth2User.getAttribute("given_name");
|
||||||
String familyName = oAuth2User.getAttribute("family_name");
|
String familyName = oAuth2User.getAttribute("family_name");
|
||||||
String email = oAuth2User.getAttribute("email");
|
String email = oAuth2User.getAttribute("email");
|
||||||
|
|
||||||
// Create a new User entity and save it to the database
|
|
||||||
User user = User.builder()
|
User user = User.builder()
|
||||||
.firstName(givenName)
|
.firstName(givenName)
|
||||||
.lastName(familyName)
|
.lastName(familyName)
|
||||||
.username(email) // You can adjust the username as needed
|
.username(email)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
String randomPassword = passwordGenerator.generateRandomPassword(8);
|
String randomPassword = passwordGenerator.generateRandomPassword(8);
|
||||||
@@ -98,7 +97,6 @@ public class AuthenticationService {
|
|||||||
System.out.println("New user: " + user);
|
System.out.println("New user: " + user);
|
||||||
System.out.println("New credentials: " + credential);
|
System.out.println("New credentials: " + credential);
|
||||||
|
|
||||||
// Return the registered user's authentication response
|
|
||||||
return AuthenticationResponse.builder()
|
return AuthenticationResponse.builder()
|
||||||
.accessToken(jwtToken)
|
.accessToken(jwtToken)
|
||||||
.refreshToken(refreshToken)
|
.refreshToken(refreshToken)
|
||||||
|
|||||||
Reference in New Issue
Block a user