parent
20b068d6bf
commit
fa1a716e6a
@ -1,18 +0,0 @@
|
|||||||
package com.example.wfwebapi.assembler;
|
|
||||||
|
|
||||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
|
|
||||||
import com.example.wfwebapi.controller.QuoteController;
|
|
||||||
import com.example.wfwebapi.model.Quote;
|
|
||||||
import org.springframework.hateoas.EntityModel;
|
|
||||||
import org.springframework.hateoas.server.RepresentationModelAssembler;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class QuoteModelAssembler implements RepresentationModelAssembler<Quote, EntityModel<Quote>> {
|
|
||||||
@Override
|
|
||||||
public EntityModel<Quote> toModel(Quote quote) {
|
|
||||||
return EntityModel.of(quote,
|
|
||||||
linkTo(methodOn(QuoteController.class).getQuoteById(quote.getId())).withSelfRel(),
|
|
||||||
linkTo(methodOn(QuoteController.class).getAllQuotes(0, 10)).withRel("quotes"));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
package com.example.wfwebapi.assembler;
|
|
||||||
|
|
||||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
|
|
||||||
import com.example.wfwebapi.controller.UserController;
|
|
||||||
import com.example.wfwebapi.model.User;
|
|
||||||
import org.springframework.hateoas.EntityModel;
|
|
||||||
import org.springframework.hateoas.server.RepresentationModelAssembler;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class UserModelAssembler implements RepresentationModelAssembler<User, EntityModel<User>> {
|
|
||||||
@Override
|
|
||||||
public EntityModel<User> toModel(User user) {
|
|
||||||
return EntityModel.of(user,
|
|
||||||
linkTo(methodOn(UserController.class).getUserById(user.getId())).withSelfRel(),
|
|
||||||
linkTo(methodOn(UserController.class).getAllUsers(0, 5)).withRel("users"));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
package com.example.wfwebapi.config;
|
|
||||||
|
|
||||||
import com.example.wfwebapi.security.JwtAuthenticationFilter;
|
|
||||||
import com.example.wfwebapi.security.JwtTokenProvider;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.security.config.Customizer;
|
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
|
||||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class SecurityConfig {
|
|
||||||
|
|
||||||
private final JwtTokenProvider tokenProvider;
|
|
||||||
|
|
||||||
public SecurityConfig(JwtTokenProvider tokenProvider) {
|
|
||||||
this.tokenProvider = tokenProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
|
||||||
http.csrf(csrf -> csrf.disable())
|
|
||||||
.authorizeHttpRequests(authz -> authz
|
|
||||||
.requestMatchers("/api/v1/auth/**").permitAll() // par exemple, pour la connexion
|
|
||||||
.anyRequest().authenticated()
|
|
||||||
)
|
|
||||||
.addFilterBefore(new JwtAuthenticationFilter(tokenProvider), UsernamePasswordAuthenticationFilter.class)
|
|
||||||
.httpBasic(Customizer.withDefaults());
|
|
||||||
|
|
||||||
return http.build();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.wfwebapi.exception;
|
|
||||||
|
|
||||||
public class ResourceNotFoundException extends RuntimeException {
|
|
||||||
public ResourceNotFoundException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.wfwebapi.exception;
|
|
||||||
|
|
||||||
public class UnauthorizedException extends RuntimeException {
|
|
||||||
public UnauthorizedException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.Admin;
|
|
||||||
|
|
||||||
public interface AdminRepository extends JpaRepository<Admin, Long> {
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.Caracter;
|
|
||||||
|
|
||||||
public interface CaracterRepository extends JpaRepository<Caracter, Long> {
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.Commentary;
|
|
||||||
|
|
||||||
public interface CommentaryRepository extends JpaRepository<Commentary, Long> {
|
|
||||||
List<Commentary> findByQuote_Id(Long quoteId);
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.DailyQuote;
|
|
||||||
|
|
||||||
public interface DailyQuoteRepository extends JpaRepository<DailyQuote, Long> {
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.Favorite;
|
|
||||||
import com.example.wfwebapi.model.FavoriteId;
|
|
||||||
|
|
||||||
public interface FavoriteRepository extends JpaRepository<Favorite, FavoriteId> {
|
|
||||||
List<Favorite> findByUser_Id(Long userId);
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.Image;
|
|
||||||
|
|
||||||
public interface ImageRepository extends JpaRepository<Image, Long> {
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.Question;
|
|
||||||
|
|
||||||
public interface QuestionRepository extends JpaRepository<Question, Long> {
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.QuizQuestion;
|
|
||||||
import com.example.wfwebapi.model.QuizQuestionId;
|
|
||||||
|
|
||||||
public interface QuizQuestionRepository extends JpaRepository<QuizQuestion, QuizQuestionId> {
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.Quote;
|
|
||||||
|
|
||||||
public interface QuoteRepository extends JpaRepository<Quote, Long> {
|
|
||||||
}
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.RecordQuiz;
|
|
||||||
import com.example.wfwebapi.model.RecordQuizId;
|
|
||||||
|
|
||||||
public interface RecordQuizRepository extends JpaRepository<RecordQuiz, RecordQuizId> {
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.Source;
|
|
||||||
|
|
||||||
public interface SourceRepository extends JpaRepository<Source, Long> {
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package com.example.wfwebapi.repository;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import com.example.wfwebapi.model.User;
|
|
||||||
|
|
||||||
public interface UserRepository extends JpaRepository<User, Long> {
|
|
||||||
Optional<User> findByUsername(String username);
|
|
||||||
Optional<User> findByEmail(String email);
|
|
||||||
boolean existsByUsername(String username);
|
|
||||||
boolean existsByEmail(String email);
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package com.example.wfwebapi.security;
|
|
||||||
|
|
||||||
import com.example.wfwebapi.exception.UnauthorizedException;
|
|
||||||
import jakarta.servlet.FilterChain;
|
|
||||||
import jakarta.servlet.ServletException;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
|
||||||
import java.io.IOException;
|
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
|
||||||
|
|
||||||
public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|
||||||
|
|
||||||
private final JwtTokenProvider tokenProvider;
|
|
||||||
|
|
||||||
public JwtAuthenticationFilter(JwtTokenProvider tokenProvider) {
|
|
||||||
this.tokenProvider = tokenProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
|
||||||
throws ServletException, IOException {
|
|
||||||
|
|
||||||
String header = request.getHeader("Authorization");
|
|
||||||
if (header == null || !header.startsWith("Bearer ")) {
|
|
||||||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Token manquant ou invalide");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String token = header.substring(7);
|
|
||||||
if (!tokenProvider.validateToken(token)) {
|
|
||||||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Token invalide ou expiré");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
filterChain.doFilter(request, response);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
package com.example.wfwebapi.security;
|
|
||||||
|
|
||||||
import io.jsonwebtoken.*;
|
|
||||||
import io.jsonwebtoken.security.Keys;
|
|
||||||
import java.security.Key;
|
|
||||||
import java.util.Date;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class JwtTokenProvider {
|
|
||||||
|
|
||||||
// Clé secrète (à stocker en sécurité dans une variable d'environnement en production)
|
|
||||||
private final Key secretKey = Keys.hmacShaKeyFor("MaCléSuperSecrètePourJWTQueJeDoisChanger".getBytes());
|
|
||||||
// Durée de validité du token (par exemple 1h)
|
|
||||||
private final long validityInMilliseconds = 3600000;
|
|
||||||
|
|
||||||
// Création du token
|
|
||||||
public String createToken(String username) {
|
|
||||||
Claims claims = Jwts.claims().setSubject(username);
|
|
||||||
Date now = new Date();
|
|
||||||
Date validity = new Date(now.getTime() + validityInMilliseconds);
|
|
||||||
|
|
||||||
return Jwts.builder()
|
|
||||||
.setClaims(claims)
|
|
||||||
.setIssuedAt(now)
|
|
||||||
.setExpiration(validity)
|
|
||||||
.signWith(secretKey, SignatureAlgorithm.HS256)
|
|
||||||
.compact();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validation du token
|
|
||||||
public boolean validateToken(String token) {
|
|
||||||
try {
|
|
||||||
Jws<Claims> claims = Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token);
|
|
||||||
return !claims.getBody().getExpiration().before(new Date());
|
|
||||||
} catch (JwtException | IllegalArgumentException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Récupération du nom d'utilisateur depuis le token
|
|
||||||
public String getUsername(String token) {
|
|
||||||
return Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token).getBody().getSubject();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue