20171106_chr_downInterceptor 自定义拦截器实现下载前登陆拦截
2017-12-15 13:09
190 查看
自定义拦截器实现下载前登陆拦截
/20171106_chr_downInterce4000
ptor/src/nuc/sw/action/DownloadAction.java
package nuc.sw.action; import com.opensymphony.xwork2.ActionSupport; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.UnsupportedEncodingException; public class DownloadAction extends ActionSupport { private String inputPath; private String contentType; private String downFileName; public String getContentType() { return contentType; } public String getDownFileName() { return downFileName; } public String getInputPath() { return inputPath; } public void setContentType(String contentType) { this.contentType = contentType; } public void setDownFileName(String downFileName) throws UnsupportedEncodingException { this.downFileName = new String(downFileName.getBytes("iso8859-1"),"utf-8"); } public void setInputPath(String inputPath) throws UnsupportedEncodingException { this.inputPath = new String(inputPath.getBytes("iso8859-1"),"utf-8"); } public InputStream getTargetFile() { InputStream is = null; try { is = new FileInputStream(inputPath); } catch (FileNotFoundException e) { e.printStackTrace(); } return is; } }
/20171106_chr_downInterceptor/src/nuc/sw/action/LoginAction.java
package nuc.sw.action; import java.util.regex.Pattern; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { //属性驱动校验 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; } //手动检验 @Override public void validate() { // TODO Auto-generated method stub //进行数据校验,长度6~15位 if(username.trim().length()<6||username.trim().length()>15||username==null) { this.addFieldError("username", "用户名长度不合法!"); } if(password.trim().length()<6||password.trim().length()>15||password==null) { this.addFieldError("password", "密码长度不合法!"); } } //登陆业务逻辑 public String loginMethod() { if(username.equals("chenghaoran")&&password.equals("12345678")) { ActionContext.getContext().getSession().put("user", username); return "loginOK"; }else { this.addFieldError("err","用户名或密码不正确!"); return "loginFail"; } } //手动校验validateXxx public void validateLoginMethod() { //使用正则校验 if(username==null||username.trim().equals("")) { this.addFieldError("username","用户名不能为空!"); }else { if(!Pattern.matches("[a-zA-Z]{6,15}", username.trim())) { this.addFieldError("username", "用户名格式错误!"); } } if(password==null||password.trim().equals("")) { this.addFieldError("password","密码不能为空!"); }else { if(!Pattern.matches("\\d{6,15}", password.trim())) { this.addFieldError("password", "密码格式错误!"); } } } }
/20171106_chr_downInterceptor/src/nuc/sw/interceptor/LoginInterceptor.java
package nuc.sw.interceptor; import com.opensymphony.xwork2.Action; 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 { @Override public String intercept(ActionInvocation arg0) throws Exception { // TODO Auto-generated method stub //判断是否登陆,通过ActionContext访问Session ActionContext ac=arg0.getInvocationContext(); String username=(String)ac.getSession().get("user"); if(username!=null&&username.equals("chenghaoran")) { return arg0.invoke();//放行 }else { ((ActionSupport)arg0.getAction()).addActionError("请先登录!"); return Action.LOGIN; } } }
/20171106_chr_downInterceptor/src/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 extends="struts-default" namespace="/" name="download"> <interceptors> <interceptor name="login" class="nuc.sw.interceptor.LoginInterceptor"></interceptor> </interceptors> <action name="download" class="nuc.sw.action.DownloadAction"> <interceptor-ref name="login"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <result name="login"> /login.jsp </result> <result type="stream"> <param name="contentType">${contentType}</param> <param name="inputName">targetFile</param> <param name="contentDisposition">attachment;filename${downFileName}</param> </result> </action> <action name="loginAction" class="nuc.sw.action.LoginAction" method="loginMethod"> <result name="loginOK"> /download.jsp </result> <result name="loginFail"> /login.jsp </result> <result name="input"> /login.jsp </result> </action> </package> </struts>
/20171106_chr_downInterceptor/WebContent/download.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title& ab3a gt;下载页</title> </head> <body> <a href="download?inputPath=f:/123.txt&contentType=text/plain&downFileName=123.txt">struts2下载文件</a> </body> </html>
/20171106_chr_downInterceptor/WebContent/login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录页</title> <s:head/> </head> <body> <s:actionerror/> <s:fielderror fieldName="err"></s:fielderror> <s:form action="loginAction" method="post"> <s:textfield label="用户名" name="username"></s:textfield> <s:password label="密码" name="password"></s:password> <s:submit value="登陆"></s:submit> </s:form> </body> </html>
相关文章推荐
- SpringMVC拦截器实现登陆时的验证拦截
- springmvc 自定义拦截器实现未登录用户的拦截
- JAVAEE——struts2_04:自定义拦截器、struts2标签、登陆功能和校验登陆拦截器的实现
- Struts拦截器实现拦截未登陆用户实例解析
- spring 自定义注解实现登陆拦截
- Struts2自定义拦截器,实现发表文章检查是否已登陆功能
- 用struts2拦截器的实现(拦截未登陆用户)
- java中,自定义注解拦截器来实现,在需要的拦截的方法上面加上一个注解@AccessRequired
- 通过Spring MVC 的自定义拦截器实现灵活的登录拦截
- 通过Spring MVC 的自定义拦截器实现灵活的登录拦截
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- 在linux shell中使用ftp命令来实现自动登陆、上传与下载
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Spring mvc拦截器实现登录验证拦截
- Asp.net Web Applicatoin实现自定义HttpModule拦截异常处理
- js实现文件下载自定义下载路径
- 自定义注解+拦截器实现权限控制
- springmvc自定义注解拦截器方式实现注解功能拦截器的配置
- 使文件下载的自定义连接支持 FlashGet 的断点续传多线程链接下载! C#/ASP.Net 实现!
- 如何使 FlashGet "正常合法" 下载 Session 中的自定义文件链接呢? JSP/Servlet 实现!