1、不同模块登录之后,租户ID的处理,2、新增绑定短信接口
This commit is contained in:
@@ -2,8 +2,8 @@ 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.clear.service.impl.PlayClerkUserInfoServiceImpl;
|
||||
import com.starry.admin.modules.weichat.service.WxTokenService;
|
||||
import com.starry.common.constant.Constants;
|
||||
import com.starry.common.constant.HttpStatus;
|
||||
@@ -28,8 +28,11 @@ import java.util.Objects;
|
||||
@Component
|
||||
public class ClerkUserLoginAspect {
|
||||
|
||||
|
||||
|
||||
@Resource
|
||||
private PlayClerkUserInfoMapper userMapper;
|
||||
private PlayClerkUserInfoServiceImpl clerkUserInfoService;
|
||||
|
||||
@Resource
|
||||
private WxTokenService tokenService;
|
||||
@Resource
|
||||
@@ -46,12 +49,12 @@ public class ClerkUserLoginAspect {
|
||||
// 解析token
|
||||
String userId;
|
||||
try {
|
||||
userId = tokenService.getMiniUserIdByToken(userToken);
|
||||
userId = tokenService.getWxUserIdByToken(userToken);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
PlayClerkUserInfoEntity entity = userMapper.selectById(userId);
|
||||
PlayClerkUserInfoEntity entity = clerkUserInfoService.selectById(userId);
|
||||
if (Objects.isNull(entity)) {
|
||||
throw new ServiceException("未查询到有效用户", HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ 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.custom.service.impl.PlayCustomUserInfoServiceImpl;
|
||||
import com.starry.admin.modules.weichat.service.WxTokenService;
|
||||
import com.starry.common.constant.Constants;
|
||||
import com.starry.common.constant.HttpStatus;
|
||||
@@ -29,7 +29,8 @@ import java.util.Objects;
|
||||
public class CustomUserLoginAspect {
|
||||
|
||||
@Resource
|
||||
private PlayCustomUserInfoMapper userMapper;
|
||||
private PlayCustomUserInfoServiceImpl customUserInfoService;
|
||||
|
||||
@Resource
|
||||
private WxTokenService tokenService;
|
||||
@Resource
|
||||
@@ -46,12 +47,12 @@ public class CustomUserLoginAspect {
|
||||
// 解析token
|
||||
String userId;
|
||||
try {
|
||||
userId = tokenService.getMiniUserIdByToken(userToken);
|
||||
userId = tokenService.getWxUserIdByToken(userToken);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
PlayCustomUserInfoEntity entity = userMapper.selectById(userId);
|
||||
PlayCustomUserInfoEntity entity = customUserInfoService.selectById(userId);
|
||||
if (Objects.isNull(entity)) {
|
||||
throw new ServiceException("未查询到有效用户", HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package com.starry.admin.common.exception;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* @author 业务异常
|
||||
* @since 2023/3/9
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class ServiceException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@@ -38,9 +43,6 @@ public class ServiceException extends RuntimeException {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getDetailMessage() {
|
||||
return detailMessage;
|
||||
}
|
||||
|
||||
public ServiceException setDetailMessage(String detailMessage) {
|
||||
this.detailMessage = detailMessage;
|
||||
@@ -56,8 +58,4 @@ public class ServiceException extends RuntimeException {
|
||||
this.message = message;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,4 +88,11 @@ public class GlobalExceptionHandler {
|
||||
public R customException(CustomException e) {
|
||||
return R.error(e.getMessage());
|
||||
}
|
||||
|
||||
// @ExceptionHandler(ServiceException.class)
|
||||
// public R serviceException(ServiceException e) {
|
||||
// return R.error(e.getMessage());
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
package com.starry.admin.common.mybatis.handler;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.starry.admin.modules.weichat.service.WxTokenService;
|
||||
import com.starry.admin.utils.SecurityUtils;
|
||||
import com.starry.common.constant.Constants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
@@ -16,29 +21,53 @@ import java.util.Date;
|
||||
@Component
|
||||
public class MyMetaObjectHandler implements MetaObjectHandler {
|
||||
|
||||
|
||||
@Resource
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Resource
|
||||
private WxTokenService tokenService;
|
||||
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
log.info("start insert fill ....");
|
||||
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", getOperatorId(), 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) {
|
||||
this.setFieldValByName("createdBy", getOperatorId(), metaObject);
|
||||
}
|
||||
}
|
||||
|
||||
public String getOperatorId() {
|
||||
if (request.getServletPath().startsWith("/wx/")) {
|
||||
String clerkToken = request.getHeader(Constants.CLERK_USER_LOGIN_TOKEN);
|
||||
String customToken = request.getHeader(Constants.CUSTOM_USER_LOGIN_TOKEN);
|
||||
if (clerkToken != null) {
|
||||
return tokenService.getWxUserIdByToken(clerkToken);
|
||||
}
|
||||
if (customToken != null) {
|
||||
return tokenService.getWxUserIdByToken(customToken);
|
||||
}
|
||||
return "";
|
||||
} else {
|
||||
if (SecurityUtils.isLogin()) {
|
||||
return SecurityUtils.getUserId();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
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() {
|
||||
// 销毁代码
|
||||
}
|
||||
}
|
||||
@@ -50,25 +50,13 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
protected void configure(HttpSecurity httpSecurity) throws Exception {
|
||||
httpSecurity.csrf().disable()// 由于使用的是JWT,我们这里不需要csrf
|
||||
.sessionManagement()// 基于token,所以不需要session
|
||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||
.and()
|
||||
.authorizeRequests()
|
||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()
|
||||
// 允许对于网站静态资源的无授权访问
|
||||
.antMatchers(HttpMethod.GET,
|
||||
"/",
|
||||
"/*.html",
|
||||
"/favicon.ico",
|
||||
"/**/*.html",
|
||||
"/**/*.css",
|
||||
"/**/*.js",
|
||||
"/swagger-resources/**",
|
||||
"/v2/api-docs/**"
|
||||
).permitAll()
|
||||
.antMatchers(HttpMethod.GET, "/", "/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js", "/swagger-resources/**", "/v2/api-docs/**").permitAll()
|
||||
// 对登录注册要允许匿名访问
|
||||
.antMatchers("/login", "/captcha/get-captcha", "/wx/**").permitAll()
|
||||
// 跨域请求会先进行一次options请求
|
||||
.antMatchers(HttpMethod.OPTIONS).permitAll()
|
||||
.anyRequest()// 除上面外的所有请求全部需要鉴权认证
|
||||
.antMatchers(HttpMethod.OPTIONS).permitAll().anyRequest()// 除上面外的所有请求全部需要鉴权认证
|
||||
.authenticated();
|
||||
// 禁用缓存
|
||||
httpSecurity.headers().cacheControl();
|
||||
@@ -77,15 +65,12 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
// 添加JWT filter
|
||||
httpSecurity.addFilterBefore(jwtAuthenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
|
||||
// 添加自定义未授权和未登录结果返回
|
||||
httpSecurity.exceptionHandling()
|
||||
.accessDeniedHandler(customAccessDeniedHandler)
|
||||
.authenticationEntryPoint(customAuthenticationEntryPoint);
|
||||
httpSecurity.exceptionHandling().accessDeniedHandler(customAccessDeniedHandler).authenticationEntryPoint(customAuthenticationEntryPoint);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.userDetailsService(userDetailsService())
|
||||
.passwordEncoder(passwordEncoder());
|
||||
auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -3,8 +3,13 @@ package com.starry.admin.common.security.filter;
|
||||
|
||||
import com.starry.admin.common.component.JwtToken;
|
||||
import com.starry.admin.common.domain.LoginUser;
|
||||
import com.starry.admin.modules.clear.service.impl.PlayClerkUserInfoServiceImpl;
|
||||
import com.starry.admin.modules.custom.service.impl.PlayCustomUserInfoServiceImpl;
|
||||
import com.starry.admin.modules.weichat.service.WxTokenService;
|
||||
import com.starry.admin.utils.SecurityUtils;
|
||||
import com.starry.common.constant.Constants;
|
||||
import com.starry.common.utils.StringUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
|
||||
@@ -26,23 +31,48 @@ import java.io.IOException;
|
||||
@Slf4j
|
||||
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
|
||||
|
||||
@Resource
|
||||
WxTokenService tokenService;
|
||||
|
||||
@Value("${jwt.tokenHeader}")
|
||||
private String tokenHeader;
|
||||
@Value("${jwt.tokenHead}")
|
||||
private String tokenHead;
|
||||
@Resource
|
||||
private JwtToken jwtToken;
|
||||
|
||||
@Resource
|
||||
private PlayCustomUserInfoServiceImpl customUserInfoService;
|
||||
|
||||
@Resource
|
||||
private PlayClerkUserInfoServiceImpl clerkUserInfoService;
|
||||
|
||||
|
||||
@Override
|
||||
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);
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
|
||||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
// 微信公众号的请求
|
||||
if (httpServletRequest.getServletPath().startsWith("/wx/")) {
|
||||
String clerkToken = httpServletRequest.getHeader(Constants.CLERK_USER_LOGIN_TOKEN);
|
||||
String customToken = httpServletRequest.getHeader(Constants.CUSTOM_USER_LOGIN_TOKEN);
|
||||
if (StringUtils.isNotEmpty(clerkToken) || StringUtils.isNotEmpty(customToken)) {
|
||||
String userId = tokenService.getWxUserIdByToken(StringUtils.isNotEmpty(clerkToken) ? clerkToken : customToken);
|
||||
if (clerkToken != null) {
|
||||
SecurityUtils.setTenantId(clerkUserInfoService.selectById(userId).getTenantId());
|
||||
} else {
|
||||
SecurityUtils.setTenantId(customUserInfoService.selectById(userId).getTenantId());
|
||||
}
|
||||
} else {
|
||||
// 如果是微信端接口,并且未登录的话,从head中获取token
|
||||
String header = httpServletRequest.getHeader("tenantkey");
|
||||
// 根据租户表信息,查询租户ID(暂时先写死)
|
||||
String tenantId = "9999";
|
||||
SecurityUtils.setTenantId(header);
|
||||
}
|
||||
} else {
|
||||
// 管理端的请求
|
||||
LoginUser jwtUser = jwtToken.getNewLoginUser(httpServletRequest);
|
||||
if (null != jwtUser && null == SecurityContextHolder.getContext().getAuthentication()) {
|
||||
jwtToken.verifyToken(jwtUser);
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
|
||||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
}
|
||||
}
|
||||
filterChain.doFilter(httpServletRequest, httpServletResponse);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user