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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

struts实战--登陆拦截器

發布時間:2025/3/20 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 struts实战--登陆拦截器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
登陸校驗攔截器 ?? ??? ??? ?

??? 一、概述

??????? 功能:用戶只有登錄成功后,才可以進行操作.


???
??? 二、實現
???

?? ?? ? ? ? 1).創建一個類,實現Interceptor接口

?????????????? 1、判斷用戶user是否為空

?????????????? 2、如果為空,則設置哪些方法可以不用登陸就可以訪問

package com.sihai.user.web.interceptor;import com.sihai.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.interceptor.AbstractInterceptor;/*** 登陸校驗攔截器* * * */ public class LoginInterceptor extends AbstractInterceptor {private String excludeMethods;@Overridepublic String intercept(ActionInvocation actionInvocation) throws Exception {// 判斷session中是否含有 user對象if (ServletActionContext.getRequest().getSession().getAttribute("user") == null) {// 未登陸 ---- 判斷是否配置放行if (excludeMethods != null) {String[] methods = excludeMethods.split(",");// 判斷methods(放行數組) 是否包含當前 訪問String currentMethod = ActionContext.getContext().getName(); // user_loginfor (String method : methods) {if (method.equals(currentMethod)) {// 當前method 被配置放行return actionInvocation.invoke();}}}// 設置錯誤信息,ActionSupport提供方法ActionSupport action = (ActionSupport) actionInvocation.getAction();action.addActionError(action.getText("nologin"));return "login";// 登陸視圖} else {// 已經登陸return actionInvocation.invoke();}}public void setExcludeMethods(String excludeMethods) {this.excludeMethods = excludeMethods;}}

?? ??? ?


?? ??? ? 2).在struts.xml文件注冊

注冊攔截器

<interceptors><!-- 注冊 --><interceptor name="login" class="com.sihai.user.web.interceptor.LoginInterceptor"></interceptor><interceptor name="myexception" class="com.sihai.user.web.interceptor.MyExceptionInterceptor"></interceptor><interceptor-stack name="myStack"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="login"></interceptor-ref><interceptor-ref name="myexception"></interceptor-ref></interceptor-stack></interceptors>

使用攔截器

添加參數設置放行方法

<!-- 配置攔截器參數 --><interceptor-ref name="myStack"><!-- 向攔截器 傳遞參數 ,多個參數 用, 分隔--><param name="login.excludeMethods">user_login</param></interceptor-ref>

?? ??

總結

以上是生活随笔為你收集整理的struts实战--登陆拦截器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。