Implement authenticated function
This commit is contained in:
parent
fbaee8aa4c
commit
21fe0db209
@ -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<JwtAuthResponse> authenticate(@RequestBody LoginDto loginDto) {
|
||||||
|
String token = authService.login(loginDto);
|
||||||
|
|
||||||
|
JwtAuthResponse jwtAuthResponse = new JwtAuthResponse(token);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(jwtAuthResponse);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.springdemo.service;
|
||||||
|
|
||||||
|
import com.example.springdemo.security.dto.LoginDto;
|
||||||
|
|
||||||
|
public interface AuthService {
|
||||||
|
String login(LoginDto loginDto);
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user