use of com.jeesuite.common.exception.UnauthorizedException in project jeesuite-libs by vakinge.
the class SecurityDelegatingFilter method doFilter.
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
// 忽略静态资源
if (request.getRequestURI().contains(DOT) && (apiUriSuffix == null || !request.getRequestURI().endsWith(apiUriSuffix))) {
chain.doFilter(req, res);
return;
}
if (request.getMethod().equals(HttpMethod.OPTIONS.name())) {
chain.doFilter(req, res);
return;
}
if (additionHandler != null) {
additionHandler.beforeAuthorization(request, response);
}
CurrentRuntimeContext.init(request, response);
UserSession userSession = null;
try {
userSession = SecurityDelegating.doAuthorization();
} catch (UnauthorizedException e) {
if (WebUtils.isAjax(request)) {
WebUtils.responseOutJson(response, MSG_401_UNAUTHORIZED);
} else {
if (SecurityDelegating.getConfigurerProvider().error401Page() == null) {
response.setStatus(401);
WebUtils.responseOutHtml(response, "401 Unauthorized");
} else {
String loginPage = WebUtils.getBaseUrl(request) + SecurityDelegating.getConfigurerProvider().error401Page();
response.sendRedirect(loginPage);
}
}
return;
} catch (ForbiddenAccessException e) {
if (WebUtils.isAjax(request)) {
WebUtils.responseOutJson(response, MSG_403_FORBIDDEN);
} else {
if (SecurityDelegating.getConfigurerProvider().error403Page() == null) {
response.setStatus(403);
WebUtils.responseOutHtml(response, "403 Forbidden");
} else {
String loginPage = WebUtils.getBaseUrl(request) + SecurityDelegating.getConfigurerProvider().error403Page();
response.sendRedirect(loginPage);
}
}
return;
}
//
if (additionHandler != null) {
additionHandler.afterAuthorization(userSession);
}
chain.doFilter(req, res);
}
use of com.jeesuite.common.exception.UnauthorizedException in project jeesuite-libs by vakinge.
the class SecurityDelegating method doAuthorization.
/**
* 鉴权
* @param userId
* @param uri
*/
public static UserSession doAuthorization() throws UnauthorizedException, ForbiddenAccessException {
UserSession session = getCurrentSession();
String uri = CurrentRuntimeContext.getRequest().getRequestURI();
boolean isAdmin = session != null && session.getUser() != null && session.getUser().isAdmin();
if (!isAdmin && !getInstance().resourceManager.isAnonymous(uri)) {
if (session == null || session.isAnonymous()) {
throw new UnauthorizedException();
}
String permissionKey = ApiPermssionCheckHelper.buildPermissionKey(CurrentRuntimeContext.getRequest().getMethod(), uri);
PermissionLevel permissionLevel = ApiPermssionCheckHelper.matchPermissionLevel(getInstance().resourceManager, permissionKey);
// 如果需鉴权
if (permissionLevel == PermissionLevel.PermissionRequired) {
List<String> permissions = getInstance().resourceManager.getUserPermissions(session);
if (!ApiPermssionCheckHelper.checkPermissions(getInstance().resourceManager, permissionKey, permissions)) {
throw new ForbiddenAccessException();
}
}
}
//
if (session != null) {
CurrentRuntimeContext.setAuthUser(session.getUser());
if (StringUtils.isNotBlank(session.getTenantId())) {
CurrentRuntimeContext.setTenantId(session.getTenantId());
}
// 续租
if (session.getExpiredAt() - System.currentTimeMillis() < SESSION_RNEWAL_BEFORE_MILLS) {
getInstance().sessionManager.storageLoginSession(session);
}
}
return session;
}
Aggregations