|
|
|
|
@@ -1,5 +1,13 @@
|
|
|
|
|
package com.labelsys.backend.interceptor;
|
|
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.web.method.HandlerMethod;
|
|
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
|
|
|
|
|
import com.labelsys.backend.annotation.RequirePosition;
|
|
|
|
|
import com.labelsys.backend.common.exception.ForbiddenException;
|
|
|
|
|
import com.labelsys.backend.common.exception.UnauthorizedException;
|
|
|
|
|
@@ -8,48 +16,30 @@ import com.labelsys.backend.context.UserContext;
|
|
|
|
|
import com.labelsys.backend.entity.SysCompany;
|
|
|
|
|
import com.labelsys.backend.entity.SysUser;
|
|
|
|
|
import com.labelsys.backend.enums.CompanyStatus;
|
|
|
|
|
import com.labelsys.backend.enums.UserPosition;
|
|
|
|
|
import com.labelsys.backend.enums.UserStatus;
|
|
|
|
|
import com.labelsys.backend.mapper.SysCompanyMapper;
|
|
|
|
|
import com.labelsys.backend.mapper.SysUserMapper;
|
|
|
|
|
import com.labelsys.backend.service.session.TokenSessionRepository;
|
|
|
|
|
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.web.method.HandlerMethod;
|
|
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
public class AuthInterceptor implements HandlerInterceptor {
|
|
|
|
|
|
|
|
|
|
private static final Set<String> OPEN_PATHS = Set.of(
|
|
|
|
|
"/label/api/auth/companies",
|
|
|
|
|
"/label/api/auth/login",
|
|
|
|
|
"/label/swagger-ui.html",
|
|
|
|
|
"/label/v3/api-docs",
|
|
|
|
|
"/label/v3/api-docs/swagger-config"
|
|
|
|
|
);
|
|
|
|
|
private static final Set<String> OPEN_PATHS = Set.of("/label/api/auth/companies", "/label/api/auth/login",
|
|
|
|
|
"/label/swagger-ui.html", "/label/v3/api-docs", "/label/v3/api-docs/swagger-config");
|
|
|
|
|
|
|
|
|
|
private static final Set<String> ALLOWED_WHEN_MUST_CHANGE_PASSWORD = Set.of(
|
|
|
|
|
"/label/api/auth/change-password",
|
|
|
|
|
"/label/api/auth/logout",
|
|
|
|
|
"/label/api/auth/me"
|
|
|
|
|
);
|
|
|
|
|
private static final Set<String> ALLOWED_WHEN_MUST_CHANGE_PASSWORD =
|
|
|
|
|
Set.of("/label/api/auth/change-password", "/label/api/auth/logout", "/label/api/auth/me");
|
|
|
|
|
|
|
|
|
|
private final TokenSessionRepository tokenSessionRepository;
|
|
|
|
|
private final SysUserMapper sysUserMapper;
|
|
|
|
|
private final SysCompanyMapper sysCompanyMapper;
|
|
|
|
|
private final Duration sessionTtl;
|
|
|
|
|
|
|
|
|
|
public AuthInterceptor(
|
|
|
|
|
TokenSessionRepository tokenSessionRepository,
|
|
|
|
|
SysUserMapper sysUserMapper,
|
|
|
|
|
SysCompanyMapper sysCompanyMapper,
|
|
|
|
|
@Value("${labelsys.session.ttl:PT2H}") Duration sessionTtl
|
|
|
|
|
) {
|
|
|
|
|
public AuthInterceptor(TokenSessionRepository tokenSessionRepository, SysUserMapper sysUserMapper,
|
|
|
|
|
SysCompanyMapper sysCompanyMapper, @Value("${labelsys.session.ttl:PT2H}") Duration sessionTtl) {
|
|
|
|
|
this.tokenSessionRepository = tokenSessionRepository;
|
|
|
|
|
this.sysUserMapper = sysUserMapper;
|
|
|
|
|
this.sysCompanyMapper = sysCompanyMapper;
|
|
|
|
|
@@ -69,12 +59,13 @@ public class AuthInterceptor implements HandlerInterceptor {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
String token = extractToken(request.getHeader("Authorization"));
|
|
|
|
|
LoginUser loginUser = tokenSessionRepository.find(token)
|
|
|
|
|
.orElseThrow(() -> new UnauthorizedException("未登录或登录已过期"));
|
|
|
|
|
LoginUser loginUser =
|
|
|
|
|
tokenSessionRepository.find(token).orElseThrow(() -> new UnauthorizedException("未登录或登录已过期"));
|
|
|
|
|
|
|
|
|
|
SysUser user = sysUserMapper.findByIdAndCompanyId(loginUser.userId(), loginUser.companyId());
|
|
|
|
|
SysCompany company = sysCompanyMapper.selectById(loginUser.companyId());
|
|
|
|
|
if (user == null || company == null || user.getStatus() != UserStatus.ENABLED || company.getStatus() != CompanyStatus.ENABLED) {
|
|
|
|
|
if (user == null || company == null || user.getStatus() != UserStatus.ENABLED
|
|
|
|
|
|| company.getStatus() != CompanyStatus.ENABLED) {
|
|
|
|
|
throw new UnauthorizedException("未登录或登录已过期");
|
|
|
|
|
}
|
|
|
|
|
if (!user.getSessionVersion().equals(loginUser.sessionVersion())) {
|
|
|
|
|
@@ -96,7 +87,8 @@ public class AuthInterceptor implements HandlerInterceptor {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
|
|
|
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
|
|
|
Exception ex) {
|
|
|
|
|
UserContext.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|