class AppAndInstanceAuthorityInterceptor extends HandlerInterceptorAdapter {
    private Logger logger = LoggerFactory.getLogger(AppAndInstanceAuthorityInterceptor.class);
    @Autowired
    private AppService appService;
    @Autowired
    private UserService userService;
    @Autowired
    private InstanceStatsCenter instanceStatsCenter;
    @Autowired
    private UserLoginStatusService userLoginStatusService;

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {<FILL_FUNCTION_BODY>}

    /**
     * 检查用户应用的权限
     *
     * @param response
     * @param session
     * @param user
     * @param appId
     * @return
     */
    private void checkUserAppPower(HttpServletResponse response, HttpSession session, AppUser user, Long appId) {
        // 应用下的用户
        List<AppToUser> appToUsers = appService.getAppToUserList(appId);
        if (CollectionUtils.isNotEmpty(appToUsers)) {
            for (AppToUser tempAppToUser : appToUsers) {
                if (user.getId().equals(tempAppToUser.getUserId())) {
                    return;
                }
            }
            // 没权限
            String path = session.getServletContext().getContextPath();
            try {
                response.sendRedirect(path + "/resources/error/noPower.jsp?appId=" + appId);
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}

        // 1. 获取用户
        String userName = userLoginStatusService.getUserNameFromLoginStatus(request);
        //未登录
        if (StringUtils.isBlank(userName)) {
            String redirectUrl = userLoginStatusService.getRedirectUrl(request);
            response.sendRedirect(redirectUrl);
            return false;
        }
        AppUser user = userService.getByName(userName);
        if (user == null || user.getType() == -1) {
            String redirectUrl = userLoginStatusService.getRegisterUrl(user);
            response.sendRedirect(redirectUrl);
            return false;
        }

        // 2. 管理员直接跳过
        if (AppUserTypeEnum.ADMIN_USER.value().equals(user.getType())) {
            return true;
        }

        // 3. 应用id
        String appId = request.getParameter("appId");
        if (StringUtils.isNotBlank(appId)) {
            checkUserAppPower(response, request.getSession(true), user, NumberUtils.toLong(appId));
        }

        // 4. 实例权限检测(其实也是应用)
        String instanceId = request.getParameter("instanceId");
        if (StringUtils.isNotBlank(instanceId)) {
            InstanceInfo instanceInfo = instanceStatsCenter.getInstanceInfo(Long.parseLong(instanceId));
            checkUserAppPower(response, request.getSession(true), user, instanceInfo.getAppId());
        }

        return true;
    
<methods>public void <init>() ,public void afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception) throws java.lang.Exception,public void afterConcurrentHandlingStarted(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object) throws java.lang.Exception,public void postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView) throws java.lang.Exception,public boolean preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object) throws java.lang.Exception<variables>