登陆后自动跳转到登录前页面

今天想实现这个功能,网上说是用 referer 来实现。不过referer也有几个问题:
1 当打开一个新窗口 直接输入网址 浏览器似乎不会发送referer 头
2 当通过js window.open等函数打开也不回产生
3 referer 发送与否 在浏览器是可设置的(当然估计也没多少人会去设置)

这里我结合struts2 说下我的实现方法:

package com.snail.commons.interceptors;

import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.aopalliance.intercept.Invocation;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.hibernate.criterion.Expression;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.snail.commons.basedao.IBaseDAO;
import com.snail.component.beans.Huiyuan;
@Scope("prototype")
@Controller("loginInter")
public class LoginInter extends AbstractInterceptor
    {
        private final static Logger logger=Logger.getLogger(LoginInter.class);
        public final static String SESSION_KEY="User";
        public final static String COOKIE_KEY="12cd";
        public final static String GOTO_URL_KEY="GOING_TO";
        @Resource(name="baseDAO")
        protected IBaseDAO baseDAO;
    @Override
    public String intercept(ActionInvocation invocation) throws Exception
        {
            HttpServletRequest request=ServletActionContext.getRequest();
           
            HttpServletResponse response=ServletActionContext.getResponse();
            HttpSession session=request.getSession();
            //通过session 判断用户是否登录,没有则继续读取cookie
            if(session!=null&&session.getAttribute(SESSION_KEY)!=null)
                {
                    return invocation.invoke();
                    
                }
            Cookie[] cookies=request.getCookies();
            if(cookies!=null&&cookies.length!=0)
                {
                    for(Cookie cookie:cookies)
                        {
                            if(COOKIE_KEY.equals(cookie.getName()))
                                {
                                    String value=cookie.getValue();
                                    if(StringUtils.isNotBlank(value))
                                        {
                                            List<Huiyuan> huiyuans=baseDAO.findEntityProperty(Huiyuan.class, Expression.eq("yongHuMing", value));
                                            if(huiyuans.size()==0)
                                                {
                                                    cookie.setPath("/");
                                                    cookie.setValue(null);
                                                    
                                                    cookie.setMaxAge(0);
                                                    response.addCookie(cookie);
                                                    return "index";
                                                }
                                            if(huiyuans.get(0)!=null)
                                                {
                                                    
                                                    session.setAttribute(SESSION_KEY, huiyuans.get(0));
                                                    logger.info("读取cookie:继续");
                                                    return invocation.invoke();
                                                }
                                            
                                        }
                                }
                        }
                }
            //这里是关键点了 设置客户原来请求的url地址
            setToGoingURL(request,session, invocation);
            return "index";
        }
    private void setToGoingURL(HttpServletRequest request,HttpSession session,ActionInvocation invocation)
        {
            //如果referer不为空 直接使用它。如果为空我们分别获得命名空间,action名,以及请求参数
            //从新构造成一个URL保存在session中
            String url=request.getHeader("referer");
            logger.info("待转向URL:"+request.getHeader("referer"));
            if(url==null||url.equals(""))
                {
                    url="";
                    String path=request.getContextPath();
                    String actionName=invocation.getProxy().getActionName();
                    String nameSpace=invocation.getProxy().getNamespace();
                    if(StringUtils.isNotEmpty(nameSpace))
                        {
                            url=url+path+nameSpace;
                            
                        }
                    if(StringUtils.isNotEmpty(actionName))
                        {
                            url=url+"/"+actionName+".action"+"?";
                        }
                    
                    Map<String,String[]> zzMap=request.getParameterMap();
                    if(zzMap!=null)
                        {
                            for(String s:zzMap.keySet())
                                {
                                    String[] value=zzMap.get(s);
                                    for(String val:value)
                                        {
                                            url=url+s+"="+val+"&";
                                        }
                                    
                                }
                        }
                    logger.info("完整URL:"+url);
                }
            
                    session.setAttribute(GOTO_URL_KEY, url);
                
            
        }
    public void setBaseDAO(IBaseDAO baseDAO)
        {
            this.baseDAO = baseDAO;
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
一、什么是XSS攻击 XSS是一种经常出现在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中。比如这些代码包括HTML代码和客户端脚本。攻击者利用XSS漏洞旁路掉访问控制——例如同源策略(same origin policy)。这种类型的漏洞由于被黑客用来编写危害性更大的网络钓鱼(Phishing)攻击而变得广为人知。对于跨站脚本攻击,黑客界共识是:跨站脚本攻击是新型的“缓冲区溢出攻击“,而JavaScript是新型的“ShellCode”。 二、XSS漏洞的危害 (1)网络钓鱼,包括盗取各类用户账号; (2)窃取用户cookies资料,从而获取用户隐私信息,或利用用户身份进一步对网站执行操作; (3)劫持用户(浏览器)话,从而执行任意操作,例如进行非法转账、强制发表日志、发送电子邮件等; (4)强制弹出广告页面、刷流量等; (5)网页挂马; (6)进行恶意操作,例如任意篡改页面信息、删除文章等; (7)进行大量的客户端攻击,如DDoS攻击; (8)获取客户端信息,例如用户的浏览历史、真实IP、开放端口等; (9)控制受害者机器向其他网站发起攻击; (10)结合其他漏洞,如CSRF漏洞,实施进一步作恶; (11)提升用户权限,包括进一步渗透网站; (12)传播跨站脚本蠕虫等; 三、过滤器配置 web.xml配置 XssFilter com.xxx.Filter.XssFilter XssFilter /*
在Vue中实现登录后的页面跳转有多种方式,以下是其中一种常见的方法: 1. 首先,你需要在Vue项目中安装`vue-router`,可以通过以下命令进行安装: ```bash npm install vue-router ``` 2. 在你的Vue项目中创建一个路由文件(通常命名为`router.js`或者`index.js`),并配置路由信息。在路由配置中,你可以使用`beforeEach`导航守卫来检查用户是否已登录。如果用户已登录,则允许页面跳转,否则将用户重定向到登录页面。 ```javascript import Vue from 'vue'; import Router from 'vue-router'; import Home from './views/Home.vue'; import Login from './views/Login.vue'; Vue.use(Router); const router = new Router({ routes: [ { path: '/', name: 'home', component: Home, meta: { requiresAuth: true // 设置需要登录才能访问的页面 }, }, { path: '/login', name: 'login', component: Login, }, ], }); router.beforeEach((to, from, next) => { const isLoggedIn = ... // 根据你的登录状态判断用户是否已登录 if (to.matched.some(record => record.meta.requiresAuth) && !isLoggedIn) { next('/login'); // 如果需要登录才能访问的页面登录,则重定向到登录页 } else { next(); // 允许页面跳转 } }); export default router; ``` 3. 在主Vue实例中引入路由配置,并将路由挂载到Vue实例中: ```javascript import Vue from 'vue'; import App from './App.vue'; import router from './router'; new Vue({ router, render: h => h(App), }).$mount('#app'); ``` 在上述代码中,`Home`组件是需要登录才能访问的页面,`Login`组件是登录页面。通过设置路由的`meta`字段中的`requiresAuth`为`true`,我们标记了需要登录才能访问的页面。在导航守卫中,我们检查用户的登录状态,如果未登录且访问的页面需要登录权限,则将用户重定向到登录页面。 当用户成功登录后,你可以使用Vue Router提供的`router.push()`方法来实现页面跳转,例如: ```javascript // 登录成功后跳转到首页 this.$router.push('/'); ``` 这样,当用户登录后,页面自动跳转到首页。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值