不会测那个apifox有token怎么加进去啊

This commit is contained in:
tortoise 2024-08-12 14:35:03 +08:00
parent 1a602dfd1d
commit efeabfc238
8 changed files with 205 additions and 2 deletions

View File

@ -60,6 +60,11 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@ -0,0 +1,17 @@
package top.suyiiyii.sims.common;
import java.lang.annotation.*;
/**
* @Author tortoise
* @Date 2024/8/12 11:26
* @PackageName:top.suyiiyii.sims.common
* @ClassName: AuthAccess
* @Description: TODO
* @Version 1.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AuthAccess {
}

View File

@ -0,0 +1,32 @@
package top.suyiiyii.sims.common;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* @Author tortoise
* @Date 2024/8/12 11:27
* @PackageName:top.suyiiyii.sims.common
* @ClassName: InterceptorConfig
* @Description: TODO 拦截器配置
* @Version 1.0
*/
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/login"); // 排除不需要验证的路径
super.addInterceptors(registry);
}
@Bean
public JwtInterceptor jwtInterceptor() {
return new JwtInterceptor();
}
}

View File

@ -0,0 +1,76 @@
package top.suyiiyii.sims.common;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import top.suyiiyii.sims.entity.User;
import top.suyiiyii.sims.exception.ServiceException;
import top.suyiiyii.sims.mapper.UserMapper;
/**
* @Author tortoise
* @Date 2024/8/12 11:33
* @PackageName:top.suyiiyii.sims.common
* @ClassName: JwtInterceptor
* @Description: TODO
* @Version 1.0
*/
public class JwtInterceptor implements HandlerInterceptor {
@Resource
UserMapper userMapper;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
// Authorization 头中获取 token
String token = request.getHeader("Authorization");
if (token != null && token.startsWith("Bearer ")) {
token = token.substring(7); // 去除 "Bearer " 前缀
} else {
// 如果 Authorization 头中没有 token则尝试从请求参数中获取
token = request.getParameter("token");
}
// 如果不是映射到方法直接通过
if (handler instanceof HandlerMethod) {
AuthAccess annotation = ((HandlerMethod) handler).getMethodAnnotation(AuthAccess.class);
if (annotation != null) {
return true;
}
}
// 执行认证
if (StrUtil.isBlank(token)) {
throw new ServiceException("401", "请登录");//权限错误
}
// 获取 token 中的 user id
String userId;
try {
userId = JWT.decode(token).getAudience().get(0);
} catch (JWTDecodeException j) {
throw new ServiceException("401", "请登录");
}
User user = userMapper.selectByUserId(Integer.parseInt(userId));
if (user == null) {
throw new ServiceException("401", "请登录");
}
JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(user.getPassword())).build();//加密,认证
//jwtVerifier 验证器
try {
jwtVerifier.verify(token);
} catch (JWTDecodeException e) {
throw new ServiceException("401", "请登录");
}
return true;
}
}

View File

@ -3,6 +3,7 @@ package top.suyiiyii.sims.controller;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.suyiiyii.sims.common.AuthAccess;
import top.suyiiyii.sims.common.Result;
import top.suyiiyii.sims.entity.User;
import top.suyiiyii.sims.exception.ServiceException;
@ -24,6 +25,7 @@ import java.util.List;
public class UserController {
@Autowired
UserService userService;
@AuthAccess
@GetMapping("/")
public Result hello(){
@ -42,6 +44,7 @@ public class UserController {
return Result.success(user);
}
@PostMapping("/register")
public Result register(@RequestBody User user){
if(StrUtil.isBlank(user.getUsername())||StrUtil.isBlank(user.getPassword())){
@ -58,6 +61,7 @@ public class UserController {
}
@GetMapping("/selectAll")
public Result selectAll() {
List<User> users = userService.selectAll();

View File

@ -17,7 +17,7 @@ import lombok.NoArgsConstructor;
* @Version 1.0
*/
@Data
@Table
/*@Table*/
@AllArgsConstructor
@NoArgsConstructor
public class User {
@ -29,5 +29,6 @@ public class User {
private String name;
private String email;
private String group;
@TableField(exist = false)
private String token;
}

View File

@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
import top.suyiiyii.sims.entity.User;
import top.suyiiyii.sims.exception.ServiceException;
import top.suyiiyii.sims.mapper.UserMapper;
import top.suyiiyii.sims.utils.TokenUtils;
import java.util.List;
@ -53,6 +54,8 @@ public class UserService {
if (!dbUser.getPassword().equals(user.getPassword())) {
throw new ServiceException("密码或用户名错误");
}
String token = TokenUtils.createToken(dbUser.getId().toString(), dbUser.getPassword());
dbUser.setToken(token);
return dbUser;
}

View File

@ -0,0 +1,65 @@
package top.suyiiyii.sims.utils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.suyiiyii.sims.entity.User;
import top.suyiiyii.sims.mapper.UserMapper;
import java.util.Date;
/**
* @Author tortoise
* @Date 2024/8/12 11:44
* @PackageName:top.suyiiyii.sims.utils
* @ClassName: TokenUtils
* @Description: TODO
* @Version 1.0
*/
@Component
public class TokenUtils{
private static UserMapper staticUserMapper;
@Resource
UserMapper userMapper;
@PostConstruct
public void setUserService() {
staticUserMapper=userMapper;
}
/**
* @author: tortoise
* @date: 2024/8/1 15:12
* @Description: 生成token
* @param userId
* @param sign
* @return: java.lang.String
*/
public static String createToken(String userId, String sign) {
return JWT.create().withAudience(userId)
.withExpiresAt(DateUtil.offsetHour(new Date(), 2))
.sign(Algorithm.HMAC256(sign));
}
public static User getCurrentUser() {
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader("token");
if (StrUtil.isBlank(token)) {
String userId = JWT.decode(token).getAudience().get(0);
return staticUserMapper.selectById(Integer.valueOf(userId));
}
} catch (Exception e) {
return null;
}
return null;
}
}