格式化代码

This commit is contained in:
石皮幼鸟 2024-04-10 15:15:36 +08:00
parent 71a01cfd87
commit 73b2ddae7d
16 changed files with 112 additions and 90 deletions

View File

@ -17,8 +17,8 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
public class RedisConfig {
@Bean
@ConditionalOnMissingBean(name = "redis template")
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<Object,Object> template = new RedisTemplate<>();
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
//使用fastjson序列化
FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
//value值的序列化采用fastJsonRedisSerializer
@ -33,7 +33,7 @@ public class RedisConfig {
@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory){
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;

View File

@ -1,8 +1,10 @@
package com.example.catchTheLetters.config;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
@ -18,7 +20,7 @@ public class WebMvcConfig {
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(org.springframework.web.servlet.config.annotation.CorsRegistry registry) {
public void addCorsMappings(@NotNull CorsRegistry registry) {
registry
.addMapping("/**")
.allowedOriginPatterns("*") // 允许所有域

View File

@ -11,7 +11,7 @@ public interface CommonConstant {
Integer TOKEN_EXPIRE_TIME = 7200000;
Integer REDIS_EXPIRE_TIME = 7*24*60*60*1000;
Integer REDIS_EXPIRE_TIME = 7 * 24 * 60 * 60 * 1000;
String EMAIL_REGEX = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";

View File

@ -13,13 +13,13 @@ import lombok.Getter;
@AllArgsConstructor
public enum HttpStatus {
SUCCESS(200,"操作成功"),
SUCCESS(200, "操作成功"),
BAD_REQUEST(400,"参数列表错误"),
BAD_REQUEST(400, "参数列表错误"),
TOKEN_EXCEPTION(401,"token验证错误"),
TOKEN_EXCEPTION(401, "token验证错误"),
ERROR(500,"系统内部错误");
ERROR(500, "系统内部错误");
private final Integer code;

View File

@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.*;
/**
* 认证控制器
*
* @author spyn
*/
@RestController
@ -53,8 +54,8 @@ public class AuthController {
@ApiOperation(value = "用户信息查询不会返回密码查不出status为0的用户")
@GetMapping("/info")
@ApiParam(name = "id", value = "用户ID不传就是拿token查自己")
public R<User> info(@RequestParam(required = false) Integer id, @RequestHeader("token")String token) {
if (id != null){
public R<User> info(@RequestParam(required = false) Integer id, @RequestHeader("token") String token) {
if (id != null) {
return R.ok(authService.getUserInfo(id));
}
return R.ok(authService.verify(token));

View File

@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.*;
/**
* 关卡控制器
*
* @author spyn
*/
@RestController

View File

@ -7,6 +7,7 @@ import java.util.List;
/**
* 关卡类
*
* @author spyn
*/
@Data

View File

@ -6,6 +6,7 @@ import java.io.Serializable;
/**
* 关卡结算信息类
*
* @author spyn
*/
@Data

View File

@ -7,6 +7,7 @@ import java.io.Serializable;
/**
* 用户类
*
* @author spyn
*/
@Data
@ -68,7 +69,7 @@ public class User implements Serializable {
*/
private Integer progress;
public UserVo toVo(){
public UserVo toVo() {
UserVo userVo = new UserVo();
userVo.setId(this.getId());
userVo.setUsername(this.getUsername());

View File

@ -6,6 +6,7 @@ import java.io.Serializable;
/**
* 数据表版本类用于Realm对MongoDB的数据表版本进行同步检测版本号不同则进行同步
*
* @author spyn
*/
@Data

View File

@ -6,6 +6,7 @@ import java.io.Serializable;
/**
* 登录信息类
*
* @author spyn
*/
@Data

View File

@ -6,6 +6,7 @@ import java.io.Serializable;
/**
* 注册信息类
*
* @author spyn
*/
@Data

View File

@ -8,11 +8,13 @@ import com.example.catchTheLetters.utils.R;
/**
* 认证服务接口
*
* @author spyn
*/
public interface AuthService {
/**
* 登录
*
* @param loginDto 登录信息
* @return token
*/
@ -20,6 +22,7 @@ public interface AuthService {
/**
* 注销
*
* @param token token
* @return 是否成功
*/
@ -27,6 +30,7 @@ public interface AuthService {
/**
* 验证token
*
* @param token token
* @return 用户信息不可以包含密码
*/
@ -34,6 +38,7 @@ public interface AuthService {
/**
* 注册
*
* @param registerDto 注册信息
* @return token
*/
@ -41,6 +46,7 @@ public interface AuthService {
/**
* 获取用户信息
*
* @param id 用户id
* @return 用户信息
*/
@ -48,6 +54,7 @@ public interface AuthService {
/**
* 通过用户名获取信息
*
* @param username 用户名
* @return 用户信息
*/

View File

@ -42,21 +42,21 @@ public class AuthServiceImpl implements AuthService {
var patternPhone = Pattern.compile(CommonConstant.PHONE_REGEX);
var matcherPhone = patternPhone.matcher(username);
User user;
if (matcherEmail.matches()){
if (matcherEmail.matches()) {
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.EMAIL).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
}else if (matcherPhone.matches()){
} else if (matcherPhone.matches()) {
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.PHONE).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
}else {
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.USERNAME).is(username).and(CommonConstant.STATUS).ne(0)),User.class);
} else {
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.USERNAME).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
}
if (Objects.isNull(user)){
if (Objects.isNull(user)) {
return R.fail("用户不存在");
}
if (!user.getPassword().equals(loginDto.getPassword())){
if (!user.getPassword().equals(loginDto.getPassword())) {
return R.fail("密码错误");
}
Map<String, String> map = new HashMap<>();
map.put("id",user.getId().toString());
map.put("id", user.getId().toString());
var token = JwtUtil.getToken(map, CommonConstant.TOKEN_EXPIRE_TIME);
redisService.hSet(token, user.getId().toString(), CommonConstant.REDIS_EXPIRE_TIME);
return R.ok(token);
@ -74,8 +74,8 @@ public class AuthServiceImpl implements AuthService {
if (map != null) {
id = map.get("id").asString().replaceAll("\"", "");
}
var user = mongoTemplate.findOne(new Query(Criteria.where("id").is(id).and(CommonConstant.STATUS).ne(0)),User.class);
if (Objects.isNull(user)){
var user = mongoTemplate.findOne(new Query(Criteria.where("id").is(id).and(CommonConstant.STATUS).ne(0)), User.class);
if (Objects.isNull(user)) {
return null;
}
return user;
@ -93,20 +93,20 @@ public class AuthServiceImpl implements AuthService {
user.setUsername(registerDto.getUsername());
user.setPassword(registerDto.getPassword());
var insert = mongoTemplate.insert(user);
if (insert.getId() != null){
if (insert.getId() != null) {
return R.ok(user.toVo());
}else {
} else {
return R.fail("注册失败,请重试");
}
}
@Override
public User getUserInfo(Integer id) {
return mongoTemplate.findOne(new Query(Criteria.where("id").is(id).and(CommonConstant.STATUS).ne(0)),User.class);
return mongoTemplate.findOne(new Query(Criteria.where("id").is(id).and(CommonConstant.STATUS).ne(0)), User.class);
}
@Override
public User getUserByName(String username) {
return mongoTemplate.findOne(new Query(Criteria.where("username").is(username).and(CommonConstant.STATUS).ne(0)),User.class);
return mongoTemplate.findOne(new Query(Criteria.where("username").is(username).and(CommonConstant.STATUS).ne(0)), User.class);
}
}

View File

@ -22,16 +22,18 @@ import java.util.Map;
@Component
public class JwtUtil {
public static final Integer EXPIRE_TIME = 1; // 1天
/**
* 生成token
*
* @param map 传入map
* @param expired token存储时间
* @return token
*/
public static String getToken(Map<String,String> map, Integer expired){
public static String getToken(Map<String, String> map, Integer expired) {
Calendar instance = Calendar.getInstance();
//设置过期时间单位
instance.add(Calendar.DATE,expired);
instance.add(Calendar.DATE, expired);
//创建JWT builder
JWTCreator.Builder builder = JWT.create();
//payload
@ -44,28 +46,30 @@ public class JwtUtil {
/**
* 验证token合法性 || 获取token信息方法
*
* @param token 传入token
* @return
*/
public static DecodedJWT verify(String token){
public static DecodedJWT verify(String token) {
return JWT.require(Algorithm.HMAC256(CommonConstant.TOKEN_SECRET)).build().verify(token);
}
/**
* 获取payload
*
* @param token 传入token
* @return 返回payload
*/
public static Map<String, Claim> getPayload(String token){
Map<String,Claim> claims = null;
try{
public static Map<String, Claim> getPayload(String token) {
Map<String, Claim> claims = null;
try {
claims = JWT.require(Algorithm.HMAC256(CommonConstant.TOKEN_SECRET)).build().verify(token).getClaims();
} catch (Exception e){
} catch (Exception e) {
e.printStackTrace();
}
try {
return claims;
} catch (NullPointerException e){
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
@ -74,19 +78,20 @@ public class JwtUtil {
/**
* 获取过期时间
*/
public static Date getExpiresAt(String token){
public static Date getExpiresAt(String token) {
return JwtUtil.getExpiresAt(token);
}
/**
* 判断token是否过期
*
* @return true:过期 false:没过期
*/
public Boolean isTokenExpired(String token){
public Boolean isTokenExpired(String token) {
try {
Date expiration = JwtUtil.getExpiresAt(token);
return expiration.before(new Date());
} catch (TokenExpiredException e){
} catch (TokenExpiredException e) {
return true;
}
}