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 java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class Result<T> {
|
||||
private Integer code;
|
||||
private String message;
|
||||
private T data;
|
||||
private Long timestamp;
|
||||
private String time;
|
||||
|
||||
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() {
|
||||
|
|
|
@ -7,19 +7,23 @@ import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|||
import org.springframework.web.filter.CorsFilter;
|
||||
|
||||
@Configuration
|
||||
public class CorsConfig {
|
||||
|
||||
public class CorsConfig {
|
||||
|
||||
//get请求变成了options 后端预检过不了怎么改
|
||||
|
||||
@Bean
|
||||
public CorsFilter corsFilter() {
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
config.addAllowedOriginPattern("*");
|
||||
config.setAllowCredentials(true);
|
||||
config.addAllowedMethod("*");
|
||||
config.addAllowedHeader("*");
|
||||
|
||||
config.addAllowedOriginPattern("*"); // 允许所有域
|
||||
config.setAllowCredentials(true); // 允许凭证
|
||||
config.addAllowedMethod("*"); // 允许所有方法
|
||||
config.addAllowedHeader("*"); // 允许所有头
|
||||
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
|
||||
source.registerCorsConfiguration("/**", config); // 对所有路径应用配置
|
||||
|
||||
return new CorsFilter(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -10,6 +10,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
|||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
@ -60,6 +61,9 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||
}
|
||||
|
||||
try {
|
||||
if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
|
||||
return;
|
||||
}
|
||||
// 验证token
|
||||
String authHeader = request.getHeader("Authorization");
|
||||
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
|
||||
|
@ -114,7 +118,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||
|
||||
UsernamePasswordAuthenticationToken authentication =
|
||||
new UsernamePasswordAuthenticationToken(
|
||||
userDetails, null, userDetails.getAuthorities());
|
||||
userDetails, userDetails.getPassword(), userDetails.getAuthorities());
|
||||
authentication.setDetails(
|
||||
new WebAuthenticationDetailsSource().buildDetails(request));
|
||||
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
package com.guwan.backend.security;
|
||||
|
||||
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.mapper.UserMapper;
|
||||
import com.guwan.backend.security.CustomUserDetails;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
@ -27,11 +25,11 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||
new LambdaQueryWrapper<User>()
|
||||
.eq(User::getUsername, username)
|
||||
);
|
||||
|
||||
|
||||
if (user == null) {
|
||||
throw new UsernameNotFoundException("用户不存在");
|
||||
}
|
||||
|
||||
|
||||
return convertToUserDetails(user);
|
||||
}
|
||||
|
||||
|
@ -50,7 +48,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||
return new CustomUserDetails(
|
||||
user.getId(),
|
||||
user.getUsername(),
|
||||
null,
|
||||
user.getPassword(),
|
||||
authorities,
|
||||
user.getStatus() == 1
|
||||
);
|
||||
|
|
|
@ -19,7 +19,7 @@ public interface UserService {
|
|||
*/
|
||||
UserDTO login(LoginDto request);
|
||||
UserDTO getCurrentUser();
|
||||
Long getCurrentUserId();
|
||||
|
||||
UserDTO getUserById(Long id);
|
||||
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.entity.User;
|
||||
import com.guwan.backend.mapper.UserMapper;
|
||||
import com.guwan.backend.security.CustomUserDetails;
|
||||
import com.guwan.backend.service.EmailService;
|
||||
import com.guwan.backend.service.UserService;
|
||||
import com.guwan.backend.util.JwtUtil;
|
||||
import com.guwan.backend.util.RedisUtil;
|
||||
import com.guwan.backend.util.RedisUtils;
|
||||
import com.guwan.backend.util.SecurityUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
|
@ -35,8 +29,7 @@ public class UserServiceImpl implements UserService {
|
|||
private final UserMapper userMapper;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final JwtUtil jwtUtil;
|
||||
private final RedisUtil redisUtil;
|
||||
private final RedisUtils redisUtils;
|
||||
private final RedisUtils redisUtil;
|
||||
private final EmailService emailService;
|
||||
private final SecurityUtil securityUtil;
|
||||
|
||||
|
@ -86,8 +79,8 @@ public class UserServiceImpl implements UserService {
|
|||
|
||||
userMapper.insert(user);
|
||||
|
||||
redisUtil.delete(request.getEmail());
|
||||
redisUtil.delete(request.getPhone());
|
||||
redisUtil.del(request.getEmail());
|
||||
redisUtil.del(request.getPhone());
|
||||
|
||||
return convertToDTO(user);
|
||||
}
|
||||
|
@ -114,7 +107,7 @@ public class UserServiceImpl implements UserService {
|
|||
if (!request.getCode().equals(redisPhoneCode)){
|
||||
throw new IllegalArgumentException("验证码错误");
|
||||
}else {
|
||||
redisUtil.delete(request.getPhone());
|
||||
redisUtil.del(request.getPhone());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,7 +120,7 @@ public class UserServiceImpl implements UserService {
|
|||
if (!request.getCode().equals(redisEmailCode)){
|
||||
throw new IllegalArgumentException("验证码错误");
|
||||
}else {
|
||||
redisUtil.delete(request.getEmail());
|
||||
redisUtil.del(request.getEmail());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -146,25 +139,23 @@ public class UserServiceImpl implements UserService {
|
|||
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;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@OperationLog(description = "获取用户信息", operationType = "获取")
|
||||
public UserDTO getCurrentUser() {
|
||||
Long userId = getCurrentUserId();
|
||||
Long userId = securityUtil.getCurrentUserId();
|
||||
if (userId == null) {
|
||||
return null;
|
||||
}
|
||||
return getUserById(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getCurrentUserId() {
|
||||
return securityUtil.getCurrentUserId();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public UserDTO getUserById(Long id) {
|
||||
|
@ -180,7 +171,7 @@ public class UserServiceImpl implements UserService {
|
|||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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