parent
f8cd2a11f8
commit
155c336835
@ -0,0 +1,29 @@
|
|||||||
|
package com.unilife.config;
|
||||||
|
|
||||||
|
import com.unilife.interceptor.JwtInterceptor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebMvcConfig implements WebMvcConfigurer {
|
||||||
|
@Autowired
|
||||||
|
private JwtInterceptor jwtInterceptor;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(jwtInterceptor).addPathPatterns("/**")
|
||||||
|
.excludePathPatterns(
|
||||||
|
"/users/login",
|
||||||
|
"/users/register",
|
||||||
|
"/users/code",
|
||||||
|
"/users/login/code",
|
||||||
|
"/swagger-resources/**",
|
||||||
|
"/v2/api-docs/**",
|
||||||
|
"/doc.html",
|
||||||
|
"/webjars/**"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
package com.unilife.interceptor;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.unilife.utils.BaseContext;
|
||||||
|
import com.unilife.utils.JwtUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class JwtInterceptor implements HandlerInterceptor {
|
||||||
|
@Autowired
|
||||||
|
private JwtUtil jwtUtil;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StringRedisTemplate stringRedisTemplate;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
|
||||||
|
log.info("JwtInterceptor preHandle");
|
||||||
|
String token = request.getHeader("Authorization");
|
||||||
|
|
||||||
|
if(StrUtil.isBlank(token)){
|
||||||
|
response.setStatus(401);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean verified = jwtUtil.verifyToken(token);
|
||||||
|
if (!verified) {
|
||||||
|
response.setStatus(401);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//从token中获取userid并存入threadlocal
|
||||||
|
Long userId = jwtUtil.getUserIdFromToken(token);
|
||||||
|
BaseContext.setId(userId);
|
||||||
|
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||||
|
BaseContext.removeId();
|
||||||
|
}
|
||||||
|
}
|
@ -1,19 +1,14 @@
|
|||||||
package com.unilife.model.dto;
|
package com.unilife.model.dto;
|
||||||
//这个是注册的DTO
|
//这个才是登录的DTO
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public class LoginDTO {
|
public class LoginDTO {
|
||||||
private String username;
|
|
||||||
private String email;
|
private String email;
|
||||||
private String password;
|
private String password;
|
||||||
private String nickname;
|
|
||||||
private String studentId;
|
|
||||||
private String department;
|
|
||||||
private String major;
|
|
||||||
private String grade;
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.unilife.model.dto;
|
||||||
|
//这个是注册的DTO
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class RegisterDTO {
|
||||||
|
private String username;
|
||||||
|
private String email;
|
||||||
|
private String password;
|
||||||
|
private String nickname;
|
||||||
|
private String studentId;
|
||||||
|
private String department;
|
||||||
|
private String major;
|
||||||
|
private String grade;
|
||||||
|
}
|
@ -1,27 +0,0 @@
|
|||||||
package com.unilife.model.vo;
|
|
||||||
|
|
||||||
import com.unilife.model.entity.User;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class LogVO {
|
|
||||||
private Integer id;
|
|
||||||
private String username;
|
|
||||||
private String nickname;
|
|
||||||
private String avatar;
|
|
||||||
private Byte role;
|
|
||||||
private Byte isVerified;
|
|
||||||
private Byte status;
|
|
||||||
|
|
||||||
public LogVO(Integer id,String username,String nickname,String avatar,Byte role,Byte isVerified,Byte status)
|
|
||||||
{
|
|
||||||
this.id = id;
|
|
||||||
this.username = username;
|
|
||||||
this.nickname = nickname;
|
|
||||||
this.avatar = avatar;
|
|
||||||
this.role = role;
|
|
||||||
this.isVerified = isVerified;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,17 +1,21 @@
|
|||||||
package com.unilife.model.vo;
|
package com.unilife.model.vo;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
public class LoginVO {
|
public class LoginVO {
|
||||||
private Integer id;
|
private Long id;
|
||||||
private String username;
|
private String username;
|
||||||
private String nickname;
|
private String nickname;
|
||||||
|
private String avatar;
|
||||||
|
private Byte role;
|
||||||
|
private Byte isVerified;
|
||||||
|
private Byte status;
|
||||||
|
private String token;
|
||||||
|
|
||||||
public LoginVO(Integer id, String username, String nickname)
|
|
||||||
{
|
|
||||||
this.id = id;
|
|
||||||
this.username = username;
|
|
||||||
this.nickname = nickname;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
package com.unilife.model.dto;
|
package com.unilife.model.vo;
|
||||||
//这个才是登录的DTO
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class LogDTO {
|
@NoArgsConstructor
|
||||||
private String email;
|
public class RegisterVO {
|
||||||
private String password;
|
private Long id;
|
||||||
|
private String username;
|
||||||
|
private String nickname;
|
||||||
|
|
||||||
}
|
}
|
@ -1,18 +1,16 @@
|
|||||||
package com.unilife.service;
|
package com.unilife.service;
|
||||||
|
|
||||||
import com.unilife.common.result.Result;
|
import com.unilife.common.result.Result;
|
||||||
import com.unilife.model.dto.LogDTO;
|
|
||||||
import com.unilife.model.dto.LoginDTO;
|
import com.unilife.model.dto.LoginDTO;
|
||||||
import com.unilife.model.dto.LoginEmailDTO;
|
import com.unilife.model.dto.LoginEmailDTO;
|
||||||
|
import com.unilife.model.dto.RegisterDTO;
|
||||||
|
|
||||||
|
|
||||||
public interface UserService {
|
public interface UserService {
|
||||||
Result register(LoginDTO loginDTO);
|
Result register(RegisterDTO registerDTO);
|
||||||
Result login(LogDTO logDTO);
|
Result login(LoginDTO loginDTO);
|
||||||
|
|
||||||
Result sendVerificationCode(String email);
|
Result sendVerificationCode(String email);
|
||||||
|
|
||||||
Result loginWithEmail(LoginEmailDTO loginEmailDTO);
|
Result loginWithEmail(LoginEmailDTO loginEmailDTO);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.unilife.utils;
|
||||||
|
|
||||||
|
public class BaseContext {
|
||||||
|
|
||||||
|
public static ThreadLocal<Long> threadLocal = new ThreadLocal<>();
|
||||||
|
|
||||||
|
public static void setId(Long id) {
|
||||||
|
threadLocal.set(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Long getId() {
|
||||||
|
return threadLocal.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeId() {
|
||||||
|
threadLocal.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.unilife.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateTime;
|
||||||
|
import cn.hutool.jwt.JWTUtil;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class JwtUtil {
|
||||||
|
@Value("${jwt.secret}")
|
||||||
|
private String secret;
|
||||||
|
|
||||||
|
@Value("${jwt.expiration}")
|
||||||
|
public long expiration;
|
||||||
|
|
||||||
|
public String generateToken(Long id) {
|
||||||
|
DateTime now = DateTime.now();
|
||||||
|
DateTime expireTime = new DateTime(now.getTime() + expiration * 1000);
|
||||||
|
|
||||||
|
Map<String, Object> payload = new HashMap<>();
|
||||||
|
payload.put("userId", id);
|
||||||
|
payload.put("created",now.getTime());
|
||||||
|
return JWTUtil.createToken(payload,secret.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean verifyToken(String token) {
|
||||||
|
try{
|
||||||
|
JWTUtil.verify(token,secret.getBytes());
|
||||||
|
return true;
|
||||||
|
}catch (Exception e){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public Long getUserIdFromToken(String token) {
|
||||||
|
try {
|
||||||
|
return (Long)JWTUtil.parseToken(token).getPayload("userId");
|
||||||
|
}catch (Exception e){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue