diff --git a/src/main/java/com/example/springdemo/controller/AuthController.java b/src/main/java/com/example/springdemo/controller/AuthController.java new file mode 100644 index 0000000..01f557b --- /dev/null +++ b/src/main/java/com/example/springdemo/controller/AuthController.java @@ -0,0 +1,28 @@ +package com.example.springdemo.controller; + +import com.example.springdemo.security.dto.JwtAuthResponse; +import com.example.springdemo.security.dto.LoginDto; +import com.example.springdemo.service.AuthService; +import jakarta.annotation.Resource; +import org.springframework.http.ResponseEntity; +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; + +@RestController +@RequestMapping("/auth") +public class AuthController { + @Resource + private AuthService authService; + + // Login REST API + @PostMapping("/login") + public ResponseEntity authenticate(@RequestBody LoginDto loginDto) { + String token = authService.login(loginDto); + + JwtAuthResponse jwtAuthResponse = new JwtAuthResponse(token); + + return ResponseEntity.ok(jwtAuthResponse); + } +} diff --git a/src/main/java/com/example/springdemo/service/AuthService.java b/src/main/java/com/example/springdemo/service/AuthService.java new file mode 100644 index 0000000..bd785b3 --- /dev/null +++ b/src/main/java/com/example/springdemo/service/AuthService.java @@ -0,0 +1,7 @@ +package com.example.springdemo.service; + +import com.example.springdemo.security.dto.LoginDto; + +public interface AuthService { + String login(LoginDto loginDto); +} diff --git a/src/main/java/com/example/springdemo/serviceImpl/AuthServiceImpl.java b/src/main/java/com/example/springdemo/serviceImpl/AuthServiceImpl.java new file mode 100644 index 0000000..e04ded9 --- /dev/null +++ b/src/main/java/com/example/springdemo/serviceImpl/AuthServiceImpl.java @@ -0,0 +1,29 @@ +package com.example.springdemo.serviceImpl; + +import com.example.springdemo.security.dto.LoginDto; +import com.example.springdemo.security.utils.JwtTokenProvider; +import com.example.springdemo.service.AuthService; +import jakarta.annotation.Resource; +import org.jetbrains.annotations.NotNull; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; + +@Service +public class AuthServiceImpl implements AuthService { + @Resource + private AuthenticationManager authenticationManager; + @Resource + private JwtTokenProvider jwtTokenProvider; + + @Override + public String login(@NotNull LoginDto loginDto) { + Authentication authentication = authenticationManager.authenticate( + new UsernamePasswordAuthenticationToken(loginDto.getUsername(), loginDto.getPassword()) + ); + SecurityContextHolder.getContext().setAuthentication(authentication); + return jwtTokenProvider.generateToken(authentication); + } +}