日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Struts 拦截器权限控制【通过拦截器实现登录后跳转到登录前页面】

發(fā)布時間:2024/9/27 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts 拦截器权限控制【通过拦截器实现登录后跳转到登录前页面】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

應(yīng)用情形:在web項(xiàng)目中,經(jīng)常會遇到用戶未登錄或SESSION失效時用戶發(fā)出非法的權(quán)限操作,如新聞的評論、文件的下載等等,在此我們可以使用struts攔截器對該用戶發(fā)出的請求進(jìn)行攔截,攔截后判斷用戶是否登錄或SESSION是否有效,然后進(jìn)行其正常操作。具體實(shí)例如下:

新建一個攔截器類UserInterceptor ,UserInterceptor.java代碼如下

[java] view plaincopyprint?
  • package?com.hsinghsu.test.interceptor;??
  • ??
  • import?com.opensymphony.xwork2.*;??
  • import?com.opensymphony.xwork2.interceptor.AbstractInterceptor;??
  • import?java.util.*;??
  • import?javax.servlet.http.HttpServletRequest;??
  • import?org.apache.struts2.ServletActionContext;??
  • ??
  • public?class?UserInterceptor?extends?AbstractInterceptor?{??
  • ??
  • ????private?static?final?long?serialVersionUID?=?4956767125951165062L;??
  • ??
  • ????//?攔截Action處理的攔截方法??
  • ????public?String?intercept(ActionInvocation?invocation)?throws?Exception?{??
  • ??
  • ????????//?取得請求相關(guān)的ActionContext實(shí)例??
  • ????????ActionContext?ctx?=?invocation.getInvocationContext();??
  • ????????Map<String,?Object>?session?=?ctx.getSession();??
  • ????????//?取出名為user的Session屬性??
  • ????????String?user?=?(String)?session.get("user");??
  • ??
  • ????????//?如果已經(jīng)登錄,放行??
  • ????????if?(user?!=?null?&&?user.equals("hsing"))?{??
  • ????????????return?invocation.invoke();??
  • ????????}??
  • ??
  • ????????//?獲取HttpServletRequest對象??
  • ????????HttpServletRequest?req?=?ServletActionContext.getRequest();??
  • ??
  • ????????//?獲取此請求的地址??
  • ????????String?path?=?req.getRequestURI();??
  • ????????System.out.println("path:"?+?path);??
  • ????????//?存入session,方便調(diào)用??
  • ????????session.put("prePage",?path);??
  • ??
  • ????????//?沒有登錄,將服務(wù)器提示設(shè)置成一個HttpServletRequest屬性??
  • ????????ctx.put("tip",?"您還沒有登錄,請輸入hsing,hsu登錄系統(tǒng)");??
  • ??
  • ????????//?直接返回login的邏輯視圖??
  • ????????return?"login";??
  • ????}??
  • }??
  • package com.hsinghsu.test.interceptor;import com.opensymphony.xwork2.*; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; import java.util.*; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext;public class UserInterceptor extends AbstractInterceptor {private static final long serialVersionUID = 4956767125951165062L;// 攔截Action處理的攔截方法public String intercept(ActionInvocation invocation) throws Exception {// 取得請求相關(guān)的ActionContext實(shí)例ActionContext ctx = invocation.getInvocationContext();Map<String, Object> session = ctx.getSession();// 取出名為user的Session屬性String user = (String) session.get("user");// 如果已經(jīng)登錄,放行if (user != null && user.equals("hsing")) {return invocation.invoke();}// 獲取HttpServletRequest對象HttpServletRequest req = ServletActionContext.getRequest();// 獲取此請求的地址String path = req.getRequestURI();System.out.println("path:" + path);// 存入session,方便調(diào)用session.put("prePage", path);// 沒有登錄,將服務(wù)器提示設(shè)置成一個HttpServletRequest屬性ctx.put("tip", "您還沒有登錄,請輸入hsing,hsu登錄系統(tǒng)");// 直接返回login的邏輯視圖return "login";} }新建登錄action,LoginAction.java代碼如下: [java] view plaincopyprint?
  • package?com.hsinghsu.test.action;??
  • ??
  • import?com.opensymphony.xwork2.ActionSupport;??
  • import?com.opensymphony.xwork2.ActionContext;??
  • ??
  • import?java.util.*;??
  • ??
  • public?class?LoginAction?extends?ActionSupport?{??
  • ??
  • ????private?static?final?long?serialVersionUID?=?8013816027944871760L;??
  • ????private?String?username;//?登錄用戶名??
  • ????private?String?password;//?登錄密碼??
  • ????private?String?prePage;//?登錄前頁面??
  • ??
  • ????public?String?execute()?throws?Exception?{??
  • ??????????
  • ????????if?(null?!=?username?&&?null?!=?password?&&?username.equals("hsing")?&&?password.equals("hsu"))?{??
  • ??
  • ????????????ActionContext?ctx?=?ActionContext.getContext();??
  • ????????????Map<String,?Object>?session?=?ctx.getSession();??
  • ??????????????
  • ????????????//保存用戶信息session??
  • ????????????session.put("user",?getUsername());??
  • ??
  • ????????????//?獲取跳轉(zhuǎn)到登陸界面之前的頁面地址,由攔截器提供??
  • ????????????prePage?=?(String)?session.get("prePage");??
  • ??
  • ????????????//?清除session中的數(shù)據(jù)??
  • ????????????session.remove("prePage");??
  • ??
  • ????????????if?(null?==?prePage)?{??
  • ????????????????return?"usercenter";//?不是攔截器跳轉(zhuǎn)到登陸頁面的,直接訪問的登陸頁面??
  • ????????????}?else?{??
  • ????????????????return?SUCCESS;//?是攔截器跳轉(zhuǎn)到登陸登錄前頁面??
  • ????????????}??
  • ??
  • ????????}?else?{??
  • ????????????return?INPUT;??
  • ????????}??
  • ????}??
  • ??
  • ????public?void?setUsername(String?username)?{??
  • ????????this.username?=?username;??
  • ????}??
  • ??
  • ????public?String?getUsername()?{??
  • ????????return?this.username;??
  • ????}??
  • ??
  • ????public?void?setPassword(String?password)?{??
  • ????????this.password?=?password;??
  • ????}??
  • ??
  • ????public?String?getPassword()?{??
  • ????????return?this.password;??
  • ????}??
  • ??
  • ????public?String?getPrePage()?{??
  • ????????return?prePage;??
  • ????}??
  • ??
  • ????public?void?setPrePage(String?prePage)?{??
  • ????????this.prePage?=?prePage;??
  • ????}??
  • }??
  • package com.hsinghsu.test.action;import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ActionContext;import java.util.*;public class LoginAction extends ActionSupport {private static final long serialVersionUID = 8013816027944871760L;private String username;// 登錄用戶名private String password;// 登錄密碼private String prePage;// 登錄前頁面public String execute() throws Exception {if (null != username && null != password && username.equals("hsing") && password.equals("hsu")) {ActionContext ctx = ActionContext.getContext();Map<String, Object> session = ctx.getSession();//保存用戶信息sessionsession.put("user", getUsername());// 獲取跳轉(zhuǎn)到登陸界面之前的頁面地址,由攔截器提供prePage = (String) session.get("prePage");// 清除session中的數(shù)據(jù)session.remove("prePage");if (null == prePage) {return "usercenter";// 不是攔截器跳轉(zhuǎn)到登陸頁面的,直接訪問的登陸頁面} else {return SUCCESS;// 是攔截器跳轉(zhuǎn)到登陸登錄前頁面}} else {return INPUT;}}public void setUsername(String username) {this.username = username;}public String getUsername() {return this.username;}public void setPassword(String password) {this.password = password;}public String getPassword() {return this.password;}public String getPrePage() {return prePage;}public void setPrePage(String prePage) {this.prePage = prePage;} }配置攔截器與action映射關(guān)系,struts.xml代碼如下: [html] view plaincopyprint?
  • <?xml?version="1.0"?encoding="UTF-8"?>??
  • <!DOCTYPE?struts?PUBLIC??
  • ????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.1.7//EN"??
  • ????"http://struts.apache.org/dtds/struts-2.1.7.dtd">??
  • <struts>??
  • ????<constant?name="struts.custom.i18n.resources"?value="globalMessages"?/>??
  • ????<constant?name="struts.i18n.encoding"?value="UTF-8"?/>??
  • ??
  • ????<package?name="hsinghsu"?extends="struts-default">??
  • ??????
  • ????????<!--?用戶攔截器定義?-->??
  • ????????<interceptors>??
  • ????????????<interceptor?name="userInterceptor"?class="com.hsinghsu.test.interceptor.UserInterceptor"?/>??
  • ????????</interceptors>??
  • ??
  • ????????<!--?定義全局result?-->??
  • ????????<global-results>??
  • ????????????<result?name="login">/jsp/login.jsp</result>??
  • ????????</global-results>??
  • ??
  • ????????<action?name="loginPro"?class="com.hsinghsu.test.action.LoginAction">??
  • ????????????<result?name="success"?type="redirectAction">${prePage}</result>??
  • ????????????<result?name="input">/jsp/login.jsp</result>??
  • ????????????<result?name="usercenter">/jsp/userCenter.jsp</result>??
  • ????????</action>??
  • ??????????
  • ????????<action?name="productList">??
  • ????????????<result?name="success">/jsp/productList.jsp</result>??
  • ????????????<interceptor-ref?name="defaultStack"?/>?<!--?默認(rèn)攔截器?-->??
  • ????????????<interceptor-ref?name="userInterceptor"?/>?<!--?應(yīng)用自定義攔截器?-->??
  • ????????</action>??
  • ??????????
  • ????</package>??
  • </struts>??
  • <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts><constant name="struts.custom.i18n.resources" value="globalMessages" /><constant name="struts.i18n.encoding" value="UTF-8" /><package name="hsinghsu" extends="struts-default"><!-- 用戶攔截器定義 --><interceptors><interceptor name="userInterceptor" class="com.hsinghsu.test.interceptor.UserInterceptor" /></interceptors><!-- 定義全局result --><global-results><result name="login">/jsp/login.jsp</result></global-results><action name="loginPro" class="com.hsinghsu.test.action.LoginAction"><result name="success" type="redirectAction">${prePage}</result><result name="input">/jsp/login.jsp</result><result name="usercenter">/jsp/userCenter.jsp</result></action><action name="productList"><result name="success">/jsp/productList.jsp</result><interceptor-ref name="defaultStack" /> <!-- 默認(rèn)攔截器 --><interceptor-ref name="userInterceptor" /> <!-- 應(yīng)用自定義攔截器 --></action></package> </struts>登錄頁面login.jsp代碼如下: [html] view plaincopyprint?
  • <%@?page?contentType="text/html;?charset=utf-8"?language="java"??
  • ????errorPage=""%>??
  • <%@?taglib?prefix="s"?uri="/struts-tags"%>??
  • <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"??
  • ????"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">??
  • <html?xmlns="http://www.w3.org/1999/xhtml">??
  • <head>??
  • <title>登錄頁面</title>??
  • </head>??
  • <body>??
  • ????<h3>用戶登錄</h3>??
  • ????${tip}??
  • ????<s:form?action="loginPro">??
  • ????????<s:textfield?name="username"?label="用戶名"?/>??
  • ????????<s:password?name="password"?label="密碼"?/>??
  • ????????<s:submit?value="登錄"?/>??
  • ????</s:form>??
  • </body>??
  • </html>??
  • <%@ page contentType="text/html; charset=utf-8" language="java"errorPage=""%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>登錄頁面</title> </head> <body><h3>用戶登錄</h3>${tip}<s:form action="loginPro"><s:textfield name="username" label="用戶名" /><s:password name="password" label="密碼" /><s:submit value="登錄" /></s:form> </body> </html>產(chǎn)品列表頁面productList.jsp代碼如下: [html] view plaincopyprint?
  • <%@?page?contentType="text/html;?charset=utf-8"?language="java"??
  • ????errorPage=""%>??
  • <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"??
  • ????"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">??
  • <html?xmlns="http://www.w3.org/1999/xhtml">??
  • <head>??
  • <title>產(chǎn)品列表</title>??
  • </head>??
  • <body>??
  • ????<h2>水果:</h2>??
  • ????蘋果<br/>?橘子<br/>?香蕉<br/>??
  • </body>??
  • </html>??
  • <%@ page contentType="text/html; charset=utf-8" language="java"errorPage=""%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>產(chǎn)品列表</title> </head> <body><h2>水果:</h2>蘋果<br/> 橘子<br/> 香蕉<br/> </body> </html> 用戶中心userCenter.jsp代碼如下: [html] view plaincopyprint?
  • <%@?page?contentType="text/html;?charset=utf-8"?language="java"??
  • ????errorPage=""%>??
  • <%@?taglib?prefix="s"?uri="/struts-tags"%>??
  • <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"??
  • ????"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">??
  • <html?xmlns="http://www.w3.org/1999/xhtml">??
  • <head>??
  • <title>成功頁面</title>??
  • </head>??
  • <body>個人用戶中心,您已經(jīng)登錄!??
  • </body>??
  • </html>??
  • <%@ page contentType="text/html; charset=utf-8" language="java"errorPage=""%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>成功頁面</title> </head> <body>個人用戶中心,您已經(jīng)登錄! </body> </html>驗(yàn)證:
    情形一:若用戶未登錄,輸入http://localhost:8686/testInterceptor/productList.action
    則系統(tǒng)會自動跳轉(zhuǎn)到login.jsp頁面,進(jìn)行用戶登錄,登錄后系統(tǒng)會自動跳到productList.jsp前臺展現(xiàn)頁面。
    情形二:若用戶已登錄,輸入http://localhost:8686/testInterceptor/productList.action
    則系統(tǒng)直接跳轉(zhuǎn)到productList.jsp前臺展現(xiàn)頁面。
    情形三:若用戶未登錄,輸入http://localhost:8686/testInterceptor/testInterceptor/loginPro.action
    則系統(tǒng)會自動跳轉(zhuǎn)到login.jsp頁面,進(jìn)行用戶登錄,登錄后系統(tǒng)會自動跳到userCenter.jsp前臺展現(xiàn)頁面。

    總結(jié)

    以上是生活随笔為你收集整理的Struts 拦截器权限控制【通过拦截器实现登录后跳转到登录前页面】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。