aop

package com.huawei.e.itr.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.e.itr.common.config.MyConstants;
import com.huawei.e.itr.common.context.AppContext;
import com.huawei.e.itr.common.dao.Dao;
import com.huawei.e.itr.common.entity.User;
import com.huawei.e.itr.common.exception.BadRequestException;
import com.huawei.e.itr.common.service.UserService;
import com.huawei.e.itr.common.utils.CommonUtil;
import com.huawei.e.itr.common.utils.LogUtils;
import com.huawei.e.itr.common.utils.Result;
import com.huawei.it.sso.filter.util.SsoConstants;
import com.huawei.it.sso.filter.util.SsoUtil;
import com.huawei.it.support.usermanage.helper.UserInfoBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class LoginFilter implements Filter {

    @Autowired
    Dao dao;

    @Autowired
    UserService userService;

    @Autowired
    LogUtils logUtils;

    @Autowired
    JedisPool jedisPool;

    private int EXP_TIME = 60 * 30; // redis 缓存过期时间 30分钟

    @Override
    public void init(FilterConfig fConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }

    public boolean isMSBrowser(HttpServletRequest request) {
        String[] IEBrowserSignals = {"MSIE", "Trident", "Edge"};
        String userAgent = request.getHeader("User-Agent");
        for (String signal : IEBrowserSignals) {
            if (userAgent.contains(signal)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;

        //判断如果是APP请求,则直接通过
        String requestURI = httpServletRequest.getRequestURI();
        if (requestURI.endsWith(".ttf") ||
                requestURI.endsWith(".eot") ||
                requestURI.endsWith(".woff2") ||
                requestURI.endsWith(".css") ||
                requestURI.endsWith(".js") ||
                requestURI.endsWith(".png") ||
                requestURI.endsWith(".jpg") ||
                requestURI.endsWith(".wav") ||
                requestURI.endsWith(".xlsx") ||
                requestURI.endsWith(".svg")
        ) {
            chain.doFilter(request, response);
            return;
        }
        if (requestURI.indexOf("/e/page") != -1) {
            if (isMSBrowser(httpServletRequest)) {
                response.setCharacterEncoding("gbk");
                PrintWriter out = response.getWriter();
                out.println("<div>本系统不支持 IE EDGE 等浏览器,推荐使用 Google Chrome浏览器,谢谢。</div>");
                return;
            }
        }
        HttpSession session = httpServletRequest.getSession();
        UserInfoBean uiBean = (UserInfoBean) session
                .getAttribute(SsoConstants.SESSION_USER_INFO_KEY);
        if (uiBean == null) {   //会话已经超时
            if (CommonUtil.isAjax(httpServletRequest)) {   //ajax请求会话超时,filter不会进入统一异常拦截
                Map<String, String> map = new HashMap<>();
                map.put("code", "401");
                response.setCharacterEncoding("utf-8");
                response.getWriter().print(JSONObject.toJSON(map));
                return;
            }
            try {
                // 从 sso 跳转回来 回到当前访问的 url
                SsoUtil.loginAndRedirect2AppCurrentURL((HttpServletRequest) request,
                        (HttpServletResponse) response);
                return;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                User token = null;
                try (Jedis jedis = jedisPool.getResource()) {
                    String redisKey = "asp_" + uiBean.getUid();
                    String tokenStr = jedis.get(redisKey);
                    if (StringUtils.isNotBlank(tokenStr)) {
                        token = JSON.parseObject(tokenStr, User.class);
                    }
                    if (token == null || token.getRootList().size() == 0) {
                        Result r = userService.login((HttpServletRequest) request,
                                (HttpServletResponse) response);
                        token = (User) r.getData();
                        if ("N".equals(token.getIsVaild())) {
                            if (CommonUtil.isAjax(httpServletRequest)) {   //ajax请求会话超时,filter不会进入统一异常拦截
                                throw new BadRequestException("此账号已被禁用,如需开通,请联系系统管理员 王超伟 w00509928 !");
                            } else {
                                response.setCharacterEncoding("gbk");
                                response.getWriter().println("此账号已被禁用,如需开通,请联系系统管理员 王超伟 w00509928 !");
                                return;
                            }
                        }
                        if (!token.getIsExists()) { // 用户在本地是否存在
                            boolean isChangeAccount = false;
                            // 本地不存在,看看是否是更换了新账号
                            String oldaccount =
                                    userService.getOldW3AccountByNewW3Account(token.getUserAgentId());
                            if (oldaccount != null) {
                                Map<String, Object> p = new HashMap<>();
                                p.put("USER_AGENTID", oldaccount);
                                Map<String, Object> oldUser =
                                        dao.findOne2Map(MyConstants.USER, p, null);
                                if (oldUser != null) { // 存在旧账号的用户信息,把用新账号替换旧账号
                                    String oldAccount = (String) oldUser.get("USER_AGENTID");
                                    oldUser.put("USER_AGENTID", token.getUserAgentId());
                                    dao.saveOrUpdate(MyConstants.USER, oldUser);
                                    dao.commit();
                                    String data = oldAccount + " 替换为 " + token.getUserAgentId();
                                    logUtils.log("更换W3账号", data, oldAccount, (String) oldUser.get("ID"));
                                    // 重新登录
                                    Result loginResult = userService.login((HttpServletRequest) request,
                                            (HttpServletResponse) response);
                                    token = (User) loginResult.getData();
                                    isChangeAccount = true;
                                }
                            }
                            if (isChangeAccount == false) { // 本地不存在,也没有更改过账号,跳转到用户信息审批页面
//                                Map<String, Object> p = new HashMap<>();
//                                p.put("USER_AGENTID", token.getUserAgentId());
//                                Map<String, Object> saveData =
//                                        dao.findOne2Map(R.T_OWS_WAIT_APPROVAL, p, null);
//                                if (saveData == null) {
//                                    saveData = new HashMap<>();
//                                    saveData.put("ID", UUID.randomUUID().toString());
//                                    saveData.put("CREATE_DATE", System.currentTimeMillis());
//                                    saveData.put("USER_NAME", token.getUserName());
//                                    saveData.put("USER_EMAIL", token.getUserEmail());
//                                    saveData.put("USER_PHONE", token.getUserPhono());
//                                    saveData.put("USER_AGENTID", token.getUserAgentId());
//                                    dao.saveOrUpdate(R.T_OWS_WAIT_APPROVAL, saveData);
//                                    dao.commit();
//                                }
//                                ((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath()
//                                        + "/page/addUser?agentId=" + token.getUserAgentId());
//                                return;
                                Map newUser = new HashMap();
                                newUser.put("USER_AGENTID", token.getUserAgentId());
                                newUser.put("ROLE_ID", "98d3a0ad63bf4a4fa0d53de884118bdf"); //默认是 申请人角色
                                newUser.put("RECT_ROLE_ID", "98d3a0ad63bf4a4fa0d53de884118bdf"); //默认是 申请人角色
                                newUser.put("CREATE_TIME", System.currentTimeMillis());
                                newUser.put("USER_NAME", token.getUserName());
                                newUser.put("USER_EMAIL", token.getUserEmail());
                                newUser.put("USER_PHONE", token.getUserPhono());
                                dao.saveOrUpdate(MyConstants.USER, newUser);
                                dao.commit();
                                logUtils.log("新账号创建", JSON.toJSONString(newUser),
                                        (String) newUser.get("USER_AGENTID"), (String) newUser.get("ID"));
                                // 重新登录
                                Result loginResult = userService.login((HttpServletRequest) request,
                                        (HttpServletResponse) response);
                                token = (User) loginResult.getData();
                            }
                        }
                        // 加载整改配置角色
                        userService.setRectRole(token);
                        // 加载ASP角色
                        userService.setAspRole(token);
                        // 加载菜单、权限等
                        userService.loadUserResource(token);
//                        session.setAttribute("userInfo", token);
                        logUtils.log("登录", null, token.getUserAgentId(), token.getId());
                        dao.commit();
                        jedis.set(redisKey, JSON.toJSONString(token));
                        log.info("从数据库中加载用户信息");
                    }
                    // 设置过期时间 单位(秒)
                    jedis.expire(redisKey, EXP_TIME);
                }
                if (!requestURI.startsWith("/e/digital/web")) {
                    if (!requestURI.startsWith("/e/digital/msd/todotask")) {
                        if (!requestURI.startsWith("/e/digital/index")) {
                            if (!requestURI.startsWith("/e/digital/sys/common")) {
                                if (StringUtils.isEmpty(token.getCurrentRole())) {
                                    ((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() +
                                            "/index/index");
                                    return;
                                }
                            }
                        }
                    }
                }
                Method m = AppContext.class.getDeclaredMethod("setCurrentUser", User.class);
                m.setAccessible(true);
                m.invoke(null, token);
                chain.doFilter(request, response);
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            } finally {
                AppContext.clearAll();
                try {
                    dao.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error(e.getMessage(), e);
                }
            }
        }
    }
}

 

package com.huawei.e.itr.aop;

import com.huawei.e.itr.annotaion.ApiCheck;
import com.huawei.e.itr.common.context.AppContext;
import com.huawei.e.itr.common.entity.User;
import com.huawei.e.itr.common.exception.BadRequestException;
import com.huawei.e.itr.entity.RoleType;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

/**
 * @author xWX620347
 * @date 2020-06-17
 */
@Aspect
@Configuration
public class ApiCheckAop {

    //    @Autowired
    //    Dao dao;

    @Pointcut(value = "@annotation(com.huawei.e.itr.annotaion.ApiCheck)")
    public void exeController() {
    }

    @Around("exeController()")
    public Object doControllerAround(ProceedingJoinPoint pjp) throws Throwable {
        return this.doCheckRight(pjp);
//        return pjp.proceed();
    }

    private Object doCheckRight(ProceedingJoinPoint point) throws Throwable {
        Object result = null;
        User user = AppContext.getCurrentUser();
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        if (method.isAnnotationPresent(ApiCheck.class)) {
//            Object[] args = point.getArgs();
            ApiCheck apiCheck = method.getAnnotation(ApiCheck.class);
            RoleType[] values = apiCheck.value();
            boolean isAuthed = false;
            for (RoleType value : values) {
                if (value == RoleType.FSE) {
                    if (user.getIsFse()) {
                        isAuthed = true;
                        break;
                    }
                } else if (value == RoleType.FSIP) {
                    if (user.getIsFsip()) {
                        isAuthed = true;
                        break;
                    }
                } else if (value == RoleType.MSD) {
                    if (user.getIsMsd()) {
                        isAuthed = true;
                        break;
                    }
                }
            }
            if (!isAuthed) {
                /**
                 * 不要注释此句代码,请为你的API添加正确的注解
                 * @see ApiCheck
                  */
                throw new BadRequestException("无权限访问");
            }
        }
        // result的值就是被拦截方法的返回值
        result = point.proceed();
        return result;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
校园悬赏任务平台对字典管理、论坛管理、任务资讯任务资讯公告管理、接取用户管理、任务管理、任务咨询管理、任务收藏管理、任务评价管理、任务订单管理、发布用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行校园悬赏任务平台程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。校园悬赏任务平台的开发让用户查看任务信息变得容易,让管理员高效管理任务信息。 校园悬赏任务平台具有管理员角色,用户角色,这几个操作权限。 校园悬赏任务平台针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理任务信息,管理任务资讯公告信息等内容。 校园悬赏任务平台针对用户设置的功能有:查看并修改个人信息,查看任务信息,查看任务资讯公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看任务,删除任务操作,新增任务操作,修改任务操作。任务资讯公告信息管理页面提供的功能操作有:新增任务资讯公告,修改任务资讯公告,删除任务资讯公告操作。任务资讯公告类型管理页面显示所有任务资讯公告类型,在此页面既可以让管理员添加新的任务资讯公告信息类型,也能对已有的任务资讯公告类型信息执行编辑更新,失效的任务资讯公告类型信息也能让管理员快速删除。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值