微信网页登录

This commit is contained in:
starrySky
2024-04-09 10:17:49 +08:00
parent b2f6921ef1
commit e8b6c8e0aa
128 changed files with 2861 additions and 4243 deletions

View File

@@ -0,0 +1,17 @@
package com.starry.admin.common.aspect;
import java.lang.annotation.*;
/**
* 陪玩登录注解
*
* @author ruoyi
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ClerkUserLogin {
boolean manage() default false;
}

View File

@@ -0,0 +1,63 @@
package com.starry.admin.common.aspect;
import com.starry.admin.common.conf.ThreadLocalRequestDetail;
import com.starry.admin.common.exception.ServiceException;
import com.starry.admin.modules.clear.mapper.PlayClerkUserInfoMapper;
import com.starry.admin.modules.clear.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.weichat.service.WxTokenService;
import com.starry.common.constant.Constants;
import com.starry.common.constant.HttpStatus;
import com.starry.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
/**
* 限流处理
*
* @author ruoyi
*/
@Slf4j
@Aspect
@Component
public class ClerkUserLoginAspect {
@Resource
private PlayClerkUserInfoMapper userMapper;
@Resource
private WxTokenService tokenService;
@Resource
private HttpServletRequest request;
@Before("@annotation(clerkUserLogin)")
public void doBefore(JoinPoint point, ClerkUserLogin clerkUserLogin) {
String userToken = request.getHeader(Constants.CLERK_USER_LOGIN_TOKEN);
if (StringUtils.isEmpty(userToken)) {
throw new ServiceException("token为空", HttpStatus.UNAUTHORIZED);
}
userToken = userToken.replace(Constants.TOKEN_PREFIX, "");
// 解析token
String userId;
try {
userId = tokenService.getMiniUserIdByToken(userToken);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
}
PlayClerkUserInfoEntity entity = userMapper.selectById(userId);
if (Objects.isNull(entity)) {
throw new ServiceException("未查询到有效用户", HttpStatus.UNAUTHORIZED);
}
if (!userToken.equals(entity.getToken())) {
throw new ServiceException("token异常", HttpStatus.UNAUTHORIZED);
}
ThreadLocalRequestDetail.setRequestDetail(entity);
}
}

View File

@@ -0,0 +1,17 @@
package com.starry.admin.common.aspect;
import java.lang.annotation.*;
/**
* 客户登录注解
*
* @author ruoyi
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CustomUserLogin {
boolean manage() default false;
}

View File

@@ -0,0 +1,63 @@
package com.starry.admin.common.aspect;
import com.starry.admin.common.conf.ThreadLocalRequestDetail;
import com.starry.admin.common.exception.ServiceException;
import com.starry.admin.modules.custom.mapper.PlayCustomUserInfoMapper;
import com.starry.admin.modules.custom.module.entity.PlayCustomUserInfoEntity;
import com.starry.admin.modules.weichat.service.WxTokenService;
import com.starry.common.constant.Constants;
import com.starry.common.constant.HttpStatus;
import com.starry.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
/**
* 限流处理
*
* @author ruoyi
*/
@Slf4j
@Aspect
@Component
public class CustomUserLoginAspect {
@Resource
private PlayCustomUserInfoMapper userMapper;
@Resource
private WxTokenService tokenService;
@Resource
private HttpServletRequest request;
@Before("@annotation(customUserLogin)")
public void doBefore(JoinPoint point, CustomUserLogin customUserLogin) {
String userToken = request.getHeader(Constants.CUSTOM_USER_LOGIN_TOKEN);
if (StringUtils.isEmpty(userToken)) {
throw new ServiceException("token为空", HttpStatus.UNAUTHORIZED);
}
userToken = userToken.replace(Constants.TOKEN_PREFIX, "");
// 解析token
String userId;
try {
userId = tokenService.getMiniUserIdByToken(userToken);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
}
PlayCustomUserInfoEntity entity = userMapper.selectById(userId);
if (Objects.isNull(entity)) {
throw new ServiceException("未查询到有效用户", HttpStatus.UNAUTHORIZED);
}
if (!userToken.equals(entity.getToken())) {
throw new ServiceException("token异常", HttpStatus.UNAUTHORIZED);
}
ThreadLocalRequestDetail.setRequestDetail(entity);
}
}

View File

@@ -98,7 +98,7 @@ public class JwtToken {
String token = IdUtil.fastSimpleUUID();
jwtUser.setToken(token);
setUserAgent(jwtUser);
refresToken(jwtUser);
refersToken(jwtUser);
Map<String, Object> claims = new HashMap<>();
claims.put(Constants.LOGIN_USER_KEY, token);
@@ -167,7 +167,7 @@ public class JwtToken {
jwtUser.setOs(userAgent.getOs().getName());
}
public void refresToken(JwtUser jwtUser) {
public void refersToken(JwtUser jwtUser) {
jwtUser.setLoginTime(System.currentTimeMillis());
jwtUser.setExpireTime(jwtUser.getLoginTime() + expire * 1000);
String userKey = getTokenKey(jwtUser.getToken());
@@ -224,7 +224,7 @@ public class JwtToken {
long expireTime = jwtUser.getExpireTime();
long currentTime = System.currentTimeMillis();
if (expireTime - currentTime <= MILLIS_MINUTE_TEN) {
refresToken(jwtUser);
refersToken(jwtUser);
}
}

View File

@@ -0,0 +1,43 @@
package com.starry.admin.common.conf;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.starry.admin.modules.clear.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.custom.module.entity.PlayCustomUserInfoEntity;
/**
* @author : huchuansai
* @since : 2024/4/2 12:10 AM
*/
public class ThreadLocalRequestDetail {
private static final TransmittableThreadLocal<Object> threadLocal = new TransmittableThreadLocal<>();
/**
* 设置请求信息到当前线程中
*/
public static void setRequestDetail(Object data) {
threadLocal.set(data);
}
/**
* 从当前线程中获取请求信息
*/
public static Object getRequestDetail() {
return threadLocal.get();
}
public static PlayClerkUserInfoEntity getClerkUserInfo() {
return (PlayClerkUserInfoEntity) threadLocal.get();
}
public static PlayCustomUserInfoEntity getCustomUserInfo() {
return (PlayCustomUserInfoEntity) threadLocal.get();
}
/**
* 销毁
*/
public static void remove() {
threadLocal.remove();
}
}

View File

@@ -1,7 +1,6 @@
package com.starry.admin.common.mybatis.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.starry.admin.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
@@ -23,23 +22,23 @@ public class MyMetaObjectHandler implements MetaObjectHandler {
this.setFieldValByName("createdTime", new Date(), metaObject);
this.setFieldValByName("deleted", false, metaObject);
this.setFieldValByName("version", 1L, metaObject);
Object createUser = this.getFieldValByName("createdBy", metaObject);
if (createUser == null) {
if (SecurityUtils.isLogin()) {
this.setFieldValByName("createdBy", SecurityUtils.getUserId(), metaObject);
}
}
// Object createUser = this.getFieldValByName("createdBy", metaObject);
// if (createUser == null) {
// if (SecurityUtils.isLogin()) {
// this.setFieldValByName("createdBy", SecurityUtils.getUserId(), metaObject);
// }
// }
}
@Override
public void updateFill(MetaObject metaObject) {
log.info("start update fill ....");
this.setFieldValByName("updatedTime", new Date(), metaObject);
Object createUser = this.getFieldValByName("updatedBy", metaObject);
if (createUser == null) {
if (SecurityUtils.isLogin()) {
this.setFieldValByName("updatedBy", SecurityUtils.getUserId(), metaObject);
}
}
// Object createUser = this.getFieldValByName("updatedBy", metaObject);
// if (createUser == null) {
// if (SecurityUtils.isLogin()) {
// this.setFieldValByName("updatedBy", SecurityUtils.getUserId(), metaObject);
// }
// }
}
}

View File

@@ -23,7 +23,7 @@ public class MyTenantLineHandler implements TenantLineHandler {
/**
* 排除过滤的表
*/
private static final String[] TABLE_FILTER = {"sys_user", "sys_menu", "sys_tenant_package", "sys_tenant", "sys_dict", "sys_dict_data"};
private static final String[] TABLE_FILTER = {"sys_user", "sys_menu", "sys_tenant_package", "sys_tenant", "sys_dict", "sys_dict_data", "sys_administrative_area_dict_info"};
/**
* 排除过滤的表前缀

View File

@@ -0,0 +1,32 @@
package com.starry.admin.common.security;
import javax.servlet.*;
import java.io.IOException;
/**
* @author admin
* @since 2024/4/7 17:17
**/
public class CustomFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) {
// 初始化代码
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 在请求处理之前可以进行一些操作
// 例如,可以记录请求开始时间
System.out.println("--------------------");
// 继续调用下一个Filter或servlet
chain.doFilter(request, response);
// 在请求处理之后可以进行一些操作
// 例如,可以记录请求结束时间并计算耗时
}
@Override
public void destroy() {
// 销毁代码
}
}

View File

@@ -65,7 +65,7 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
"/v2/api-docs/**"
).permitAll()
// 对登录注册要允许匿名访问
.antMatchers("/login", "/captcha/get-captcha", "/wx/test/**","/wp/clear/**").permitAll()
.antMatchers("/login", "/captcha/get-captcha", "/wx/**").permitAll()
// 跨域请求会先进行一次options请求
.antMatchers(HttpMethod.OPTIONS).permitAll()
.anyRequest()// 除上面外的所有请求全部需要鉴权认证

View File

@@ -34,24 +34,9 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
@Resource
private JwtToken jwtToken;
@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
String url = request.getRequestURL().toString();
log.debug("url ={}", url);
// 指定URL不拦截
if (url.contains("/wp/clear/")) {
return true;
}
// 指定URL不拦截
if (url.contains("/wp/custom/")) {
return true;
}
return false;
}
@Override
protected void doFilterInternal(@NotNull HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
protected void doFilterInternal(@NotNull HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
LoginUser jwtUser = jwtToken.getNewLoginUser(httpServletRequest);
if (null != jwtUser && null == SecurityContextHolder.getContext().getAuthentication()) {
jwtToken.verifyToken(jwtUser);