如果是说在JSP页面加上代码也不用说,
是否可以通过过滤器或者拦截器去禁止访问,我想这肯定是可以的。只是怎么做?
11 个解决方案
#1
你写个struts2自定义过滤器在到struts配置文件中配置一下就好了,不清楚可以google “struts2 自定义过滤器”
#2
如果我会就不用发帖求助了,我也知道思想,但是我不会写啊
#3
添加一个过滤器配置啊, 在过滤器里面,可以进行一些判断, 比如不是从主页进来,没有登录等一些条件下, 则
不能访问某些jsp 页面。
#4
唉,我要的是实例代码啊
#5
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 过滤器使用:用户没用登录,不可以在浏览器输入地址访问页面
* @author Administrator
*
*/
public class OnlineFilter extends HttpServlet implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
RequestDispatcher dispatcher = request.getRequestDispatcher("Login.jsp");
HttpServletRequest req =(HttpServletRequest)request;
HttpServletResponse res =(HttpServletResponse)response;
HttpSession session =req.getSession(true);
//从session 里面获取用户名的信息
String user =(String)session.getAttribute("user");
//判断如果没有取到用户信息,就跳转到登陆页面,提示用户进行登陆
if(user == null || "".equals(user)){
//跳转到登陆的页面,进行用户登录
dispatcher.forward(request,response);
System.out.println("用户没有登录,请登陆!");
}else{
System.out.println("用户已经登陆成功,允许继续操作!");
}
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void init() throws ServletException {
}
}
#6
谢谢上楼,我也刚刚找到了,贴出完整一点的,
Java代码
web.xml配置
<filter>
<filter-name>jspFilter</filter-name>
<filter-class>com.cb.cbms.coreserver.servlet.JspFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>jspFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Java代码
package com.cb.cbms.coreserver.servlet;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 不允许直接访问jsp
* 所有对jsp的直接访问,跳转到首页面
* @author eason
*
*/
public class JspFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
String url = httpServletRequest.getRequestURI();
if(url != null && url.endsWith(".jsp")) {
httpServletResponse.sendRedirect(httpServletRequest.getContextPath());
return;
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
Java代码
web.xml配置
<filter>
<filter-name>jspFilter</filter-name>
<filter-class>com.cb.cbms.coreserver.servlet.JspFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>jspFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Java代码
package com.cb.cbms.coreserver.servlet;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 不允许直接访问jsp
* 所有对jsp的直接访问,跳转到首页面
* @author eason
*
*/
public class JspFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
String url = httpServletRequest.getRequestURI();
if(url != null && url.endsWith(".jsp")) {
httpServletResponse.sendRedirect(httpServletRequest.getContextPath());
return;
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
#7
Filter源码:
web.xml配置
package com.filterdemo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MyFilter1
implements Filter
{
public void destroy()
{
System.out.println("MyFilter1 destroy...");
}
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException
{
System.out.println("MyFilter1 doFilter...");
System.out.println("You can not come into jsp page derectly.");
arg1.setContentType("text/html");
arg1.setCharacterEncoding("utf-8");
PrintWriter out = arg1.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Filter测试</title>");
out.println("</head>");
out.println("</html>");
out.println("<body>");
out.println("不允许直接访问JSP页面!");
out.println("</bidy>");
out.println("</html>");
}
public void init(FilterConfig arg0)
throws ServletException
{
System.out.println("MyFilter1 init...");
}
}
web.xml配置
<filter>
<filter-name>MyFilter1</filter-name>
<filter-class>com.filterdemo.MyFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter1</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
#8
package base;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyInterceptor extends MethodFilterInterceptor{
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("Action的类:"+invocation.getAction().getClass().getName());
System.out.println("Action的方法:"+invocation.getProxy().getMethod());
System.out.println("我的拦截器被执行了。。。。");
// ServletActionContext.getResponse().sendRedirect();
return invocation.invoke();
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="struts2" extends="struts-default">
<!-- 定义声明拦截器 -->
<interceptors>
<interceptor name="myinter"
class="base.MyInterceptor">
</interceptor>
<!-- 配置拦截器栈 -->
<interceptor-stack name="myInterceptorStack">
<!-- 引用拦截器 -->
<interceptor-ref name="myinter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
</package>
</struts>
这个够详细了吧,copy上去可以直接运行
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyInterceptor extends MethodFilterInterceptor{
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("Action的类:"+invocation.getAction().getClass().getName());
System.out.println("Action的方法:"+invocation.getProxy().getMethod());
System.out.println("我的拦截器被执行了。。。。");
// ServletActionContext.getResponse().sendRedirect();
return invocation.invoke();
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="struts2" extends="struts-default">
<!-- 定义声明拦截器 -->
<interceptors>
<interceptor name="myinter"
class="base.MyInterceptor">
</interceptor>
<!-- 配置拦截器栈 -->
<interceptor-stack name="myInterceptorStack">
<!-- 引用拦截器 -->
<interceptor-ref name="myinter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
</package>
</struts>
这个够详细了吧,copy上去可以直接运行
#9
struts.xml在最近一句内容<default-interceptor-ref name="myInterceptorStack">
</default-interceptor-ref>
</default-interceptor-ref>
#10
#11
如果是多用户怎么办?
#1
你写个struts2自定义过滤器在到struts配置文件中配置一下就好了,不清楚可以google “struts2 自定义过滤器”
#2
如果我会就不用发帖求助了,我也知道思想,但是我不会写啊
#3
添加一个过滤器配置啊, 在过滤器里面,可以进行一些判断, 比如不是从主页进来,没有登录等一些条件下, 则
不能访问某些jsp 页面。
#4
唉,我要的是实例代码啊
#5
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 过滤器使用:用户没用登录,不可以在浏览器输入地址访问页面
* @author Administrator
*
*/
public class OnlineFilter extends HttpServlet implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
RequestDispatcher dispatcher = request.getRequestDispatcher("Login.jsp");
HttpServletRequest req =(HttpServletRequest)request;
HttpServletResponse res =(HttpServletResponse)response;
HttpSession session =req.getSession(true);
//从session 里面获取用户名的信息
String user =(String)session.getAttribute("user");
//判断如果没有取到用户信息,就跳转到登陆页面,提示用户进行登陆
if(user == null || "".equals(user)){
//跳转到登陆的页面,进行用户登录
dispatcher.forward(request,response);
System.out.println("用户没有登录,请登陆!");
}else{
System.out.println("用户已经登陆成功,允许继续操作!");
}
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void init() throws ServletException {
}
}
#6
谢谢上楼,我也刚刚找到了,贴出完整一点的,
Java代码
web.xml配置
<filter>
<filter-name>jspFilter</filter-name>
<filter-class>com.cb.cbms.coreserver.servlet.JspFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>jspFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Java代码
package com.cb.cbms.coreserver.servlet;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 不允许直接访问jsp
* 所有对jsp的直接访问,跳转到首页面
* @author eason
*
*/
public class JspFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
String url = httpServletRequest.getRequestURI();
if(url != null && url.endsWith(".jsp")) {
httpServletResponse.sendRedirect(httpServletRequest.getContextPath());
return;
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
Java代码
web.xml配置
<filter>
<filter-name>jspFilter</filter-name>
<filter-class>com.cb.cbms.coreserver.servlet.JspFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>jspFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Java代码
package com.cb.cbms.coreserver.servlet;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 不允许直接访问jsp
* 所有对jsp的直接访问,跳转到首页面
* @author eason
*
*/
public class JspFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
String url = httpServletRequest.getRequestURI();
if(url != null && url.endsWith(".jsp")) {
httpServletResponse.sendRedirect(httpServletRequest.getContextPath());
return;
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
#7
Filter源码:
web.xml配置
package com.filterdemo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MyFilter1
implements Filter
{
public void destroy()
{
System.out.println("MyFilter1 destroy...");
}
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException
{
System.out.println("MyFilter1 doFilter...");
System.out.println("You can not come into jsp page derectly.");
arg1.setContentType("text/html");
arg1.setCharacterEncoding("utf-8");
PrintWriter out = arg1.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Filter测试</title>");
out.println("</head>");
out.println("</html>");
out.println("<body>");
out.println("不允许直接访问JSP页面!");
out.println("</bidy>");
out.println("</html>");
}
public void init(FilterConfig arg0)
throws ServletException
{
System.out.println("MyFilter1 init...");
}
}
web.xml配置
<filter>
<filter-name>MyFilter1</filter-name>
<filter-class>com.filterdemo.MyFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter1</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
#8
package base;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyInterceptor extends MethodFilterInterceptor{
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("Action的类:"+invocation.getAction().getClass().getName());
System.out.println("Action的方法:"+invocation.getProxy().getMethod());
System.out.println("我的拦截器被执行了。。。。");
// ServletActionContext.getResponse().sendRedirect();
return invocation.invoke();
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="struts2" extends="struts-default">
<!-- 定义声明拦截器 -->
<interceptors>
<interceptor name="myinter"
class="base.MyInterceptor">
</interceptor>
<!-- 配置拦截器栈 -->
<interceptor-stack name="myInterceptorStack">
<!-- 引用拦截器 -->
<interceptor-ref name="myinter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
</package>
</struts>
这个够详细了吧,copy上去可以直接运行
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyInterceptor extends MethodFilterInterceptor{
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("Action的类:"+invocation.getAction().getClass().getName());
System.out.println("Action的方法:"+invocation.getProxy().getMethod());
System.out.println("我的拦截器被执行了。。。。");
// ServletActionContext.getResponse().sendRedirect();
return invocation.invoke();
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="struts2" extends="struts-default">
<!-- 定义声明拦截器 -->
<interceptors>
<interceptor name="myinter"
class="base.MyInterceptor">
</interceptor>
<!-- 配置拦截器栈 -->
<interceptor-stack name="myInterceptorStack">
<!-- 引用拦截器 -->
<interceptor-ref name="myinter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
</package>
</struts>
这个够详细了吧,copy上去可以直接运行
#9
struts.xml在最近一句内容<default-interceptor-ref name="myInterceptorStack">
</default-interceptor-ref>
</default-interceptor-ref>
#10
#11
如果是多用户怎么办?