Maxence
Maxence
JCHJava Community | Help. Code. Learn.
Created by Maxence on 4/8/2025 in #java-help
Do not receive Headers from Front-end with JWT
Hi, I work on a website and I send a token with axios with the front-end
export default {
data() {
return {
produits: [] // Tableau pour stocker les produits
};
},
created() {
// Appel à l'API pour récupérer les produits
axios
.get("http://localhost:8080/api/produits", {
headers: {
"Authorization": `Bearer ${localStorage.getItem("token")}`
}
}) // URL du back-end
.then((response) => {
this.produits = response.data; // On stocke les produits dans la variable produits
console.log("Produits après assignation : ", this.produits);
})
.catch((error) => {
console.error("Il y a une erreur lors de la récupération des produits : ", error);
});
}
};
export default {
data() {
return {
produits: [] // Tableau pour stocker les produits
};
},
created() {
// Appel à l'API pour récupérer les produits
axios
.get("http://localhost:8080/api/produits", {
headers: {
"Authorization": `Bearer ${localStorage.getItem("token")}`
}
}) // URL du back-end
.then((response) => {
this.produits = response.data; // On stocke les produits dans la variable produits
console.log("Produits après assignation : ", this.produits);
})
.catch((error) => {
console.error("Il y a une erreur lors de la récupération des produits : ", error);
});
}
};
but my jwt filter don't see the headers Authorization, it's always null I don"t know why. The filter is used with SecurityConfig with addFilterBefore(...)
public class JwtFilter extends OncePerRequestFilter {

private final UserService userService;
private final JwtUtils jwtUtils;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
System.out.println("Executing JWT Filter");
System.out.println("request: " + request);

// Permet de récupérer le token via le header
final String authorizationHeader = request.getHeader("Authorization");
System.out.println("Authorization header: " + authorizationHeader);

String email = null;
String jwt = null;

//-----

}
public class JwtFilter extends OncePerRequestFilter {

private final UserService userService;
private final JwtUtils jwtUtils;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
System.out.println("Executing JWT Filter");
System.out.println("request: " + request);

// Permet de récupérer le token via le header
final String authorizationHeader = request.getHeader("Authorization");
System.out.println("Authorization header: " + authorizationHeader);

String email = null;
String jwt = null;

//-----

}
`
5 replies
JCHJava Community | Help. Code. Learn.
Created by Maxence on 3/27/2025 in #java-help
JWT & Spring Boot
Hello, I develop a website and I have some questions for the connexion with users. For the back-end, I use Spring Boot and I have this function :
@PostMapping("/login")
public ResponseEntity<?> loginUser(@RequestBody User user) {
User userFound = userRepository.findByEmail(user.getEmail());
if(userFound == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Email non trouvé");
}

if(!passwordEncoder.matches(user.getPassword(), userFound.getPassword())) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Mot de passe incorrect");
}

return ResponseEntity.ok("Login effectué");
}
@PostMapping("/login")
public ResponseEntity<?> loginUser(@RequestBody User user) {
User userFound = userRepository.findByEmail(user.getEmail());
if(userFound == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Email non trouvé");
}

if(!passwordEncoder.matches(user.getPassword(), userFound.getPassword())) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Mot de passe incorrect");
}

return ResponseEntity.ok("Login effectué");
}
I return this because I don't really know what to return. I would like use a JWT token and I search on the web and I don't understand what I have to do. Actually I would like a token and all the informations I need about the user (email, name etc...) , so what can I return and how to convert these datas in VueJS (I use Axios to recover the datas) ?
14 replies
JCHJava Community | Help. Code. Learn.
Created by Maxence on 3/25/2025 in #java-help
Spring boot
Hi, I am a beginner at Spring Boot and I tried to configure my API espcially for user connexion. I have a database which is connected and it works. But I try to create a SecurityConfig and I have this error : java: variable userService not initialized in the default constructor In the code (after this message), I use @RequiredArgsConstructor so it should work... I have a problem with the model User for the database because it says it doesnt find getters but I use @Data with Loombook and it works on another model. Can someone please try to explain me ?
39 replies