Spaces:
Configuration error
Configuration error
Download src/main/java/com/example/app/controllers/UserAuthController.java from variphx/java-learn: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/spaces/variphx/java-learn/resolve/2225c94eb81a278f6e7682c736ee781191326090/src/main/java/com/example/app/controllers/UserAuthController.java
- Command line
-
hf download hf://spaces/variphx/java-learn@2225c94eb81a278f6e7682c736ee781191326090/src/main/java/com/example/app/controllers/UserAuthController.java
-
curl -L -o UserAuthController.java https://huggingface.co/spaces/variphx/java-learn/resolve/2225c94eb81a278f6e7682c736ee781191326090/src/main/java/com/example/app/controllers/UserAuthController.java
1.61 kB
| package com.example.app.controllers; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.security.authentication.AuthenticationManager; | |
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | |
| import org.springframework.security.core.Authentication; | |
| import org.springframework.security.core.userdetails.UsernameNotFoundException; | |
| import org.springframework.web.bind.annotation.PostMapping; | |
| import org.springframework.web.bind.annotation.RequestBody; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.RestController; | |
| import com.example.app.dtos.JwtRequestDto; | |
| import com.example.app.mappers.JwtRequestMapper; | |
| import com.example.app.misc.JwtRequest; | |
| import com.example.app.services.JwtService; | |
| public class UserAuthController { | |
| private JwtService jwtService; | |
| private AuthenticationManager authenticationManager; | |
| JwtRequestMapper mapper; | |
| public String authenticateAndGetToken( JwtRequestDto dto) { | |
| JwtRequest jwtRequest = mapper.fromDto(dto); | |
| Authentication authentication = authenticationManager.authenticate( | |
| new UsernamePasswordAuthenticationToken(jwtRequest.getUsername(), jwtRequest.getPassword())); | |
| if (authentication.isAuthenticated()) { | |
| return jwtService.generateToken(jwtRequest.getUsername()); | |
| } | |
| throw new UsernameNotFoundException("Invalid user request"); | |
| } | |
| } | |