@@ -2,7 +2,8 @@ package com.ski.lichuan.admin.controller.auth;
import com.ski.lichuan.admin.controller.auth.dto.LoginRequest ;
import com.ski.lichuan.common.utils.JwtUtils ;
import com.ski.lichuan.model.SysUser ;
import com.ski.lichuan.model.auth. SysUser ;
import com.ski.lichuan.model.common.HttpResponseData ;
import io.swagger.v3.oas.annotations.Operation ;
import lombok.Data ;
import lombok.extern.slf4j.Slf4j ;
@@ -39,30 +40,35 @@ public class AuthController {
* @return 登录结果
*/
@PostMapping ( " /login " )
@Operation ( summary = " 用户登录 " , description = " 根据用户名和密码登录, 返回JWT Token " )
public ResponseEntity < Map < String , Object > > login ( @RequestBody LoginRequest loginRequest ) {
@Operation ( summary = " 用户登录 " , description = " 根据用户名和密码登录, 返回JWT Token " , requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody ( description = " 登录请求参数 " , required = true , content = @io.swagger.v3.oas.annotations.media.Content ( mediaType = " application/json " , schema = @io.swagger.v3.oas.annotations.media.Schema ( implementation = LoginRequest . class ) ) ) )
public Http ResponseData < LoginVo > login ( @RequestBody LoginRequest loginRequest ) {
// 1. 构造认证请求(用户名+密码)
UsernamePasswordAuthenticationToken authToken =
new UsernamePasswordAuthenticationToken ( loginRequest . getUsername ( ) , loginRequest . getPassword ( ) ) ;
// 2. 触发认证(会调用 UserDetailsService.loadUserByUsername 验证用户)
Authentication authentication = authenticationManager . authenticate ( authToken ) ;
// 2. 触发认证
try {
Authentication authentication = authenticationManager . authenticate ( authToken ) ;
// 3. 认证成功,生成 Token
SysUser user = ( SysUser ) authentication . getPrincipal ( ) ;
String token = jwtUtils . generateToken ( user . getUsername ( ) ) ;
// 3 . 认证成功,生成 Token
SysUser u ser = ( SysUser ) authentication . getPrincipal ( ) ;
String token = jwtUtils . generateT oken ( user . getUser name ( ) ) ;
// 4 . 构建响应体
HttpResponseData < LoginVo > respon se = new HttpResponseData < > ( ) ;
LoginVo loginVo = new LoginVo ( t oken , user . getNick name ( ) ) ;
response . setSuccess ( true ) ;
response . setCode ( 200 ) ;
response . setMessage ( " 登录成功 " ) ;
response . setData ( loginVo ) ;
// 4. 构建响应体
Map < String , Object > response = new HashMap < > ( ) ;
response . put ( " success " , true ) ;
response . put ( " message " , " 登录成功 " ) ;
Map < String , Object > data = new HashMap < > ( ) ;
data . put ( " token " , token ) ;
data . put ( " nickname " , user . getNickname ( ) ) ;
response . put ( " data " , data ) ;
return ResponseEntity . ok ( response ) ;
return response ;
} catch ( Exception e ) {
HttpResponseData < LoginVo > response = new HttpResponseData < > ( ) ;
response . setSuccess ( false ) ;
response . setCode ( 401 ) ;
response . setMessage ( " 用户名或密码错误 " ) ;
return response ;
}
}
/**
@@ -71,7 +77,7 @@ public class AuthController {
* @return 用户信息
*/
@GetMapping ( " /userinfo " )
@Operation ( summary = " 获取当前用户信息 " , description = " 根据JWT Token获取当前登录用户的基本信息 " )
@Operation ( summary = " 获取当前用户信息 " , description = " 根据JWT Token获取当前登录用户的基本信息 " , )
public ResponseEntity < Map < String , Object > > getUserInfo ( ) {
log . info ( " 获取用户信息请求 " ) ;