1.fix token 2.interface to rest style

main
Gary 10 months ago
parent b1fd675442
commit 4af43dd130

@ -10,8 +10,6 @@ import enums.RequestFrequencyTypeEnum;
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface GlobalInterceptor { public @interface GlobalInterceptor {
boolean checkLogin() default false;
boolean checkAppUserLogin() default false; boolean checkAppUserLogin() default false;
boolean checkAdminLogin() default false; boolean checkAdminLogin() default false;

@ -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_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 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 = "roles";
public static final String TOKEN_ROLE_APP_USER = "user"; public static final String TOKEN_ROLE_APP_USER = "user";

@ -6,6 +6,7 @@ import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import java.util.Date; import java.util.Date;
@ -68,11 +69,12 @@ public class JwtUtil {
.getBody(); .getBody();
} }
public UserLoginDto getUserLoginDto(String token) { public UserLoginDto getUserLoginDto(String authorization) {
try { try {
if (StringTools.isEmpty(token)) { if (StringTools.isEmpty(authorization)||!authorization.startsWith(Constants.TOKEN_PREFIX)) {
return null; return null;
} }
String token = authorization.substring(Constants.TOKEN_PREFIX_LENGTH);
UserLoginDto userLoginDto=new UserLoginDto(); UserLoginDto userLoginDto=new UserLoginDto();
Claims claims = parseJWT(token); Claims claims = parseJWT(token);
userLoginDto.setUserId(claims.getId()); userLoginDto.setUserId(claims.getId());

@ -18,15 +18,11 @@ import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import com.luoo.music.util.RedisUtils;
import constants.Constants;
import annotation.GlobalInterceptor; import annotation.GlobalInterceptor;
import annotation.VerifyParam; import annotation.VerifyParam;
import api.StatusCode; import api.StatusCode;
import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.RequestFrequencyTypeEnum;
import exception.BizException; import exception.BizException;
import util.JwtUtil; import util.JwtUtil;
import util.StringTools; import util.StringTools;
@ -42,24 +38,14 @@ public class OperationAspect {
@Autowired @Autowired
private JwtUtil jwtUtil; private JwtUtil jwtUtil;
@Autowired
private RedisUtils redisUtils;
@Before("@annotation(annotation.GlobalInterceptor)") @Before("@annotation(annotation.GlobalInterceptor)")
public void interceptorDo(JoinPoint point) { public void interceptorDo(JoinPoint point) {
Object[] arguments = point.getArgs(); Object[] arguments = point.getArgs();
Object target = point.getTarget();
Method method = ((MethodSignature) point.getSignature()).getMethod(); Method method = ((MethodSignature) point.getSignature()).getMethod();
GlobalInterceptor interceptor = method.getAnnotation(GlobalInterceptor.class); GlobalInterceptor interceptor = method.getAnnotation(GlobalInterceptor.class);
if (null == interceptor) { if (null == interceptor) {
return; return;
} }
/**
*
*/
if (interceptor.checkLogin()) {
checkLogin();
}
/** /**
* *
@ -82,25 +68,6 @@ public class OperationAspect {
validateParams(method, arguments); 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() { private void checkAppUserLogin() {
UserLoginDto userLoginDto = getUserLoginDtoFromToken(); UserLoginDto userLoginDto = getUserLoginDtoFromToken();
@ -122,18 +89,11 @@ public class OperationAspect {
} }
} }
private void checkLogin() {
UserLoginDto userLoginDto = getUserLoginDtoFromToken();
if (userLoginDto == null) {
throw new BizException(StatusCode.UNAUTHORIZED);
}
}
private UserLoginDto getUserLoginDtoFromToken() { private UserLoginDto getUserLoginDtoFromToken() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest(); .getRequest();
String token = request.getHeader("token"); String authorization = request.getHeader("Authorization");
return jwtUtil.getUserLoginDto(token); return jwtUtil.getUserLoginDto(authorization);
} }
private void validateParams(Method method, Object[] arguments) { private void validateParams(Method method, Object[] arguments) {
@ -190,32 +150,4 @@ public class OperationAspect {
throw new BizException(StatusCode.VALIDATE_FAILED); 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;
}
} }

@ -47,12 +47,13 @@ public class JournalController {
@Autowired @Autowired
private JwtUtil jwtUtil; private JwtUtil jwtUtil;
@ApiOperation(value = "1.查询期刊信息", notes = "若token为空或token校验失败默认返回最新的10期期刊筛选条件对游客不可用") @ApiOperation(value = "1.查询期刊信息", notes = "若authorization为空或authorization校验失败默认返回最新的10期期刊筛选条件对游客不可用")
@GetMapping("/list") @GetMapping("/list")
@GlobalInterceptor @GlobalInterceptor
public Result<PageResult<JournalRespDTO>> page(@RequestHeader(value = "token", required = false) String token, public Result<PageResult<JournalRespDTO>> page(
@RequestHeader(value = "Authorization", required = false) String authorization,
@VerifyParam JournalQueryReq queryReq) { @VerifyParam JournalQueryReq queryReq) {
UserLoginDto user = jwtUtil.getUserLoginDto(token); UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
if (null == user) { if (null == user) {
queryReq.setLanguage(null); queryReq.setLanguage(null);
queryReq.setStyle(null); queryReq.setStyle(null);
@ -75,6 +76,7 @@ public class JournalController {
@GetMapping("/collect/{userId}/{pageNum}/{pageSize}") @GetMapping("/collect/{userId}/{pageNum}/{pageSize}")
@GlobalInterceptor(checkAppUserLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<PageResult<JournalRespDTO>> collectPage( public Result<PageResult<JournalRespDTO>> collectPage(
@RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable @VerifyParam(required = true)String userId, @PathVariable @VerifyParam(required = true)String userId,
@PathVariable @VerifyParam(required = true)Integer pageNum, @PathVariable @VerifyParam(required = true)Integer pageNum,
@PathVariable @VerifyParam(required = true)Integer pageSize) { @PathVariable @VerifyParam(required = true)Integer pageSize) {
@ -89,9 +91,9 @@ public class JournalController {
@ApiOperation(value = "3.根据期刊id查询期刊信息") @ApiOperation(value = "3.根据期刊id查询期刊信息")
@GetMapping("/{id}") @GetMapping("/{id}")
@GlobalInterceptor @GlobalInterceptor
public Result<JournalRespDTO> findById(@RequestHeader(value = "token", required = false) String token, public Result<JournalRespDTO> findById(@RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable @VerifyParam(required = true) String id) { @PathVariable @VerifyParam(required = true) String id) {
UserLoginDto user = jwtUtil.getUserLoginDto(token); UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
Article journal=articleService.findById(id); Article journal=articleService.findById(id);
Set<String> journalCollectSet = null==user?Collections.emptySet():userCollectService.getCollectSet(user.getUserId(),CollectTypeEnum.JOURNAL); Set<String> journalCollectSet = null==user?Collections.emptySet():userCollectService.getCollectSet(user.getUserId(),CollectTypeEnum.JOURNAL);
return Result.success(getJournalRespDTO(journal, journalCollectSet)); return Result.success(getJournalRespDTO(journal, journalCollectSet));

@ -58,9 +58,10 @@ public class SongController {
@ApiOperation(value = "1.根据期刊号查询歌曲信息", notes = "若为游客期刊号须在最新10期内") @ApiOperation(value = "1.根据期刊号查询歌曲信息", notes = "若为游客期刊号须在最新10期内")
@GetMapping("/getByJournalNo/{journalNo}") @GetMapping("/getByJournalNo/{journalNo}")
@GlobalInterceptor @GlobalInterceptor
public Result<List<SongRespDTO>> getByJournalNo(@RequestHeader(value = "token", required = false) String token, public Result<List<SongRespDTO>> getByJournalNo(
@RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable String journalNo) { @PathVariable String journalNo) {
UserLoginDto user = jwtUtil.getUserLoginDto(token); UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
if (null == user && !isLatest10(journalNo)) { if (null == user && !isLatest10(journalNo)) {
return Result.unauthorized(null); return Result.unauthorized(null);
} }
@ -82,7 +83,9 @@ public class SongController {
@ApiImplicitParam(name = "pageSize", value = "分页: 每页数量", required = true) }) @ApiImplicitParam(name = "pageSize", value = "分页: 每页数量", required = true) })
@GetMapping("/collect/{userId}/{pageNum}/{pageSize}") @GetMapping("/collect/{userId}/{pageNum}/{pageSize}")
@GlobalInterceptor(checkAppUserLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<PageResult<SongRespDTO>> collectPage(@PathVariable @VerifyParam(required = true) String userId, public Result<PageResult<SongRespDTO>> 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 pageNum,
@PathVariable @VerifyParam(required = true) Integer pageSize) { @PathVariable @VerifyParam(required = true) Integer pageSize) {
List<String> objectIds = userCollectService.getCollectList(userId, pageNum, pageSize, CollectTypeEnum.SONG); List<String> objectIds = userCollectService.getCollectList(userId, pageNum, pageSize, CollectTypeEnum.SONG);
@ -98,10 +101,11 @@ public class SongController {
@ApiImplicitParams({ @ApiImplicitParam(name = "limit", value = "随机歌曲数最少1首最多30首", required = false) }) @ApiImplicitParams({ @ApiImplicitParam(name = "limit", value = "随机歌曲数最少1首最多30首", required = false) })
@GetMapping("/random/{limit}") @GetMapping("/random/{limit}")
@GlobalInterceptor @GlobalInterceptor
public Result<List<SongRespDTO>> random(@RequestHeader(value = "token", required = false) String token, public Result<List<SongRespDTO>> random(
@RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) {
List<Song> songs = songService.random(limit); List<Song> songs = songService.random(limit);
UserLoginDto user = jwtUtil.getUserLoginDto(token); UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
Set<String> songCollectSet = null == user ? Collections.emptySet() Set<String> songCollectSet = null == user ? Collections.emptySet()
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet))
@ -112,9 +116,9 @@ public class SongController {
@ApiOperation(value = "4.根据歌曲id查询歌曲信息") @ApiOperation(value = "4.根据歌曲id查询歌曲信息")
@GetMapping("/{id}") @GetMapping("/{id}")
@GlobalInterceptor @GlobalInterceptor
public Result<SongRespDTO> findById(@RequestHeader(value = "token", required = false) String token, public Result<SongRespDTO> findById(@RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable @VerifyParam(required = true) String id) { @PathVariable @VerifyParam(required = true) String id) {
UserLoginDto user = jwtUtil.getUserLoginDto(token); UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
Song song = songService.findById(id); Song song = songService.findById(id);
Set<String> songCollectSet = null == user ? Collections.emptySet() Set<String> songCollectSet = null == user ? Collections.emptySet()
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG);
@ -154,99 +158,72 @@ public class SongController {
* @param size * @param size
* @return * @return
*/ */
@PostMapping("/search/{page}/{size}") /*
public Result<PageResult<Song>> findSearch(@RequestBody Map searchMap, @PathVariable int page, * @PostMapping("/search/{page}/{size}") public Result<PageResult<Song>>
@PathVariable int size) { * findSearch(@RequestBody Map searchMap, @PathVariable int page,
Page<Song> pageList = songService.findSearch(searchMap, page, size); *
return Result.success(new PageResult<Song>(pageList.getTotalElements(), pageList.getContent())); * @PathVariable int size) { Page<Song> pageList =
} * songService.findSearch(searchMap, page, size); return Result.success(new
* PageResult<Song>(pageList.getTotalElements(), pageList.getContent())); }
@PostMapping("/init") *
public Result<Void> init(@RequestBody Map map) { * @PostMapping("/init") public Result<Void> init(@RequestBody Map map) {
List<Map> data = (List) map.get("data"); * List<Map> data = (List) map.get("data"); System.out.println(data.size());
System.out.println(data.size()); * Song song = new Song(); Set<Article> set = new HashSet<Article>();
Song song = new Song(); *
Set<Article> set = new HashSet<Article>(); * int temp = 0; int num = 0; for (Map dataMap : data) { Article article = new
* Article(); article.setVolid(dataMap.get("id") + "");
int temp = 0; * article.setTitle(dataMap.get("title") + ""); String avatar = "0000" +
int num = 0; * dataMap.get("id"); String substring = avatar.substring(avatar.length() - 5);
for (Map dataMap : data) { * System.out.println(substring); article.setImage(substring + "/00.jpg"); if
Article article = new Article(); * (temp == (int) dataMap.get("id")) { num = num + 1; } else { num = 0; } temp =
article.setVolid(dataMap.get("id") + ""); * (int) dataMap.get("id"); String numstr = "0000" + (num + 1);
article.setTitle(dataMap.get("title") + ""); * System.out.println(numstr.substring(numstr.length() - 2));
String avatar = "0000" + dataMap.get("id"); *
String substring = avatar.substring(avatar.length() - 5); * song.setAlbum(dataMap.get("album") + "");
System.out.println(substring); * song.setArtist(dataMap.get("artist") + ""); song.setName(dataMap.get("name")
article.setImage(substring + "/00.jpg"); * + ""); song.setVolid(dataMap.get("id") + ""); song.setUrl(substring + "/" +
if (temp == (int) dataMap.get("id")) { * numstr.substring(numstr.length() - 2) + ".mp3"); song.setSongno(num + 1);
num = num + 1; *
} else { * songService.add(song); set.add(article); } System.out.println(set.size());
num = 0; *
} * for (Article pojo : set) { // articleService.add(pojo); } return
temp = (int) dataMap.get("id"); * Result.success(); }
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();
}
/**
* *
* *
* @param searchMap * @param searchMap
* @return * @return
*/ */
@PostMapping("/search") /*
public Result<List<Song>> findSearch(@RequestBody Map searchMap) { * @PostMapping("/search") public Result<List<Song>> findSearch(@RequestBody Map
return Result.success(songService.findSearch(searchMap)); * searchMap) { return Result.success(songService.findSearch(searchMap)); }
} *
*//**
/**
* *
* *
* @param song * @param song
*/ */
@PostMapping /*
public Result<Void> add(@RequestBody Song song) { * @PostMapping public Result<Void> add(@RequestBody Song song) {
songService.add(song); * songService.add(song); return Result.success(); }
return Result.success(); *
} *//**
/**
* *
* *
* @param song * @param song
*/ */
@PutMapping("/{id}") /*
public Result<Void> update(@RequestBody Song song, @PathVariable String id) { * @PutMapping("/{id}") public Result<Void> update(@RequestBody Song
song.setId(id); * song, @PathVariable String id) { song.setId(id); songService.update(song);
songService.update(song); * return Result.success(); }
return Result.success(); *
} *//**
/**
* *
* *
* @param id * @param id
*//*
* @DeleteMapping("/{id}") public Result<Void> delete(@PathVariable String id) {
* songService.deleteById(id); return Result.success(); }
*/ */
@DeleteMapping("/{id}")
public Result<Void> delete(@PathVariable String id) {
songService.deleteById(id);
return Result.success();
}
} }

@ -53,12 +53,6 @@ public class OperationAspect {
if (null == interceptor) { if (null == interceptor) {
return; return;
} }
/**
*
*/
if (interceptor.checkLogin()) {
checkLogin();
}
/** /**
* *
@ -121,18 +115,11 @@ public class OperationAspect {
} }
} }
private void checkLogin() {
UserLoginDto userLoginDto = getUserLoginDtoFromToken();
if (userLoginDto == null) {
throw new BizException(StatusCode.UNAUTHORIZED);
}
}
private UserLoginDto getUserLoginDtoFromToken() { private UserLoginDto getUserLoginDtoFromToken() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest(); .getRequest();
String token = request.getHeader("token"); String authorization = request.getHeader("Authorization");
return jwtUtil.getUserLoginDto(token); return jwtUtil.getUserLoginDto(authorization);
} }
private void validateParams(Method method, Object[] arguments) { private void validateParams(Method method, Object[] arguments) {

@ -22,7 +22,7 @@ public class IndexController {
private CarouselService carouselService; private CarouselService carouselService;
@ApiOperation(value = "1.获取轮播图", notes = "返回5个") @ApiOperation(value = "1.获取轮播图", notes = "返回5个")
@GetMapping("/loadCarousel") @GetMapping("/carousel")
public Result<List<Carousel>> loadCarousel() { public Result<List<Carousel>> loadCarousel() {
List<Carousel> carouselList = carouselService.loadCarousel(); List<Carousel> carouselList = carouselService.loadCarousel();
return Result.success(carouselList); return Result.success(carouselList);

@ -100,7 +100,7 @@ public class LoginController extends BaseController {
return Result.success(); return Result.success();
} }
@ApiOperation(value = "2.登录/注册", notes = "成功后返回token") @ApiOperation(value = "2.登录/注册", notes = "成功后返回authorization")
@ApiImplicitParams({ @ApiImplicitParam(name = "mobile", value = "手机号", required = true), @ApiImplicitParams({ @ApiImplicitParam(name = "mobile", value = "手机号", required = true),
@ApiImplicitParam(name = "mobileCheckCode", value = "6位验证码", required = true), @ApiImplicitParam(name = "mobileCheckCode", value = "6位验证码", required = true),
@ApiImplicitParam(name = "deviceId", value = "设备id", 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天") @ApiOperation(value = "3.token续期", notes = "有效期7天")
@ApiImplicitParams({ @ApiImplicitParam(name = "deviceId", value = "设备id", required = true), @ApiImplicitParams({ @ApiImplicitParam(name = "deviceId", value = "设备id", required = true),
@ -142,10 +142,10 @@ public class LoginController extends BaseController {
@PostMapping("/autoLogin") @PostMapping("/autoLogin")
@GlobalInterceptor @GlobalInterceptor
public Result<String> autoLogin(HttpServletRequest request, public Result<String> autoLogin(HttpServletRequest request,
@RequestHeader(value = "token", required = false) String token, @RequestHeader(value = "Authorization", required = false) String authorization,
@VerifyParam(required = true) @RequestParam("deviceId") String deviceId, @VerifyParam(required = true) @RequestParam("deviceId") String deviceId,
@RequestParam("deviceBrand") String deviceBrand) { @RequestParam("deviceBrand") String deviceBrand) {
return Result.success(userInfoService.autoLogin(token, deviceId, deviceBrand, getIpAddr(request))); return Result.success(userInfoService.autoLogin(authorization, deviceId, deviceBrand, getIpAddr(request)));
} }
/** /**

@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; 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.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -74,10 +75,10 @@ public class MyController extends BaseController {
public static String UPLOAD_DIRECTORY = "user/avatar/"; public static String UPLOAD_DIRECTORY = "user/avatar/";
@ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息") @ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息")
@GetMapping("/getUserInfo") @GetMapping("/userInfo")
@GlobalInterceptor(checkAppUserLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<UserRespDTO> getUserInfo(@RequestHeader(value = "token", required = false) String token) { public Result<UserRespDTO> getUserInfo(@RequestHeader(value = "Authorization", required = false) String authorization) {
UserLoginDto userLoginDto = getUserLoginDto(token); UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) { if (null == userLoginDto) {
return Result.unauthorized(null); return Result.unauthorized(null);
} }
@ -118,11 +119,11 @@ public class MyController extends BaseController {
} }
@ApiOperation(value = "2.更新个人信息", notes = "游客无法编辑个人信息") @ApiOperation(value = "2.更新个人信息", notes = "游客无法编辑个人信息")
@PostMapping("/updateUserInfo") @PutMapping("/userInfo")
@GlobalInterceptor(checkAppUserLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<Void> updateUserInfo(@RequestHeader(value = "token", required = false) String token, public Result<Void> updateUserInfo(@RequestHeader(value = "Authorization", required = false) String authorization,
@VerifyParam(required = true) @RequestBody UserInfoUpdateDto userInfoUpdateDto) { @VerifyParam(required = true) @RequestBody UserInfoUpdateDto userInfoUpdateDto) {
UserLoginDto userLoginDto = getUserLoginDto(token); UserLoginDto userLoginDto = getUserLoginDto(authorization);
UserInfo user = userInfoService.findById(userLoginDto.getUserId()); UserInfo user = userInfoService.findById(userLoginDto.getUserId());
String nickName = userInfoUpdateDto.getNickName(); String nickName = userInfoUpdateDto.getNickName();
if (!StringTools.isEmpty(nickName) && !nickName.equals(user.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/") @ApiOperation(value = "3.上传个人头像", notes = "图片压缩为70X70 JPEG存入S3桶为indie目录为 user/avatar/")
@PostMapping("/uploadAvatar") @PostMapping("/avatar")
@GlobalInterceptor(checkLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<String> uploadAvatar(@RequestHeader(value = "token", required = false) String token, public Result<String> uploadAvatar(@RequestHeader(value = "Authorization", required = false) String authorization,
@VerifyParam(required = true) MultipartFile file) throws IOException { @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); byte[] thumbnail = ScaleFilter.createThumbnail(file.getInputStream(), Constants.LENGTH_70, Constants.LENGTH_70);
String avatarName = userLoginDto.getUserId() + "_" + idWorker.nextId() String avatarName = userLoginDto.getUserId() + "_" + idWorker.nextId()
@ -171,9 +172,10 @@ public class MyController extends BaseController {
} }
@ApiOperation(value = "4.查看他人信息", notes = "游客无法查看他人信息") @ApiOperation(value = "4.查看他人信息", notes = "游客无法查看他人信息")
@GetMapping("/getOtherUserInfo/{userId}") @GetMapping("/otherUserInfo/{userId}")
@GlobalInterceptor(checkAppUserLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<UserRespDTO> getOtherUserInfo(@VerifyParam(required = true) @PathVariable String userId) { public Result<UserRespDTO> getOtherUserInfo(@RequestHeader(value = "Authorization", required = false) String authorization,
@VerifyParam(required = true) @PathVariable String userId) {
UserInfo user = userInfoService.findById(userId); UserInfo user = userInfoService.findById(userId);
if (null == user) { if (null == user) {
return Result.failed(StatusCode.USER_INVALID_USER_ID); return Result.failed(StatusCode.USER_INVALID_USER_ID);

@ -15,7 +15,7 @@ import util.JwtUtil;
@Api(tags = "UserCollectController") @Api(tags = "UserCollectController")
@RestController @RestController
@RequestMapping("/userCollect") @RequestMapping("/collect")
public class UserCollectController { public class UserCollectController {
@Autowired @Autowired
private JwtUtil jwtUtil; private JwtUtil jwtUtil;
@ -29,12 +29,12 @@ public class UserCollectController {
//@ApiImplicitParam(name = "collectType", value = "0:歌曲1:期刊2:关注3:黑名单4:点赞", required = true) }) //@ApiImplicitParam(name = "collectType", value = "0:歌曲1:期刊2:关注3:黑名单4:点赞", required = true) })
@ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊的id", required = true), @ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊的id", required = true),
@ApiImplicitParam(name = "collectType", value = "0:歌曲1:期刊", required = true) }) @ApiImplicitParam(name = "collectType", value = "0:歌曲1:期刊", required = true) })
@PostMapping("/addCollect") @PostMapping
@GlobalInterceptor(checkLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<Void> addCollect(@RequestHeader(value = "token", required = false) String token, public Result<Void> addCollect(@RequestHeader(value = "Authorization", required = false) String authorization,
@VerifyParam(required = true) @RequestParam("objectId") String objectId, @VerifyParam(required = true) @RequestParam("objectId") String objectId,
@VerifyParam(required = true) @RequestParam("collectType") Integer collectType) { @VerifyParam(required = true) @RequestParam("collectType") Integer collectType) {
UserLoginDto loginDto = jwtUtil.getUserLoginDto(token); UserLoginDto loginDto = jwtUtil.getUserLoginDto(authorization);
userCollectService.saveCollect(loginDto.getUserId(), objectId, collectType); userCollectService.saveCollect(loginDto.getUserId(), objectId, collectType);
return Result.success(); return Result.success();
} }
@ -46,12 +46,12 @@ public class UserCollectController {
//@ApiImplicitParam(name = "collectType", value = "0:歌曲1:期刊2:关注3:黑名单4:点赞", required = true) }) //@ApiImplicitParam(name = "collectType", value = "0:歌曲1:期刊2:关注3:黑名单4:点赞", required = true) })
@ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊的id", required = true), @ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊的id", required = true),
@ApiImplicitParam(name = "collectType", value = "0:歌曲1:期刊", required = true) }) @ApiImplicitParam(name = "collectType", value = "0:歌曲1:期刊", required = true) })
@DeleteMapping("/cancelCollect") @DeleteMapping
@GlobalInterceptor(checkLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<Void> cancelCollect(@RequestHeader(value = "token", required = false) String token, public Result<Void> cancelCollect(@RequestHeader(value = "Authorization", required = false) String authorization,
@VerifyParam(required = true) @RequestParam("objectId") String objectId, @VerifyParam(required = true) @RequestParam("objectId") String objectId,
@VerifyParam(required = true) @RequestParam("collectType") Integer collectType) { @VerifyParam(required = true) @RequestParam("collectType") Integer collectType) {
UserLoginDto loginDto = jwtUtil.getUserLoginDto(token); UserLoginDto loginDto = jwtUtil.getUserLoginDto(authorization);
userCollectService.deleteByUserIdAndObjectIdAndCollectType(loginDto.getUserId(), objectId, userCollectService.deleteByUserIdAndObjectIdAndCollectType(loginDto.getUserId(), objectId,
collectType); collectType);
return Result.success(); return Result.success();

@ -31,11 +31,9 @@ public class UserRespDTO {
@ApiModelProperty(value = "喜欢歌曲数") @ApiModelProperty(value = "喜欢歌曲数")
private int songCount; private int songCount;
private Set songSet;
@ApiModelProperty(value = "收藏期刊数") @ApiModelProperty(value = "收藏期刊数")
private int journalCount; private int journalCount;
private Set journalSet;
@ApiModelProperty(value = "获得评论数") @ApiModelProperty(value = "获得评论数")
private int commentReplyCount; private int commentReplyCount;
@ApiModelProperty(value = "生日,格式为: yyyy.MM.dd") @ApiModelProperty(value = "生日,格式为: yyyy.MM.dd")

@ -266,8 +266,8 @@ public class UserInfoService {
return userInfo; return userInfo;
} }
public String autoLogin(String token, String deviceId, String deviceBrand, String ip) { public String autoLogin(String authorization, String deviceId, String deviceBrand, String ip) {
UserLoginDto userLoginDto=jwtUtil.getUserLoginDto(token); UserLoginDto userLoginDto=jwtUtil.getUserLoginDto(authorization);
if (null==userLoginDto) { if (null==userLoginDto) {
return null; return null;
} }
@ -280,7 +280,7 @@ public class UserInfoService {
userInfo.setLastUseDeviceId(deviceId); userInfo.setLastUseDeviceId(deviceId);
userInfo.setLastUseDeviceBrand(deviceBrand); userInfo.setLastUseDeviceBrand(deviceBrand);
userInfoDao.save(userInfo); 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) { public long countByNickName(String nickName) {
@ -304,7 +304,7 @@ public class UserInfoService {
} }
user.setLastLoginTime(new Date()); user.setLastLoginTime(new Date());
userInfoDao.save(user); 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<UserInfo> orderByField(List<String> idList) { public List<UserInfo> orderByField(List<String> idList) {

Loading…
Cancel
Save