parent
044477e2de
commit
1790b245fe
|
@ -1,43 +0,0 @@
|
||||||
package com.guwan.backend.common;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
import javax.servlet.*;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class CorsConfig implements Filter {
|
|
||||||
@Override
|
|
||||||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
|
|
||||||
HttpServletResponse response = (HttpServletResponse) res;
|
|
||||||
HttpServletRequest request = (HttpServletRequest) req;
|
|
||||||
|
|
||||||
String origin = request.getHeader("Origin");
|
|
||||||
if(origin!=null) {
|
|
||||||
response.setHeader("Access-Control-Allow-Origin", origin);
|
|
||||||
}
|
|
||||||
|
|
||||||
String headers = request.getHeader("Access-Control-Request-Headers");
|
|
||||||
if(headers!=null) {
|
|
||||||
response.setHeader("Access-Control-Allow-Headers", headers);
|
|
||||||
response.setHeader("Access-Control-Expose-Headers", headers);
|
|
||||||
}
|
|
||||||
|
|
||||||
response.setHeader("Access-Control-Allow-Methods", "*");
|
|
||||||
response.setHeader("Access-Control-Max-Age", "3600");
|
|
||||||
response.setHeader("Access-Control-Allow-Credentials", "true");
|
|
||||||
|
|
||||||
chain.doFilter(request, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(FilterConfig filterConfig) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,15 +2,18 @@ package com.guwan.backend.common;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class Result<T> {
|
public class Result<T> {
|
||||||
private Integer code;
|
private Integer code;
|
||||||
private String message;
|
private String message;
|
||||||
private T data;
|
private T data;
|
||||||
private Long timestamp;
|
private String time;
|
||||||
|
|
||||||
public Result() {
|
public Result() {
|
||||||
this.timestamp = System.currentTimeMillis();
|
this.time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> Result<T> success() {
|
public static <T> Result<T> success() {
|
||||||
|
|
|
@ -7,19 +7,23 @@ import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
import org.springframework.web.filter.CorsFilter;
|
import org.springframework.web.filter.CorsFilter;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class CorsConfig {
|
public class CorsConfig {
|
||||||
|
|
||||||
|
//get请求变成了options 后端预检过不了怎么改
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public CorsFilter corsFilter() {
|
public CorsFilter corsFilter() {
|
||||||
CorsConfiguration config = new CorsConfiguration();
|
CorsConfiguration config = new CorsConfiguration();
|
||||||
config.addAllowedOriginPattern("*");
|
config.addAllowedOriginPattern("*"); // 允许所有域
|
||||||
config.setAllowCredentials(true);
|
config.setAllowCredentials(true); // 允许凭证
|
||||||
config.addAllowedMethod("*");
|
config.addAllowedMethod("*"); // 允许所有方法
|
||||||
config.addAllowedHeader("*");
|
config.addAllowedHeader("*"); // 允许所有头
|
||||||
|
|
||||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||||
source.registerCorsConfiguration("/**", config);
|
source.registerCorsConfiguration("/**", config); // 对所有路径应用配置
|
||||||
|
|
||||||
return new CorsFilter(source);
|
return new CorsFilter(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -10,6 +10,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
@ -60,6 +61,9 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// 验证token
|
// 验证token
|
||||||
String authHeader = request.getHeader("Authorization");
|
String authHeader = request.getHeader("Authorization");
|
||||||
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
|
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
|
||||||
|
@ -114,7 +118,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
UsernamePasswordAuthenticationToken authentication =
|
UsernamePasswordAuthenticationToken authentication =
|
||||||
new UsernamePasswordAuthenticationToken(
|
new UsernamePasswordAuthenticationToken(
|
||||||
userDetails, null, userDetails.getAuthorities());
|
userDetails, userDetails.getPassword(), userDetails.getAuthorities());
|
||||||
authentication.setDetails(
|
authentication.setDetails(
|
||||||
new WebAuthenticationDetailsSource().buildDetails(request));
|
new WebAuthenticationDetailsSource().buildDetails(request));
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
package com.guwan.backend.security;
|
package com.guwan.backend.security;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.guwan.backend.dto.user.UserDTO;
|
|
||||||
import com.guwan.backend.entity.User;
|
import com.guwan.backend.entity.User;
|
||||||
import com.guwan.backend.mapper.UserMapper;
|
import com.guwan.backend.mapper.UserMapper;
|
||||||
import com.guwan.backend.security.CustomUserDetails;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
@ -50,7 +48,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||||
return new CustomUserDetails(
|
return new CustomUserDetails(
|
||||||
user.getId(),
|
user.getId(),
|
||||||
user.getUsername(),
|
user.getUsername(),
|
||||||
null,
|
user.getPassword(),
|
||||||
authorities,
|
authorities,
|
||||||
user.getStatus() == 1
|
user.getStatus() == 1
|
||||||
);
|
);
|
||||||
|
|
|
@ -19,7 +19,7 @@ public interface UserService {
|
||||||
*/
|
*/
|
||||||
UserDTO login(LoginDto request);
|
UserDTO login(LoginDto request);
|
||||||
UserDTO getCurrentUser();
|
UserDTO getCurrentUser();
|
||||||
Long getCurrentUserId();
|
|
||||||
UserDTO getUserById(Long id);
|
UserDTO getUserById(Long id);
|
||||||
UserDTO updateUserInfo(UserDTO userDTO);
|
UserDTO updateUserInfo(UserDTO userDTO);
|
||||||
|
|
||||||
|
|
|
@ -7,25 +7,19 @@ import com.guwan.backend.dto.user.RegisterDTO;
|
||||||
import com.guwan.backend.dto.user.UserDTO;
|
import com.guwan.backend.dto.user.UserDTO;
|
||||||
import com.guwan.backend.entity.User;
|
import com.guwan.backend.entity.User;
|
||||||
import com.guwan.backend.mapper.UserMapper;
|
import com.guwan.backend.mapper.UserMapper;
|
||||||
import com.guwan.backend.security.CustomUserDetails;
|
|
||||||
import com.guwan.backend.service.EmailService;
|
import com.guwan.backend.service.EmailService;
|
||||||
import com.guwan.backend.service.UserService;
|
import com.guwan.backend.service.UserService;
|
||||||
import com.guwan.backend.util.JwtUtil;
|
import com.guwan.backend.util.JwtUtil;
|
||||||
import com.guwan.backend.util.RedisUtil;
|
|
||||||
import com.guwan.backend.util.RedisUtils;
|
import com.guwan.backend.util.RedisUtils;
|
||||||
import com.guwan.backend.util.SecurityUtil;
|
import com.guwan.backend.util.SecurityUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
|
||||||
import org.springframework.security.core.Authentication;
|
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
@ -35,8 +29,7 @@ public class UserServiceImpl implements UserService {
|
||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
private final JwtUtil jwtUtil;
|
private final JwtUtil jwtUtil;
|
||||||
private final RedisUtil redisUtil;
|
private final RedisUtils redisUtil;
|
||||||
private final RedisUtils redisUtils;
|
|
||||||
private final EmailService emailService;
|
private final EmailService emailService;
|
||||||
private final SecurityUtil securityUtil;
|
private final SecurityUtil securityUtil;
|
||||||
|
|
||||||
|
@ -86,8 +79,8 @@ public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
userMapper.insert(user);
|
userMapper.insert(user);
|
||||||
|
|
||||||
redisUtil.delete(request.getEmail());
|
redisUtil.del(request.getEmail());
|
||||||
redisUtil.delete(request.getPhone());
|
redisUtil.del(request.getPhone());
|
||||||
|
|
||||||
return convertToDTO(user);
|
return convertToDTO(user);
|
||||||
}
|
}
|
||||||
|
@ -114,7 +107,7 @@ public class UserServiceImpl implements UserService {
|
||||||
if (!request.getCode().equals(redisPhoneCode)){
|
if (!request.getCode().equals(redisPhoneCode)){
|
||||||
throw new IllegalArgumentException("验证码错误");
|
throw new IllegalArgumentException("验证码错误");
|
||||||
}else {
|
}else {
|
||||||
redisUtil.delete(request.getPhone());
|
redisUtil.del(request.getPhone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,7 +120,7 @@ public class UserServiceImpl implements UserService {
|
||||||
if (!request.getCode().equals(redisEmailCode)){
|
if (!request.getCode().equals(redisEmailCode)){
|
||||||
throw new IllegalArgumentException("验证码错误");
|
throw new IllegalArgumentException("验证码错误");
|
||||||
}else {
|
}else {
|
||||||
redisUtil.delete(request.getEmail());
|
redisUtil.del(request.getEmail());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,25 +139,23 @@ public class UserServiceImpl implements UserService {
|
||||||
userDTO.setToken(token);
|
userDTO.setToken(token);
|
||||||
|
|
||||||
// 缓存用户信息
|
// 缓存用户信息
|
||||||
redisUtils.set(USER_CACHE_KEY + user.getId(), userDTO, USER_CACHE_DURATION);
|
redisUtil.set(USER_CACHE_KEY + user.getId(), userDTO, USER_CACHE_DURATION);
|
||||||
|
|
||||||
return userDTO;
|
return userDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@OperationLog(description = "获取用户信息", operationType = "获取")
|
||||||
public UserDTO getCurrentUser() {
|
public UserDTO getCurrentUser() {
|
||||||
Long userId = getCurrentUserId();
|
Long userId = securityUtil.getCurrentUserId();
|
||||||
if (userId == null) {
|
if (userId == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return getUserById(userId);
|
return getUserById(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Long getCurrentUserId() {
|
|
||||||
return securityUtil.getCurrentUserId();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserDTO getUserById(Long id) {
|
public UserDTO getUserById(Long id) {
|
||||||
|
@ -180,7 +171,7 @@ public class UserServiceImpl implements UserService {
|
||||||
}
|
}
|
||||||
|
|
||||||
UserDTO userDTO = convertToDTO(user);
|
UserDTO userDTO = convertToDTO(user);
|
||||||
redisUtil.set(USER_CACHE_KEY + id, userDTO, USER_CACHE_DURATION, TimeUnit.SECONDS);
|
redisUtil.set(USER_CACHE_KEY + id, userDTO, USER_CACHE_DURATION);
|
||||||
return userDTO;
|
return userDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,120 +0,0 @@
|
||||||
package com.guwan.backend.util;
|
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Component
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class RedisUtil {
|
|
||||||
|
|
||||||
private final RedisTemplate<String, Object> redisTemplate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置缓存
|
|
||||||
*/
|
|
||||||
public void set(String key, Object value, long timeout, TimeUnit timeUnit) {
|
|
||||||
try {
|
|
||||||
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Redis set error: ", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置缓存(默认过期时间)
|
|
||||||
*/
|
|
||||||
public void set(String key, Object value) {
|
|
||||||
set(key, value, 24, TimeUnit.HOURS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取缓存
|
|
||||||
*/
|
|
||||||
public Object get(String key) {
|
|
||||||
try {
|
|
||||||
return redisTemplate.opsForValue().get(key);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Redis get error: ", e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除缓存
|
|
||||||
*/
|
|
||||||
public void delete(String key) {
|
|
||||||
try {
|
|
||||||
redisTemplate.delete(key);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Redis delete error: ", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断key是否存在
|
|
||||||
*/
|
|
||||||
public boolean hasKey(String key) {
|
|
||||||
try {
|
|
||||||
return Boolean.TRUE.equals(redisTemplate.hasKey(key));
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Redis hasKey error: ", e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置过期时间
|
|
||||||
*/
|
|
||||||
public void expire(String key, long timeout, TimeUnit timeUnit) {
|
|
||||||
try {
|
|
||||||
redisTemplate.expire(key, timeout, timeUnit);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Redis expire error: ", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 自增操作
|
|
||||||
*/
|
|
||||||
public Long increment(String key, long delta) {
|
|
||||||
try {
|
|
||||||
return redisTemplate.opsForValue().increment(key, delta);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Redis increment error: ", e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set集合操作
|
|
||||||
*/
|
|
||||||
public void sAdd(String key, String... values) {
|
|
||||||
try {
|
|
||||||
redisTemplate.opsForSet().add(key, values);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Redis sAdd error: ", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRemove(String key, Object... values) {
|
|
||||||
try {
|
|
||||||
redisTemplate.opsForSet().remove(key, values);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Redis setRemove error: ", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean sHasKey(String key, Object value) {
|
|
||||||
try {
|
|
||||||
return Boolean.TRUE.equals(redisTemplate.opsForSet().isMember(key, value));
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Redis sHasKey error: ", e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue