整合作业
This commit is contained in:
@@ -36,4 +36,10 @@ public class JwtUtil {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 从 Authorization 头解析 JWT 格式Bearer xxx失败返回 null
|
||||
public static Claims parseBearer(String auth) {
|
||||
if (auth == null || !auth.startsWith("Bearer ")) return null;
|
||||
return parse(auth.substring(7));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,9 +28,4 @@ public class Result<T> {
|
||||
public static <T> Result<T> error(String message) {
|
||||
return new Result<>(ResultCode.BAD_REQUEST.getCode(), message, null);
|
||||
}
|
||||
|
||||
/** 失败(指定错误码) */
|
||||
public static <T> Result<T> error(Integer code, String message) {
|
||||
return new Result<>(code, message, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,7 @@ import lombok.Getter;
|
||||
@Getter
|
||||
public enum ResultCode {
|
||||
SUCCESS(200, "成功"),
|
||||
BAD_REQUEST(400, "参数错误"),
|
||||
UNAUTHORIZED(401, "未登录");
|
||||
BAD_REQUEST(400, "参数错误");
|
||||
|
||||
private final Integer code;
|
||||
private final String message;
|
||||
|
||||
@@ -24,7 +24,7 @@ public class AuthInterceptor implements HandlerInterceptor {
|
||||
String auth = request.getHeader("Authorization");// 请求头 Authorization
|
||||
|
||||
// 已登录就把 username 放进 request,供 Controller 取用
|
||||
Claims claims = parse(auth);
|
||||
Claims claims = JwtUtil.parseBearer(auth);
|
||||
if (claims != null) {
|
||||
request.setAttribute("username", claims.get("username", String.class));
|
||||
}
|
||||
@@ -49,10 +49,4 @@ public class AuthInterceptor implements HandlerInterceptor {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 解析 token,失败返回 null
|
||||
private Claims parse(String auth) {
|
||||
if (auth == null || !auth.startsWith("Bearer ")) return null;
|
||||
return JwtUtil.parse(auth.substring(7));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public class WebAppConfig implements WebMvcConfigurer {
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(authInterceptor)
|
||||
.addPathPatterns("/api/**")
|
||||
.excludePathPatterns("/api/login", "/api/upload", "/api/me", "/api/prodect", "/api/logs");
|
||||
.excludePathPatterns("/api/login", "/api/upload", "/api/prodect", "/api/logs");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,11 +20,12 @@ public class UploadController {
|
||||
String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
|
||||
String fileName = UUID.randomUUID().toString().replace("-", "") + ext;
|
||||
|
||||
// 固定目录:放在部署目录之外,重新编译/部署也不会丢失图片
|
||||
// 如果上传目录不存在就创建
|
||||
File dir = new File(UploadConstants.UPLOAD_DIR);
|
||||
if (!dir.exists()) dir.mkdirs();
|
||||
file.transferTo(new File(dir, fileName));
|
||||
|
||||
// 返回上传成功后的URL路径
|
||||
Map<String, String> data = new HashMap<>();
|
||||
data.put("url", "/upload/" + fileName);
|
||||
return Result.success(data);
|
||||
|
||||
@@ -2,11 +2,9 @@ package com.ycjy.controller;
|
||||
|
||||
import com.ycjy.common.JwtUtil;
|
||||
import com.ycjy.common.Result;
|
||||
import com.ycjy.common.ResultCode;
|
||||
import com.ycjy.dao.User;
|
||||
import com.ycjy.dto.LoginDTO;
|
||||
import com.ycjy.service.UserService;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -32,16 +30,4 @@ public class UserController {
|
||||
data.put("user", user);
|
||||
return Result.success(data);
|
||||
}
|
||||
|
||||
@GetMapping("/me")
|
||||
public Result<User> me(@RequestHeader(value = "Authorization", required = false) String auth) {
|
||||
if (auth == null || !auth.startsWith("Bearer ")) return Result.error(ResultCode.UNAUTHORIZED.getCode(), "未登录");
|
||||
Claims claims = JwtUtil.parse(auth.substring(7));
|
||||
if (claims == null) return Result.error(ResultCode.UNAUTHORIZED.getCode(), "登录已失效");
|
||||
Integer userId = Integer.valueOf(claims.getSubject());
|
||||
User user = userService.getById(userId);
|
||||
if (user == null) return Result.error(ResultCode.UNAUTHORIZED.getCode(), "用户不存在");
|
||||
user.setPassword(null);
|
||||
return Result.success(user);
|
||||
}
|
||||
}
|
||||
@@ -6,5 +6,4 @@ import org.apache.ibatis.annotations.Param;
|
||||
|
||||
public interface UserMapper {
|
||||
User getUser(@Param("username") String username);
|
||||
User getById(@Param("id") Integer id);
|
||||
}
|
||||
|
||||
@@ -16,8 +16,4 @@ public class UserService {
|
||||
if (user == null || !password.equals(user.getPassword())) return null;
|
||||
return user;
|
||||
}
|
||||
|
||||
public User getById(Integer id) {
|
||||
return userMapper.getById(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,4 @@
|
||||
WHERE username = #{username}
|
||||
LIMIT 1
|
||||
</select>
|
||||
|
||||
<select id="getById" resultType="com.ycjy.dao.User">
|
||||
SELECT id, username
|
||||
FROM demo_user
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user