From 4af43dd130f0b0d749cbd330e217b61cff4c0bfb Mon Sep 17 00:00:00 2001 From: Gary Date: Sat, 27 Jan 2024 07:25:01 +0800 Subject: [PATCH] 1.fix token 2.interface to rest style --- .../java/annotation/GlobalInterceptor.java | 2 - .../src/main/java/constants/Constants.java | 3 + luoo_common/src/main/java/util/JwtUtil.java | 6 +- .../luoo/music/aspect/OperationAspect.java | 74 +------- .../music/controller/JournalController.java | 12 +- .../luoo/music/controller/SongController.java | 175 ++++++++---------- .../com/luoo/user/aspect/OperationAspect.java | 17 +- .../luoo/user/controller/IndexController.java | 2 +- .../luoo/user/controller/LoginController.java | 8 +- .../luoo/user/controller/MyController.java | 26 +-- .../controller/UserCollectController.java | 18 +- .../luoo/user/dto/response/UserRespDTO.java | 2 - .../luoo/user/service/UserInfoService.java | 8 +- 13 files changed, 127 insertions(+), 226 deletions(-) diff --git a/luoo_common/src/main/java/annotation/GlobalInterceptor.java b/luoo_common/src/main/java/annotation/GlobalInterceptor.java index 7177629..138eaa5 100644 --- a/luoo_common/src/main/java/annotation/GlobalInterceptor.java +++ b/luoo_common/src/main/java/annotation/GlobalInterceptor.java @@ -10,8 +10,6 @@ import enums.RequestFrequencyTypeEnum; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface GlobalInterceptor { - boolean checkLogin() default false; - boolean checkAppUserLogin() default false; boolean checkAdminLogin() default false; diff --git a/luoo_common/src/main/java/constants/Constants.java b/luoo_common/src/main/java/constants/Constants.java index 189c159..6117e13 100644 --- a/luoo_common/src/main/java/constants/Constants.java +++ b/luoo_common/src/main/java/constants/Constants.java @@ -6,6 +6,9 @@ public class Constants { public static final String REDIS_KEY_IMAGE_CHECK_CODE = "redis_key_image_check_code_"; public static final String REDIS_KEY_MOBILE_CHECK_CODE = "redis_key_mobile_check_code_"; + public static final String TOKEN_PREFIX = "Bearer "; + public static final int TOKEN_PREFIX_LENGTH = TOKEN_PREFIX.length(); + public static final String TOKEN_ROLE = "roles"; public static final String TOKEN_ROLE_APP_USER = "user"; diff --git a/luoo_common/src/main/java/util/JwtUtil.java b/luoo_common/src/main/java/util/JwtUtil.java index fb7931d..cf576d2 100644 --- a/luoo_common/src/main/java/util/JwtUtil.java +++ b/luoo_common/src/main/java/util/JwtUtil.java @@ -6,6 +6,7 @@ import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import org.springframework.boot.context.properties.ConfigurationProperties; +import constants.Constants; import dto.UserLoginDto; import java.util.Date; @@ -68,11 +69,12 @@ public class JwtUtil { .getBody(); } - public UserLoginDto getUserLoginDto(String token) { + public UserLoginDto getUserLoginDto(String authorization) { try { - if (StringTools.isEmpty(token)) { + if (StringTools.isEmpty(authorization)||!authorization.startsWith(Constants.TOKEN_PREFIX)) { return null; } + String token = authorization.substring(Constants.TOKEN_PREFIX_LENGTH); UserLoginDto userLoginDto=new UserLoginDto(); Claims claims = parseJWT(token); userLoginDto.setUserId(claims.getId()); diff --git a/luoo_music/src/main/java/com/luoo/music/aspect/OperationAspect.java b/luoo_music/src/main/java/com/luoo/music/aspect/OperationAspect.java index db17959..432a67f 100644 --- a/luoo_music/src/main/java/com/luoo/music/aspect/OperationAspect.java +++ b/luoo_music/src/main/java/com/luoo/music/aspect/OperationAspect.java @@ -18,15 +18,11 @@ import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; -import com.luoo.music.util.RedisUtils; - -import constants.Constants; - import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.StatusCode; +import constants.Constants; import dto.UserLoginDto; -import enums.RequestFrequencyTypeEnum; import exception.BizException; import util.JwtUtil; import util.StringTools; @@ -42,24 +38,14 @@ public class OperationAspect { @Autowired private JwtUtil jwtUtil; - @Autowired - private RedisUtils redisUtils; - @Before("@annotation(annotation.GlobalInterceptor)") public void interceptorDo(JoinPoint point) { Object[] arguments = point.getArgs(); - Object target = point.getTarget(); Method method = ((MethodSignature) point.getSignature()).getMethod(); GlobalInterceptor interceptor = method.getAnnotation(GlobalInterceptor.class); if (null == interceptor) { return; } - /** - * 登录校验 - */ - if (interceptor.checkLogin()) { - checkLogin(); - } /** * 管理用户登录校验 @@ -82,26 +68,7 @@ public class OperationAspect { validateParams(method, arguments); } - /** - * 频次校验 - */ - if (interceptor.frequencyType() != RequestFrequencyTypeEnum.NO_LIMIT && interceptor.requestFrequencyThreshold() != 0) { - String fullMethodName = target.getClass().getName() + "." + method.getName(); - checkRequestFrequency(fullMethodName, interceptor.frequencyType(), interceptor.requestFrequencyThreshold()); - } } - private void checkRequestFrequency(String fullMethodName, RequestFrequencyTypeEnum frequencyTyp, Integer threshold) { - HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); - String ip = getIpAddr(request); - ip = ip.replace(":", ""); - String redisKey = fullMethodName + ip; - Object count=redisUtils.get(redisKey); - Integer cuCount= null==count?0:Integer.valueOf((String)count); - if (cuCount != null && cuCount > threshold - 1) { - throw new BizException(StatusCode.REQUEST_TOO_FREQUENCY); - } - redisUtils.increment(redisKey, 1, frequencyTyp.getSeconds()); - } private void checkAppUserLogin() { UserLoginDto userLoginDto = getUserLoginDtoFromToken(); if (userLoginDto == null) { @@ -121,19 +88,12 @@ public class OperationAspect { throw new BizException(StatusCode.FORBIDDEN); } } - - private void checkLogin() { - UserLoginDto userLoginDto = getUserLoginDtoFromToken(); - if (userLoginDto == null) { - throw new BizException(StatusCode.UNAUTHORIZED); - } - } private UserLoginDto getUserLoginDtoFromToken() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); - String token = request.getHeader("token"); - return jwtUtil.getUserLoginDto(token); + String authorization = request.getHeader("Authorization"); + return jwtUtil.getUserLoginDto(authorization); } private void validateParams(Method method, Object[] arguments) { @@ -190,32 +150,4 @@ public class OperationAspect { throw new BizException(StatusCode.VALIDATE_FAILED); } } - private String getIpAddr(HttpServletRequest request) { - String ip = request.getHeader("x-forwarded-for"); - if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { - // 多次反向代理后会有多个ip值,第一个ip才是真实ip - if (ip.indexOf(",") != -1) { - ip = ip.split(",")[0]; - } - } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getHeader("Proxy-Client-IP"); - } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getHeader("WL-Proxy-Client-IP"); - } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getHeader("HTTP_CLIENT_IP"); - } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getHeader("HTTP_X_FORWARDED_FOR"); - } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getHeader("X-Real-IP"); - } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getRemoteAddr(); - } - return ip; - } } diff --git a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java index 29ac4f2..46c7131 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java @@ -47,12 +47,13 @@ public class JournalController { @Autowired private JwtUtil jwtUtil; - @ApiOperation(value = "1.查询期刊信息", notes = "若token为空或token校验失败,默认返回最新的10期,期刊筛选条件对游客不可用") + @ApiOperation(value = "1.查询期刊信息", notes = "若authorization为空或authorization校验失败,默认返回最新的10期,期刊筛选条件对游客不可用") @GetMapping("/list") @GlobalInterceptor - public Result> page(@RequestHeader(value = "token", required = false) String token, + public Result> page( + @RequestHeader(value = "Authorization", required = false) String authorization, @VerifyParam JournalQueryReq queryReq) { - UserLoginDto user = jwtUtil.getUserLoginDto(token); + UserLoginDto user = jwtUtil.getUserLoginDto(authorization); if (null == user) { queryReq.setLanguage(null); queryReq.setStyle(null); @@ -75,6 +76,7 @@ public class JournalController { @GetMapping("/collect/{userId}/{pageNum}/{pageSize}") @GlobalInterceptor(checkAppUserLogin = true) public Result> collectPage( + @RequestHeader(value = "Authorization", required = false) String authorization, @PathVariable @VerifyParam(required = true)String userId, @PathVariable @VerifyParam(required = true)Integer pageNum, @PathVariable @VerifyParam(required = true)Integer pageSize) { @@ -89,9 +91,9 @@ public class JournalController { @ApiOperation(value = "3.根据期刊id查询期刊信息") @GetMapping("/{id}") @GlobalInterceptor - public Result findById(@RequestHeader(value = "token", required = false) String token, + public Result findById(@RequestHeader(value = "Authorization", required = false) String authorization, @PathVariable @VerifyParam(required = true) String id) { - UserLoginDto user = jwtUtil.getUserLoginDto(token); + UserLoginDto user = jwtUtil.getUserLoginDto(authorization); Article journal=articleService.findById(id); Set journalCollectSet = null==user?Collections.emptySet():userCollectService.getCollectSet(user.getUserId(),CollectTypeEnum.JOURNAL); return Result.success(getJournalRespDTO(journal, journalCollectSet)); diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java index 48db66f..ca90f21 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java @@ -58,9 +58,10 @@ public class SongController { @ApiOperation(value = "1.根据期刊号查询歌曲信息", notes = "若为游客,期刊号须在最新10期内") @GetMapping("/getByJournalNo/{journalNo}") @GlobalInterceptor - public Result> getByJournalNo(@RequestHeader(value = "token", required = false) String token, + public Result> getByJournalNo( + @RequestHeader(value = "Authorization", required = false) String authorization, @PathVariable String journalNo) { - UserLoginDto user = jwtUtil.getUserLoginDto(token); + UserLoginDto user = jwtUtil.getUserLoginDto(authorization); if (null == user && !isLatest10(journalNo)) { return Result.unauthorized(null); } @@ -71,7 +72,7 @@ public class SongController { .collect(Collectors.toList()); return Result.success(results); } - + private boolean isLatest10(String journalNo) { return articleService.isLatest10(journalNo); } @@ -82,7 +83,9 @@ public class SongController { @ApiImplicitParam(name = "pageSize", value = "分页: 每页数量", required = true) }) @GetMapping("/collect/{userId}/{pageNum}/{pageSize}") @GlobalInterceptor(checkAppUserLogin = true) - public Result> collectPage(@PathVariable @VerifyParam(required = true) String userId, + public Result> collectPage( + @RequestHeader(value = "Authorization", required = false) String authorization, + @PathVariable @VerifyParam(required = true) String userId, @PathVariable @VerifyParam(required = true) Integer pageNum, @PathVariable @VerifyParam(required = true) Integer pageSize) { List objectIds = userCollectService.getCollectList(userId, pageNum, pageSize, CollectTypeEnum.SONG); @@ -98,10 +101,11 @@ public class SongController { @ApiImplicitParams({ @ApiImplicitParam(name = "limit", value = "随机歌曲数,最少1首,最多30首", required = false) }) @GetMapping("/random/{limit}") @GlobalInterceptor - public Result> random(@RequestHeader(value = "token", required = false) String token, + public Result> random( + @RequestHeader(value = "Authorization", required = false) String authorization, @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { List songs = songService.random(limit); - UserLoginDto user = jwtUtil.getUserLoginDto(token); + UserLoginDto user = jwtUtil.getUserLoginDto(authorization); Set songCollectSet = null == user ? Collections.emptySet() : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) @@ -112,9 +116,9 @@ public class SongController { @ApiOperation(value = "4.根据歌曲id查询歌曲信息") @GetMapping("/{id}") @GlobalInterceptor - public Result findById(@RequestHeader(value = "token", required = false) String token, + public Result findById(@RequestHeader(value = "Authorization", required = false) String authorization, @PathVariable @VerifyParam(required = true) String id) { - UserLoginDto user = jwtUtil.getUserLoginDto(token); + UserLoginDto user = jwtUtil.getUserLoginDto(authorization); Song song = songService.findById(id); Set songCollectSet = null == user ? Collections.emptySet() : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); @@ -154,99 +158,72 @@ public class SongController { * @param size 页大小 * @return 分页结果 */ - @PostMapping("/search/{page}/{size}") - public Result> findSearch(@RequestBody Map searchMap, @PathVariable int page, - @PathVariable int size) { - Page pageList = songService.findSearch(searchMap, page, size); - return Result.success(new PageResult(pageList.getTotalElements(), pageList.getContent())); - } - - @PostMapping("/init") - public Result init(@RequestBody Map map) { - List data = (List) map.get("data"); - System.out.println(data.size()); - Song song = new Song(); - Set
set = new HashSet
(); - - int temp = 0; - int num = 0; - for (Map dataMap : data) { - Article article = new Article(); - article.setVolid(dataMap.get("id") + ""); - article.setTitle(dataMap.get("title") + ""); - String avatar = "0000" + dataMap.get("id"); - String substring = avatar.substring(avatar.length() - 5); - System.out.println(substring); - article.setImage(substring + "/00.jpg"); - if (temp == (int) dataMap.get("id")) { - num = num + 1; - } else { - num = 0; - } - temp = (int) dataMap.get("id"); - String numstr = "0000" + (num + 1); - System.out.println(numstr.substring(numstr.length() - 2)); - - song.setAlbum(dataMap.get("album") + ""); - song.setArtist(dataMap.get("artist") + ""); - song.setName(dataMap.get("name") + ""); - song.setVolid(dataMap.get("id") + ""); - song.setUrl(substring + "/" + numstr.substring(numstr.length() - 2) + ".mp3"); - song.setSongno(num + 1); - - songService.add(song); - set.add(article); - } - System.out.println(set.size()); - - for (Article pojo : set) { -// articleService.add(pojo); - } - return Result.success(); - } - - /** - * 根据条件查询 + /* + * @PostMapping("/search/{page}/{size}") public Result> + * findSearch(@RequestBody Map searchMap, @PathVariable int page, * - * @param searchMap - * @return - */ - @PostMapping("/search") - public Result> findSearch(@RequestBody Map searchMap) { - return Result.success(songService.findSearch(searchMap)); - } - - /** - * 增加 + * @PathVariable int size) { Page pageList = + * songService.findSearch(searchMap, page, size); return Result.success(new + * PageResult(pageList.getTotalElements(), pageList.getContent())); } * - * @param song - */ - @PostMapping - public Result add(@RequestBody Song song) { - songService.add(song); - return Result.success(); - } - - /** - * 修改 + * @PostMapping("/init") public Result init(@RequestBody Map map) { + * List data = (List) map.get("data"); System.out.println(data.size()); + * Song song = new Song(); Set
set = new HashSet
(); * - * @param song - */ - @PutMapping("/{id}") - public Result update(@RequestBody Song song, @PathVariable String id) { - song.setId(id); - songService.update(song); - return Result.success(); - } - - /** - * 删除 + * int temp = 0; int num = 0; for (Map dataMap : data) { Article article = new + * Article(); article.setVolid(dataMap.get("id") + ""); + * article.setTitle(dataMap.get("title") + ""); String avatar = "0000" + + * dataMap.get("id"); String substring = avatar.substring(avatar.length() - 5); + * System.out.println(substring); article.setImage(substring + "/00.jpg"); if + * (temp == (int) dataMap.get("id")) { num = num + 1; } else { num = 0; } temp = + * (int) dataMap.get("id"); String numstr = "0000" + (num + 1); + * System.out.println(numstr.substring(numstr.length() - 2)); * - * @param id - */ - @DeleteMapping("/{id}") - public Result delete(@PathVariable String id) { - songService.deleteById(id); - return Result.success(); - } + * song.setAlbum(dataMap.get("album") + ""); + * song.setArtist(dataMap.get("artist") + ""); song.setName(dataMap.get("name") + * + ""); song.setVolid(dataMap.get("id") + ""); song.setUrl(substring + "/" + + * numstr.substring(numstr.length() - 2) + ".mp3"); song.setSongno(num + 1); + * + * songService.add(song); set.add(article); } System.out.println(set.size()); + * + * for (Article pojo : set) { // articleService.add(pojo); } return + * Result.success(); } + * + *//** + * 根据条件查询 + * + * @param searchMap + * @return + */ + /* + * @PostMapping("/search") public Result> findSearch(@RequestBody Map + * searchMap) { return Result.success(songService.findSearch(searchMap)); } + * + *//** + * 增加 + * + * @param song + */ + /* + * @PostMapping public Result add(@RequestBody Song song) { + * songService.add(song); return Result.success(); } + * + *//** + * 修改 + * + * @param song + */ + /* + * @PutMapping("/{id}") public Result update(@RequestBody Song + * song, @PathVariable String id) { song.setId(id); songService.update(song); + * return Result.success(); } + * + *//** + * 删除 + * + * @param id + *//* + * @DeleteMapping("/{id}") public Result delete(@PathVariable String id) { + * songService.deleteById(id); return Result.success(); } + */ } diff --git a/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java b/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java index 64f8cda..71ec7b5 100644 --- a/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java +++ b/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java @@ -53,12 +53,6 @@ public class OperationAspect { if (null == interceptor) { return; } - /** - * 登录校验 - */ - if (interceptor.checkLogin()) { - checkLogin(); - } /** * 管理用户登录校验 @@ -120,19 +114,12 @@ public class OperationAspect { throw new BizException(StatusCode.FORBIDDEN); } } - - private void checkLogin() { - UserLoginDto userLoginDto = getUserLoginDtoFromToken(); - if (userLoginDto == null) { - throw new BizException(StatusCode.UNAUTHORIZED); - } - } private UserLoginDto getUserLoginDtoFromToken() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); - String token = request.getHeader("token"); - return jwtUtil.getUserLoginDto(token); + String authorization = request.getHeader("Authorization"); + return jwtUtil.getUserLoginDto(authorization); } private void validateParams(Method method, Object[] arguments) { diff --git a/luoo_user/src/main/java/com/luoo/user/controller/IndexController.java b/luoo_user/src/main/java/com/luoo/user/controller/IndexController.java index 8e7c912..8c91bbf 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/IndexController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/IndexController.java @@ -22,7 +22,7 @@ public class IndexController { private CarouselService carouselService; @ApiOperation(value = "1.获取轮播图", notes = "返回5个") - @GetMapping("/loadCarousel") + @GetMapping("/carousel") public Result> loadCarousel() { List carouselList = carouselService.loadCarousel(); return Result.success(carouselList); diff --git a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java index f846420..0c7c6e2 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java @@ -100,7 +100,7 @@ public class LoginController extends BaseController { return Result.success(); } - @ApiOperation(value = "2.登录/注册", notes = "成功后返回token") + @ApiOperation(value = "2.登录/注册", notes = "成功后返回authorization") @ApiImplicitParams({ @ApiImplicitParam(name = "mobile", value = "手机号", required = true), @ApiImplicitParam(name = "mobileCheckCode", value = "6位验证码", required = true), @ApiImplicitParam(name = "deviceId", value = "设备id", required = true), @@ -134,7 +134,7 @@ public class LoginController extends BaseController { } /** - * token 续期 + * authorization 续期 */ @ApiOperation(value = "3.token续期", notes = "有效期7天") @ApiImplicitParams({ @ApiImplicitParam(name = "deviceId", value = "设备id", required = true), @@ -142,10 +142,10 @@ public class LoginController extends BaseController { @PostMapping("/autoLogin") @GlobalInterceptor public Result autoLogin(HttpServletRequest request, - @RequestHeader(value = "token", required = false) String token, + @RequestHeader(value = "Authorization", required = false) String authorization, @VerifyParam(required = true) @RequestParam("deviceId") String deviceId, @RequestParam("deviceBrand") String deviceBrand) { - return Result.success(userInfoService.autoLogin(token, deviceId, deviceBrand, getIpAddr(request))); + return Result.success(userInfoService.autoLogin(authorization, deviceId, deviceBrand, getIpAddr(request))); } /** diff --git a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java index a7587f4..e4e9a0b 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java @@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; @@ -74,10 +75,10 @@ public class MyController extends BaseController { public static String UPLOAD_DIRECTORY = "user/avatar/"; @ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息") - @GetMapping("/getUserInfo") + @GetMapping("/userInfo") @GlobalInterceptor(checkAppUserLogin = true) - public Result getUserInfo(@RequestHeader(value = "token", required = false) String token) { - UserLoginDto userLoginDto = getUserLoginDto(token); + public Result getUserInfo(@RequestHeader(value = "Authorization", required = false) String authorization) { + UserLoginDto userLoginDto = getUserLoginDto(authorization); if (null == userLoginDto) { return Result.unauthorized(null); } @@ -118,11 +119,11 @@ public class MyController extends BaseController { } @ApiOperation(value = "2.更新个人信息", notes = "游客无法编辑个人信息") - @PostMapping("/updateUserInfo") + @PutMapping("/userInfo") @GlobalInterceptor(checkAppUserLogin = true) - public Result updateUserInfo(@RequestHeader(value = "token", required = false) String token, + public Result updateUserInfo(@RequestHeader(value = "Authorization", required = false) String authorization, @VerifyParam(required = true) @RequestBody UserInfoUpdateDto userInfoUpdateDto) { - UserLoginDto userLoginDto = getUserLoginDto(token); + UserLoginDto userLoginDto = getUserLoginDto(authorization); UserInfo user = userInfoService.findById(userLoginDto.getUserId()); String nickName = userInfoUpdateDto.getNickName(); if (!StringTools.isEmpty(nickName) && !nickName.equals(user.getNickName())) { @@ -151,11 +152,11 @@ public class MyController extends BaseController { } @ApiOperation(value = "3.上传个人头像", notes = "图片压缩为70X70 JPEG,存入S3,桶为indie,目录为 user/avatar/") - @PostMapping("/uploadAvatar") - @GlobalInterceptor(checkLogin = true) - public Result uploadAvatar(@RequestHeader(value = "token", required = false) String token, + @PostMapping("/avatar") + @GlobalInterceptor(checkAppUserLogin = true) + public Result uploadAvatar(@RequestHeader(value = "Authorization", required = false) String authorization, @VerifyParam(required = true) MultipartFile file) throws IOException { - UserLoginDto userLoginDto = getUserLoginDto(token); + UserLoginDto userLoginDto = getUserLoginDto(authorization); byte[] thumbnail = ScaleFilter.createThumbnail(file.getInputStream(), Constants.LENGTH_70, Constants.LENGTH_70); String avatarName = userLoginDto.getUserId() + "_" + idWorker.nextId() @@ -171,9 +172,10 @@ public class MyController extends BaseController { } @ApiOperation(value = "4.查看他人信息", notes = "游客无法查看他人信息") - @GetMapping("/getOtherUserInfo/{userId}") + @GetMapping("/otherUserInfo/{userId}") @GlobalInterceptor(checkAppUserLogin = true) - public Result getOtherUserInfo(@VerifyParam(required = true) @PathVariable String userId) { + public Result getOtherUserInfo(@RequestHeader(value = "Authorization", required = false) String authorization, + @VerifyParam(required = true) @PathVariable String userId) { UserInfo user = userInfoService.findById(userId); if (null == user) { return Result.failed(StatusCode.USER_INVALID_USER_ID); diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java index 1efcb9f..d693331 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java @@ -15,7 +15,7 @@ import util.JwtUtil; @Api(tags = "UserCollectController") @RestController -@RequestMapping("/userCollect") +@RequestMapping("/collect") public class UserCollectController { @Autowired private JwtUtil jwtUtil; @@ -29,12 +29,12 @@ public class UserCollectController { //@ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊,2:关注,3:黑名单,4:点赞", required = true) }) @ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊的id", required = true), @ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊", required = true) }) - @PostMapping("/addCollect") - @GlobalInterceptor(checkLogin = true) - public Result addCollect(@RequestHeader(value = "token", required = false) String token, + @PostMapping + @GlobalInterceptor(checkAppUserLogin = true) + public Result addCollect(@RequestHeader(value = "Authorization", required = false) String authorization, @VerifyParam(required = true) @RequestParam("objectId") String objectId, @VerifyParam(required = true) @RequestParam("collectType") Integer collectType) { - UserLoginDto loginDto = jwtUtil.getUserLoginDto(token); + UserLoginDto loginDto = jwtUtil.getUserLoginDto(authorization); userCollectService.saveCollect(loginDto.getUserId(), objectId, collectType); return Result.success(); } @@ -46,12 +46,12 @@ public class UserCollectController { //@ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊,2:关注,3:黑名单,4:点赞", required = true) }) @ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊的id", required = true), @ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊", required = true) }) - @DeleteMapping("/cancelCollect") - @GlobalInterceptor(checkLogin = true) - public Result cancelCollect(@RequestHeader(value = "token", required = false) String token, + @DeleteMapping + @GlobalInterceptor(checkAppUserLogin = true) + public Result cancelCollect(@RequestHeader(value = "Authorization", required = false) String authorization, @VerifyParam(required = true) @RequestParam("objectId") String objectId, @VerifyParam(required = true) @RequestParam("collectType") Integer collectType) { - UserLoginDto loginDto = jwtUtil.getUserLoginDto(token); + UserLoginDto loginDto = jwtUtil.getUserLoginDto(authorization); userCollectService.deleteByUserIdAndObjectIdAndCollectType(loginDto.getUserId(), objectId, collectType); return Result.success(); diff --git a/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java b/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java index 70a0358..348f507 100644 --- a/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java +++ b/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java @@ -31,11 +31,9 @@ public class UserRespDTO { @ApiModelProperty(value = "喜欢歌曲数") private int songCount; - private Set songSet; @ApiModelProperty(value = "收藏期刊数") private int journalCount; - private Set journalSet; @ApiModelProperty(value = "获得评论数") private int commentReplyCount; @ApiModelProperty(value = "生日,格式为: yyyy.MM.dd") diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index 5d48ae9..f902e59 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -266,8 +266,8 @@ public class UserInfoService { return userInfo; } - public String autoLogin(String token, String deviceId, String deviceBrand, String ip) { - UserLoginDto userLoginDto=jwtUtil.getUserLoginDto(token); + public String autoLogin(String authorization, String deviceId, String deviceBrand, String ip) { + UserLoginDto userLoginDto=jwtUtil.getUserLoginDto(authorization); if (null==userLoginDto) { return null; } @@ -280,7 +280,7 @@ public class UserInfoService { userInfo.setLastUseDeviceId(deviceId); userInfo.setLastUseDeviceBrand(deviceBrand); userInfoDao.save(userInfo); - return jwtUtil.createJWT(userLoginDto.getUserId(), userInfo.getNickName(), userLoginDto.getRoles()); + return Constants.TOKEN_PREFIX+jwtUtil.createJWT(userLoginDto.getUserId(), userInfo.getNickName(), userLoginDto.getRoles()); } public long countByNickName(String nickName) { @@ -304,7 +304,7 @@ public class UserInfoService { } user.setLastLoginTime(new Date()); userInfoDao.save(user); - return jwtUtil.createJWT(user.getId(),user.getNickName(),Constants.TOKEN_ROLE_APP_USER); + return Constants.TOKEN_PREFIX+jwtUtil.createJWT(user.getId(),user.getNickName(),Constants.TOKEN_ROLE_APP_USER); } public List orderByField(List idList) {