Strut2使用拦截器过滤权限问题

本文介绍了一个使用Struts2框架实现的简单登录系统案例,包括JSP页面展示、Action处理逻辑、过滤器验证及视图跳转等内容。

JSP请求(login.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%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
	<script type="text/javascript">
		function abc(id){
			var buttonObj = document.getElementById(id);
			buttonObj.style.color="red";
		}
		function efg(id){
			var buttonObj = document.getElementById(id);
			buttonObj.style.color="black";
		}
	</script>
	
  </head>
  
  <body>
  	<div style="color:red" align="center">${requestScope.tip}</div>
    <form action="login.action" method="post">
    	<table align="center">
    		<caption><h2>用户登录</h2></caption>
    		<tr>
    			<td style="font-style: inherit;color: green">用户名:<input type="text" name="username" style="color: red;background: #fffddd" /></td>
    		</tr>
			<tr>
				<td style="font-style: inherit;color: green">密&nbsp;&nbsp;码:<input type="password" name="password" style="color: red;background: #fffddd"/></td>
			</tr>
			<tr align="center">
				<td colspan="2"><input id="1" onmouseover="javascript:abc(this.id)" onmouseout="javascript:efg(this.id)" style="color: black" type="submit" value="登录"/>
				<input type="reset" id="2" onmouseover="javascript:abc(this.id)" onmouseout="javascript:efg(this.id)" style="color: black" value="重填" /></td>
			</tr>
    	</table>
    </form>
    <div align="center"><a href="viewBook.action">
	查看图书</a><div>
  </body>
</html>
 

struts.xml文件 
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
	"http://struts.apache.org/dtds/struts-2.1.dtd">
<!-- struts是Struts 2配置文件的根元素 -->
<struts>
	
	<package name="login" namespace="/" extends="struts-default">
		<interceptors>
			<interceptor name="authority" class="com.lbx.interceptor.AuthorityInterceptor" />
		</interceptors>
		
		<global-results>
			<result name="login">/login.jsp</result>
		</global-results>
		
		<action name="login" class="com.lbx.action.LoginAction">
			<result>/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		
		<action name="viewBook">
			<!-- 返回success视图名时,转入/WEB-INF/jsp/viewBook.jsp页面 -->
			<result>/viewBook.jsp</result>
			<!-- 拦截器一般配置在result元素之后! -->
			<interceptor-ref name="defaultStack"/>
			<!-- 应用自定义拦截器 -->
			<interceptor-ref name="authority"/>
		</action>
		
	</package>

</struts>
 

User类 
package com.lbx.model;

public class User {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}
 

过滤器(实现AbstractInterceptor接口) 
package com.lbx.interceptor;
import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class AuthorityInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		//取得相关的ActionContext实例
		ActionContext ctx = invocation.getInvocationContext();
		Map session = ctx.getSession();
		
		String user = (String)session.get("user");
		if(user!=null && user.equals("libinxuan")){
			return invocation.invoke();
		}
		ctx.put("tip", "你还没有登录,请输入libinxuan登录系统");
		return Action.LOGIN;
	}

}
 

Action 
package com.lbx.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {

	private String username;
	private String password;

	// 封装处理结果的tip属性
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String execute() throws Exception {
		Thread.sleep(1500);
		if (getUsername().equals("libinxuan")) {
			ActionContext ctx = ActionContext.getContext();
			Map session = ctx.getSession();
			session.put("user", getUsername());
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

}
 
error.jsp部分代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"  %>
<%
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%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <s:debug></s:debug>
   	您不能登录!<br />
	<a href="viewBook.action">查看图书</a>
  </body>
</html>
 
success.jsp部分代码 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="ss" uri="/struts-tags" %>
<%
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%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <ss:debug></ss:debug>
  	 您已经登录!<br />
	<a href="viewBook.action">查看图书</a>
  </body>
</html>
 

viewBook.jsp部分代码
<%@ 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>作者的图书:</title>
</head>
<body>
	<h3>作者的图书:</h3>
	Java<br />
	Java EE<br />
	Ajax<br />
</body>
</html>
 



 

内容概要:本文系统阐述了企业新闻发稿在生成式引擎优化(GEO)时代下的全渠道策略与效果评估体系,涵盖当前企业传播面临的预算、资源、内容与效果评估四大挑战,并深入分析2025年新闻发稿行业五大趋势,包括AI驱动的智能化转型、精准化传播、首发内容价值提升、内容资产化及数据可视化。文章重点解析央媒、地方官媒、综合门户和自媒体四类媒体资源的特性、传播优势与发稿策略,提出基于内容适配性、时间节奏、话题设计的策略制定方法,并构建涵盖品牌价值、销售转化与GEO优化的多维评估框架。此外,结合“传声港”工具实操指南,提供AI智能投放、效果监测、自媒体管理与舆情应对的全流程解决方案,并针对科技、消费、B2B、区域品牌四大行业推出定制化发稿方案。; 适合人群:企业市场/公关负责人、品牌传播管理者、数字营销从业者及中小企业决策者,具备一定媒体传播经验并希望提升发稿效率与ROI的专业人士。; 使用场景及目标:①制定科学的新闻发稿策略,实现从“流量思维”向“价值思维”转型;②构建央媒定调、门户扩散、自媒体互动的立体化传播矩阵;③利用AI工具实现精准投放与GEO优化,提升品牌在AI搜索中的权威性与可见性;④通过数据驱动评估体系量化品牌影响力与销售转化效果。; 阅读建议:建议结合文中提供的实操清单、案例分析与工具指南进行系统学习,重点关注媒体适配性策略与GEO评估指标,在实际发稿中分阶段试点“AI+全渠道”组合策略,并定期复盘优化,以实现品牌传播的长期复利效应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值