feat(jwt): implement JWT-based authentication system
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.hanserwei.jwt.handler;
|
||||
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import com.hanserwei.jwt.model.LoginRspVO;
|
||||
import com.hanserwei.jwt.utils.JwtTokenHelper;
|
||||
import com.hanserwei.jwt.utils.ResultUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RestAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
||||
|
||||
@Resource
|
||||
private JwtTokenHelper jwtTokenHelper;
|
||||
|
||||
@Override
|
||||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
|
||||
// 从 authentication 对象中获取用户的 UserDetails 实例,这里是获取用户的用户名
|
||||
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
||||
|
||||
// 通过用户名生成 Token
|
||||
String username = userDetails.getUsername();
|
||||
String token = jwtTokenHelper.generateToken(username);
|
||||
|
||||
// 返回 Token
|
||||
LoginRspVO loginRspVO = LoginRspVO.builder().token(token).build();
|
||||
|
||||
ResultUtil.ok(response, Response.success(loginRspVO));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user