获取当前登录用户信息

This commit is contained in:
myh 2023-12-11 00:01:40 +08:00
parent 85709591e1
commit 7c6116f2c5
3 changed files with 52 additions and 1 deletions

View File

@ -3,8 +3,15 @@ package com.example.springdemo.controller;
import com.example.springdemo.security.dto.JwtAuthResponse; import com.example.springdemo.security.dto.JwtAuthResponse;
import com.example.springdemo.security.dto.LoginDto; import com.example.springdemo.security.dto.LoginDto;
import com.example.springdemo.service.AuthService; import com.example.springdemo.service.AuthService;
import com.example.springdemo.utils.Result;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -16,6 +23,9 @@ public class AuthController {
@Resource @Resource
private AuthService authService; private AuthService authService;
@Resource
private UserDetailsService userDetailsService;
// Login REST API // Login REST API
@PostMapping("/login") @PostMapping("/login")
public ResponseEntity<JwtAuthResponse> authenticate(@RequestBody LoginDto loginDto) { public ResponseEntity<JwtAuthResponse> authenticate(@RequestBody LoginDto loginDto) {
@ -25,4 +35,37 @@ public class AuthController {
return ResponseEntity.ok(jwtAuthResponse); return ResponseEntity.ok(jwtAuthResponse);
} }
}
@PostMapping("/profile")
public Result<Object> profile(@NotNull HttpServletRequest request) {
String token;
String bearerToken = request.getHeader("Authorization");
Result<Object> result = new Result<>();
// request 获取 JWT token
if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) {
token = bearerToken.substring(7);
} else {
token = "";
}
// 校验 token
if (StringUtils.hasText(token) && authService.getJwtTokenProvider().validateToken(token)) {
// token 获取 username
String username = authService.getJwtTokenProvider().getUsername(token);
// 加载与 token 关联的用户
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
userDetails,
null,
userDetails.getAuthorities()
);
result.setStatus(200);
result.setMessage("success");
result.setData(authenticationToken);
} else {
result.setStatus(401);
result.setMessage("fail");
}
return result;
}
}

View File

@ -1,7 +1,10 @@
package com.example.springdemo.service; package com.example.springdemo.service;
import com.example.springdemo.security.dto.LoginDto; import com.example.springdemo.security.dto.LoginDto;
import com.example.springdemo.security.utils.JwtTokenProvider;
public interface AuthService { public interface AuthService {
String login(LoginDto loginDto); String login(LoginDto loginDto);
JwtTokenProvider getJwtTokenProvider();
} }

View File

@ -26,4 +26,9 @@ public class AuthServiceImpl implements AuthService {
SecurityContextHolder.getContext().setAuthentication(authentication); SecurityContextHolder.getContext().setAuthentication(authentication);
return jwtTokenProvider.generateToken(authentication); return jwtTokenProvider.generateToken(authentication);
} }
@Override
public JwtTokenProvider getJwtTokenProvider() {
return jwtTokenProvider;
}
} }