-
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.
- Loading branch information
1 parent
6bdda1b
commit 7f1f67e
Showing
10 changed files
with
176 additions
and
37 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
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 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
43 changes: 43 additions & 0 deletions
43
src/main/java/com/aisip/OnO/backend/Auth/AuthUserController.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,43 @@ | ||
package com.aisip.OnO.backend.Auth; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/user") | ||
public class AuthUserController { | ||
|
||
@Autowired | ||
private AuthService authService; | ||
|
||
@GetMapping("/info") | ||
public ResponseEntity<?> getUserInfo(Authentication authentication) { | ||
Long userId = (Long) authentication.getPrincipal(); | ||
UserEntity userEntity = authService.getUserById(userId); | ||
if (userEntity != null) { | ||
return ResponseEntity.ok(userEntity); | ||
} else { | ||
return ResponseEntity.status(404).body(new ErrorResponse("User not found")); | ||
} | ||
} | ||
|
||
public static class ErrorResponse { | ||
private String error; | ||
|
||
public ErrorResponse(String error) { | ||
this.error = error; | ||
} | ||
|
||
public String getError() { | ||
return error; | ||
} | ||
|
||
public void setError(String error) { | ||
this.error = error; | ||
} | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
src/main/java/com/aisip/OnO/backend/Auth/JwtTokenFilter.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,53 @@ | ||
package com.aisip.OnO.backend.Auth; | ||
|
||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.JWTVerifier; | ||
import com.auth0.jwt.algorithms.Algorithm; | ||
import com.auth0.jwt.exceptions.JWTVerificationException; | ||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
|
||
public class JwtTokenFilter extends OncePerRequestFilter { | ||
|
||
private final String secret; | ||
|
||
public JwtTokenFilter(String secret) { | ||
this.secret = secret; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
String authHeader = request.getHeader("Authorization"); | ||
if (authHeader != null && authHeader.startsWith("Bearer ")) { | ||
String token = authHeader.substring(7); | ||
try { | ||
Algorithm algorithm = Algorithm.HMAC512(secret); | ||
JWTVerifier verifier = JWT.require(algorithm).build(); | ||
DecodedJWT decodedJWT = verifier.verify(token); | ||
|
||
String username = decodedJWT.getSubject(); | ||
Long userId = decodedJWT.getClaim("userId").asLong(); | ||
|
||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userId, null, Collections.emptyList()); | ||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} catch (JWTVerificationException e) { | ||
// JWT Verification failed | ||
SecurityContextHolder.clearContext(); | ||
} | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
src/main/java/com/aisip/OnO/backend/Auth/SecurityConfig.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,39 @@ | ||
package com.aisip.OnO.backend.Auth; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfig { | ||
|
||
@Value("${spring.jwt.secret}") | ||
private String secret; | ||
|
||
@Bean | ||
public JwtTokenFilter jwtTokenFilter() { | ||
return new JwtTokenFilter(secret); | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
http.csrf().disable() | ||
.authorizeHttpRequests(authorizeRequests -> | ||
authorizeRequests | ||
.requestMatchers("/api/auth/**").permitAll() | ||
.anyRequest().authenticated() | ||
) | ||
.sessionManagement(sessionManagement -> | ||
sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
) | ||
.addFilterBefore(jwtTokenFilter(), UsernamePasswordAuthenticationFilter.class); | ||
|
||
return http.build(); | ||
} | ||
} |
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 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