All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.invms.x.components.basis.PowerInterceptor Maven / Gradle / Ivy

package com.invms.x.components.basis;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import com.invms.x.builders.ArgsBuilder;
import com.invms.x.components.DefaultConfigurer;
import com.invms.x.components.DefaultException;
import com.invms.x.components.IEntryInterceptor;
import com.invms.x.filters.ActionFilter;
import com.invms.x.filters.IgnoreFilter;
import com.invms.x.filters.LogFilter;
import com.invms.x.filters.LoggedFilter;
import com.invms.x.filters.RoleFilter;
import com.invms.x.filters.UserFilter;
import com.invms.x.models.basis.UserModel;
import com.invms.x.services.basis.AuthorizeService;
import com.invms.x.services.basis.LogService;
import com.invms.x.services.basis.SessionService;
import com.invms.x.utils.ComUtils;

@Order(value = 2)
@Component
public class PowerInterceptor implements IEntryInterceptor {
	private static Logger logger = LoggerFactory.getLogger(PowerInterceptor.class);

	@Autowired
	private LogService logService;

	@Autowired
	private SessionService sessionService;

	@Autowired
	private AuthorizeService authorizeService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if (handler instanceof HandlerMethod) {
			HandlerMethod handlerMethod = (HandlerMethod) handler;
			Class beanClass = handlerMethod.getBeanType();
			Method beanMethod = handlerMethod.getMethod();
			ArgsBuilder argsBuilder = new ArgsBuilder();
			argsBuilder.setServletRequest(request);
			UserModel user = authorizeService.getLoggedUser(argsBuilder, false);
			try {
				sessionService.keepAlive(argsBuilder, user);
			} catch (Exception e) {
				logger.error("", e);
			}
			if (user != null) {
				argsBuilder.setValue(DefaultConfigurer.loggedId, user.getId());
				argsBuilder.setValue(DefaultConfigurer.loggedName, user.getName());
			}
			List list = new LinkedList();
			list.addAll(Arrays.asList(beanClass.getAnnotations()));
			list.addAll(Arrays.asList(beanMethod.getAnnotations()));
			boolean loggedEnabled = false;
			boolean requireAuthorize = authorizeService.isRequireAuthorize(argsBuilder);
			List logFilters = new LinkedList();
			List loggedFilters = new LinkedList();
			for (Annotation item : list) {
				if (item instanceof LogFilter) {
					logFilters.add((LogFilter) item);
				} else if (item instanceof IgnoreFilter) {
					requireAuthorize = false;
					loggedEnabled = ((IgnoreFilter) item).logged();
				} else if (item instanceof LoggedFilter) {
					loggedEnabled = true;
				} else if (item instanceof UserFilter) {
					loggedEnabled = true;
					loggedFilters.add(item);
				} else if (item instanceof RoleFilter) {
					loggedEnabled = true;
					loggedFilters.add(item);
				} else if (item instanceof ActionFilter) {
					loggedEnabled = true;
					loggedFilters.add(item);
				}
			}
			if (!logFilters.isEmpty()) {
				logService.writeLog(argsBuilder, logFilters);
			}
			if (loggedEnabled) {
				String message = null;
				if (requireAuthorize && !loggedFilters.isEmpty()) {
					message = authorizeService.hasLoggedPower(argsBuilder, loggedFilters);
				} else {
					message = authorizeService.isUserLogged(argsBuilder) ? null : "没有登入,请刷新或重新登入";
				}
				if (!ComUtils.empty(message)) {
					throw new DefaultException(message);
				}
			}
		}
		return true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy