feat(all): 添加了JWT相关功能
This commit is contained in:
parent
e28467f3fa
commit
0341293512
@ -0,0 +1,10 @@
|
||||
package org.mmga.clubs.annotations;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface AuthorizationRequired {
|
||||
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package org.mmga.clubs.configuration;
|
||||
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import jakarta.servlet.ServletOutputStream;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.NonNull;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.mmga.clubs.annotations.AuthorizationRequired;
|
||||
import org.mmga.clubs.entities.BaseResponse;
|
||||
import org.mmga.clubs.utils.JwtUtils;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class TokenConfiguration implements HandlerInterceptor, WebMvcConfigurer{
|
||||
private final JwtUtils jwtUtils;
|
||||
public TokenConfiguration(JwtUtils jwtUtils){
|
||||
this.jwtUtils = jwtUtils;
|
||||
}
|
||||
@Override
|
||||
public boolean preHandle(@NonNull HttpServletRequest request,@NonNull HttpServletResponse response,@NonNull Object handler) throws Exception {
|
||||
if (handler instanceof HandlerMethod handlerMethod){
|
||||
if (handlerMethod.hasMethodAnnotation(AuthorizationRequired.class)){
|
||||
String authorization = request.getHeader("Authorization");
|
||||
try{
|
||||
jwtUtils.verifyToken(authorization);
|
||||
}catch (JWTVerificationException e){
|
||||
log.debug("用户鉴权时出现错误:", e);
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
response.addHeader("Content-Encoding", "UTF-8");
|
||||
//TODO 使用fastjson2 进行json序列化
|
||||
BaseResponse<Object> err = BaseResponse.failed(401, "token错误");
|
||||
outputStream.write(err.toString().getBytes(StandardCharsets.UTF_8));
|
||||
outputStream.close();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(this);
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.mmga.clubs.annotations.AuthorizationRequired;
|
||||
import org.mmga.clubs.entities.BaseResponse;
|
||||
import org.mmga.clubs.entities.user.User;
|
||||
import org.mmga.clubs.entities.user.UserLoginVo;
|
||||
@ -33,4 +34,9 @@ public class UserController {
|
||||
public BaseResponse<User> createUser(@RequestBody UserRegVo user, HttpServletResponse response){
|
||||
return service.createUser(user, response);
|
||||
}
|
||||
@GetMapping("/test")
|
||||
@AuthorizationRequired
|
||||
public BaseResponse<Boolean> test(){
|
||||
return BaseResponse.success(true);
|
||||
}
|
||||
}
|
||||
|
@ -44,6 +44,9 @@ public class UserService {
|
||||
}
|
||||
return BaseResponse.success(newUser);
|
||||
}
|
||||
public User getUserById(int id){
|
||||
return packageUser(userDao.getUserById(id));
|
||||
}
|
||||
private User packageUser(UserVo vo) {
|
||||
if (vo == null){
|
||||
return null;
|
||||
|
@ -2,8 +2,11 @@ package org.mmga.clubs.utils;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.mmga.clubs.entities.user.User;
|
||||
import org.mmga.clubs.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@ -25,4 +28,8 @@ public class JwtUtils {
|
||||
log.debug("对用户:{},生成JWT:{}", user.getName(), jwt);
|
||||
return jwt;
|
||||
}
|
||||
public Integer verifyToken(String token) throws JWTVerificationException {
|
||||
DecodedJWT verify = JWT.require(Algorithm.ECDSA512(ecPublicKey, ecPrivateKey)).build().verify(token);
|
||||
return verify.getClaim("uid").asInt();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user