欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

struts2如何使用攔截器進(jìn)行用戶權(quán)限控制實(shí)例

 更新時(shí)間:2017年05月09日 10:24:48   作者:阿木俠  
本篇文章主要介紹了struts2如何使用攔截器進(jìn)行用戶權(quán)限控制實(shí)例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

大多數(shù)網(wǎng)站會(huì)設(shè)置用戶權(quán)限,如過濾非法用戶,用戶不登錄時(shí)不能進(jìn)行訪問,或者設(shè)置訪問的權(quán)限,如部分內(nèi)容僅對VIP開放等等,這些權(quán)限的控制都可以用struts2中的攔截器來實(shí)現(xiàn)。

下面通過一個(gè)簡單的Demo來模擬這種用戶權(quán)限控制的實(shí)現(xiàn)流程,設(shè)定三種不同身份的用戶,commen為普通用戶,VIP為會(huì)員用戶,還有一種admin為管理員。

先看一下Demo的整體結(jié)構(gòu):

首先搭建struts2框架的開發(fā)環(huán)境(前面博客中有介紹),環(huán)境搭建完之后又再看一看如何配置struts.xml:

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
 "http://struts.apache.org/dtds/struts-2.3.dtd"> 
<struts> 
 <package name="hello" extends="struts-default" namespace="/"> 
  <interceptors> 
   <interceptor name="testInterceptor" class="org.interceptor.InterceptorTest"></interceptor> 
   <!-- 一個(gè)攔截器棧中可以定義多個(gè)攔截器 --> 
   <interceptor-stack name="testStack"> 
    <interceptor-ref name="testInterceptor" /> 
    <interceptor-ref name="defaultStack" /> 
   </interceptor-stack> 
  </interceptors> 
  <!--全局結(jié)果處理 --> 
  <global-results> 
   <result name="error">/Error.jsp</result> 
  </global-results> 
  <action name="login" class="org.interceptor.LoginAction"> 
   <result>/WEB-INF/pages/index.jsp</result> 
  </action> 
  <action name="admin" class="org.interceptor.LoginAction" method="AdminExecute"> 
   <interceptor-ref name="testStack"></interceptor-ref> 
   <result>/WEB-INF/pages/admin.jsp</result> 
  </action> 
  <action name="vip" class="org.interceptor.LoginAction" method="vipExecute"> 
   <interceptor-ref name="testStack"></interceptor-ref> 
   <result>/WEB-INF/pages/vipUser.jsp</result> 
  </action> 
  <action name="commen" class="org.interceptor.LoginAction" method="commenExecute"> 
   <interceptor-ref name="testStack"></interceptor-ref> 
   <result>/WEB-INF/pages/commen.jsp</result> 
  </action> 
 </package> 
</struts> 

 其中,<global-results></global-results>是全局的result,有很多時(shí)候一個(gè)<result>可供很多<action>使用,這時(shí)可以使用<global-results>標(biāo)簽來定義全局的<result>。執(zhí)行順序:當(dāng)一個(gè)Action返回的String沒有相應(yīng)的<result>與之對應(yīng),Struts2就會(huì)查找全局的<result>,所以本次模擬測試中不符合條件被攔截的請求都會(huì)轉(zhuǎn)到error.jsp。

Action類,不做處理,全部放行,讓攔截器處理:

public class LoginAction implements SessionAware{ 
 @SuppressWarnings("unused") 
 private String username; 
 private Map<String,Object> session; 
 public void setUsername(String username) { 
  this.username = username; 
  session.put("username", username); 
 } 
 public void setSession(Map<String, Object> session) { 
  // TODO Auto-generated method stub 
  this.session = session; 
 } 
  
 public String AdminExecute(){ 
  return "success"; 
 } 
 public String vipExecute(){ 
  return "success"; 
 } 
 public String commenExecute(){ 
  return "success"; 
 } 
 public String execute(){ 
  return "success"; 
 } 
} 

Inteceptor(攔截器類):

public class LoginAction implements SessionAware{ 
 @SuppressWarnings("unused") 
 private String username; 
 private Map<String,Object> session; 
 public void setUsername(String username) { 
  this.username = username; 
  session.put("username", username); 
 } 
 public void setSession(Map<String, Object> session) { 
  // TODO Auto-generated method stub 
  this.session = session; 
 } 
  
 public String AdminExecute(){ 
  return "success"; 
 } 
 public String vipExecute(){ 
  return "success"; 
 } 
 public String commenExecute(){ 
  return "success"; 
 } 
 public String execute(){ 
  return "success"; 
 } 
} 

 只是 模擬攔截器的實(shí)現(xiàn)思路,沒有持久層的數(shù)據(jù),這里的方法是使用invocation.getProxy().getActionName()方法來獲取struts.xml中配置的action名稱,和用戶表單提交的名稱做對比,如果輸入的用戶名是以action名開頭的,就放行,否則攔截。

登錄jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >  
 <title>login</title> 
 </head> 
 
 <body> 
 <form action="login.action"> 
  <input type="text" name="username"/> 
  <input type="password" name="password"/> 
  <input type="submit" value="login"> 
 </form> 
 </body> 
</html> 

攔截后跳轉(zhuǎn)頁:

<body> 
 <h4>你的權(quán)限不足,請先升級權(quán)限...</h4> 
 </body> 

訪問資源代碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" > 
 <title>index</title> 
 </head> 
 
 <body> 
 <a href="admin.action" rel="external nofollow" >admin</a><br/> 
 <a href="vip.action" rel="external nofollow" >vip</a><br/> 
 <a href="commen.action" rel="external nofollow" >commen</a> 
 </body> 
</html> 

其余admin.jsp等界面沒有內(nèi)容,只是為了區(qū)分實(shí)現(xiàn)跳轉(zhuǎn)頁面不同。

運(yùn)行結(jié)果:

使用commen角色登錄:

點(diǎn)擊VIP以及admin跳轉(zhuǎn)鏈接時(shí):

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java獲取隨機(jī)數(shù)的3種方法

    Java獲取隨機(jī)數(shù)的3種方法

    本篇文章主要介紹了Java獲取隨機(jī)數(shù)的3種方法,現(xiàn)在分享給大家,也給大家做個(gè)參考,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • java中對list分頁并顯示數(shù)據(jù)到頁面實(shí)例代碼

    java中對list分頁并顯示數(shù)據(jù)到頁面實(shí)例代碼

    這篇文章主要介紹了java中對list分頁并顯示數(shù)據(jù)到頁面實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • Java函數(shù)式編程之通過行為參數(shù)化傳遞代碼

    Java函數(shù)式編程之通過行為參數(shù)化傳遞代碼

    行為參數(shù)化就是可以幫助你處理頻繁變更的需求的一種軟件開發(fā)模式,這篇文章將給大家詳細(xì)的介紹一下Java函數(shù)式編程之行為參數(shù)化傳遞代碼,感興趣的同學(xué)可以參考閱讀下
    2023-08-08
  • Java中的@Conditional條件注解詳細(xì)解析

    Java中的@Conditional條件注解詳細(xì)解析

    這篇文章主要介紹了Java中的@Conditional條件注解詳細(xì)解析,@Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿足條件給容器注冊bean,需要的朋友可以參考下
    2023-11-11
  • 計(jì)算Java數(shù)組長度函數(shù)的方法以及代碼分析

    計(jì)算Java數(shù)組長度函數(shù)的方法以及代碼分析

    在本篇內(nèi)容里,小編給大家整理了關(guān)于計(jì)算Java數(shù)組長度函數(shù)的方法以及代碼分析內(nèi)容,有興趣的朋友么可以學(xué)習(xí)參考下。
    2022-11-11
  • Netty分布式ByteBuf中PooledByteBufAllocator剖析

    Netty分布式ByteBuf中PooledByteBufAllocator剖析

    這篇文章主要為大家介紹了Netty分布式ByteBuf剖析PooledByteBufAllocator簡述,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • Spring鉤子接口匯總分析使用示例

    Spring鉤子接口匯總分析使用示例

    Spring提供了非常多的擴(kuò)展接口,官方將這些接口稱之為鉤子,這些鉤子會(huì)在特定的時(shí)間被回調(diào),以此來增強(qiáng)Spring功能,眾多優(yōu)秀的框架也是通過擴(kuò)展這些接口,來實(shí)現(xiàn)自身特定的功能,如SpringBoot、mybatis等
    2023-01-01
  • SpringBoot整合Druid實(shí)現(xiàn)SQL監(jiān)控和數(shù)據(jù)庫密碼加密

    SpringBoot整合Druid實(shí)現(xiàn)SQL監(jiān)控和數(shù)據(jù)庫密碼加密

    Druid連接池是阿里巴巴開源的數(shù)據(jù)庫連接池項(xiàng)目,Druid連接池為監(jiān)控而生,內(nèi)置強(qiáng)大的監(jiān)控功能,監(jiān)控特性不影響性能,本文給大家介紹了SpringBoot整合Druid實(shí)現(xiàn)SQL監(jiān)控和數(shù)據(jù)庫密碼加密,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下
    2024-06-06
  • IDEA:Git stash 暫存分支修改的實(shí)現(xiàn)代碼

    IDEA:Git stash 暫存分支修改的實(shí)現(xiàn)代碼

    這篇文章主要介紹了IDEA:Git stash 暫存分支修改的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Java開發(fā)工具IntelliJ IDEA安裝圖解

    Java開發(fā)工具IntelliJ IDEA安裝圖解

    這篇文章主要介紹了Java開發(fā)工具IntelliJ IDEA安裝圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11

最新評論