-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Informatik-Projekt-Kurs/dev
Full User System
- Loading branch information
Showing
19 changed files
with
704 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version = 1 | ||
|
||
[[analyzers]] | ||
name = "java" | ||
|
||
[analyzers.meta] | ||
runtime_version = "19" | ||
|
||
[[transformers]] | ||
name = "google-java-format" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.MeetMate; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
// @EnableConfigurationProperties | ||
// @EntityScan(basePackages = {"com.MeetMate.user"}) //force scan the packages | ||
public class MeetMateApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(MeetMateApplication.class, args); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/java/com/MeetMate/experiments/Experimentational.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.MeetMate.experiments; | ||
|
||
public @interface Experimentational {} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/MeetMate/response/AuthenticationResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.MeetMate.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@Builder | ||
public class AuthenticationResponse { | ||
|
||
String access_Token; | ||
long expires_at; | ||
String refresh_token; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.MeetMate.response; | ||
|
||
import com.MeetMate.roles.Role; | ||
import java.time.LocalDate; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@Builder | ||
public class GetResponse { | ||
|
||
long id; | ||
String name; | ||
LocalDate created_at; | ||
String email; | ||
Role role; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.MeetMate.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@Builder | ||
public class RefreshResponse { | ||
|
||
String access_Token; | ||
long expires_at; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.MeetMate.roles; | ||
|
||
public enum Role { | ||
ADMIN, | ||
COMPANY, | ||
CLIENT; | ||
} |
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
src/main/java/com/MeetMate/security/ApplicationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.MeetMate.security; | ||
|
||
import com.MeetMate.user.UserRepository; | ||
import jakarta.persistence.EntityNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider; | ||
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class ApplicationConfig { | ||
|
||
private final UserRepository userRepository; | ||
|
||
@Bean | ||
public UserDetailsService userDetailsService() { | ||
return username -> | ||
userRepository | ||
.findUserByEmail(username) | ||
.orElseThrow(() -> new EntityNotFoundException("User not found")); | ||
} | ||
|
||
@Bean | ||
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) | ||
throws Exception { | ||
return configuration.getAuthenticationManager(); | ||
} | ||
|
||
@Bean | ||
public AuthenticationProvider authenticationProvider() { | ||
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); | ||
daoAuthenticationProvider.setUserDetailsService(userDetailsService()); | ||
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder()); | ||
return daoAuthenticationProvider; | ||
} | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/MeetMate/security/JwtAuthenticationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.MeetMate.security; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
|
||
private final JwtService jwtService; | ||
private final UserDetailsService userDetailsService; | ||
|
||
@Override | ||
protected void doFilterInternal( | ||
@NotNull HttpServletRequest request, | ||
@NotNull HttpServletResponse response, | ||
@NotNull FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
final String authHeader = request.getHeader("Authorization"); | ||
final String jwt; | ||
final String userEmail; | ||
if (authHeader == null || !authHeader.startsWith("Bearer ")) { | ||
filterChain.doFilter(request, response); | ||
return; | ||
} | ||
// extract jwt token | ||
// beginIndex is 7 bc "Bearer " is 7 | ||
jwt = authHeader.substring(7); | ||
userEmail = jwtService.extractUserEmail(jwt); | ||
|
||
if (userEmail != null | ||
&& SecurityContextHolder.getContext().getAuthentication() | ||
== null) { // check f if user is already authenticated | ||
UserDetails userDetails = userDetailsService.loadUserByUsername(userEmail); | ||
|
||
if (jwtService.isTokenValid(jwt, userDetails)) { | ||
UsernamePasswordAuthenticationToken authToken = | ||
new UsernamePasswordAuthenticationToken( | ||
userDetails, null, userDetails.getAuthorities()); | ||
|
||
authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); | ||
SecurityContextHolder.getContext().setAuthentication(authToken); | ||
} | ||
} | ||
filterChain.doFilter(request, response); | ||
} | ||
} |
Oops, something went wrong.