打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java用户登录拦截判断

用maven生成了appfuse的webssh项目里面东西比较多调查了下做个记录。

web应用中,我们经常使用filter机制来确定外部请求的用户是否登录,角色是否合法等。spring提供了
一个DelegatingFilterProxy机制来完成filter的部分功能。通过例子做一个简单对比。   二者看起来
没有什么太大的区别,在spring中,filter被纳入了bean 管理机制。

1.servlet filter
* LoginFilter .Java 

  1. package servlet.filter;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4. import java.io.*;  
  5. public class LoginFilter implements Filter {  
  6. protected FilterConfig filterConfig;  
  7. public void init(FilterConfig filterConfig) throws ServletException {  
  8. this.filterConfig = filterConfig;  
  9. }  
  10. public void destroy() {  
  11. this.filterConfig = null;  
  12. }  
  13. public void doFilter(ServletRequest request, ServletResponse response,  
  14. FilterChain chain) throws java.io.IOException, ServletException {  
  15. HttpServletRequest req = (HttpServletRequest) request;  
  16. HttpServletResponse res = (HttpServletResponse) response;  
  17. String username = req.getParameter("j_username");  
  18. System.out.println("this is in servlet filter");  
  19. chain.doFilter(request, response);  
  20. }  
  21. }  
*web.xml
  1. <filter>  
  2. <filter-name>simplelogin</filter-name>  
  3. <filter-class>servlet.filter.LoginFilter</filter-class>  
  4. <init-param>  
  5. <param-name>hello</param-name>  
  6. <param-value>hello filter</param-value>  
  7. </init-param>  
  8. </filter>  
  9. <filter-mapping>  
  10. <filter-name>simplelogin</filter-name>  
  11. <url-pattern>/*</url-pattern>  
  12. </filter-mapping>  
2. Spring filter 
*LoginFilter.java
  1. package filter;  
  2. import java.io.IOException;  
  3. import java.security.Principal;  
  4. import javax.servlet.*;  
  5. import javax.servlet.http.*;  
  6. import org.apache.commons.logging.*;  
  7. public class LoginFilter implements Filter {  
  8. private FilterConfig filterConfig = null;  
  9. Log log = LogFactory.getLog(LoginFilter.class);  
  10. public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {  
  11. HttpServletRequest request = (HttpServletRequest) req;  
  12. Principal principal=request.getUserPrincipal();  
  13. HttpServletResponse response=(HttpServletResponse)res;   
  14. log.info("filter user name:"+request.getParameter("j_username")+":"+principal.toString());  
  15. log.info("filter password :"+request.getParameter("j_password"));  
  16. chain.doFilter(req,res);  
  17. }  
  18. public void init(FilterConfig config) throws ServletException {  
  19. this.filterConfig = config;  
  20. }  
  21. public void destroy() {  
  22. filterConfig = null;  
  23. }  
  24. }  
*
web.xml
  1. <filter>  
  2. <description>LoginFilter</description>  
  3. <display-name> LoginFilter</display-name>  
  4. <filter-name>LoginFilter</filter-name>  
  5. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  6. <init-param>  
  7. <description>  
  8. </description>  
  9. <param-name>targetBeanName</param-name>  
  10. <param-value>loginFilter</param-value>  
  11. </init-param>  
  12. </filter>  
  13. <filter-mapping>  
  14. <filter-name>LoginFilter</filter-name>  
  15. <url-pattern>/*</url-pattern>  
  16. </filter-mapping>  
*spring context config
  1. <bean id="loginFilter" class="filter.LoginFilter" />  
3.struts2 的拦截器

  1. <interceptors>  
  2.     <!-- 登录验证拦截器 -->  
  3.     <interceptor name="authority"  
  4.         class="com.bolo.examples.common.interceptor.AuthorityInterceptor" />  
  5.     <!-- 自定义拦截器栈 -->  
  6.     <interceptor-stack name="boloStack">  
  7.         <!-- 将登录验证拦截器加入默认的拦截器栈中 -->  
  8.         <interceptor-ref name="authority">  
  9.             <param name="excludeMethods">welcome,login</param>  
  10.         </interceptor-ref>  
  11.         <interceptor-ref name="defaultStack"></interceptor-ref>  
  12.     </interceptor-stack>  
  13. </interceptors>  
  14. <!-- 将自定义拦截器栈设置默认的全局拦截器 -->  
  15. <default-interceptor-ref name="boloStack" />  
  16. <!-- 全局跳转页面 -->  
  17. <global-results>  
  18.     <result name="login">/jump.jsp</result>  
  19. </global-results>  

Interceptor类

  1. /** 
  2.  * 登录验证拦截器 
  3.  * @author 菠萝大象 
  4.  */  
  5. public class AuthorityInterceptor extends MethodFilterInterceptor{  
  6.   
  7.     @Override  
  8.     protected String doIntercept(ActionInvocation actioninvocation) throws Exception {  
  9.         Object user = ServletActionContext.getRequest().getSession().getAttribute("user");  
  10.         if(user != null){  
  11.             return actioninvocation.invoke(); //递归调用拦截器  
  12.         }else{  
  13.             return Action.LOGIN; //返回到登录页面  
  14.         }  
  15.     }  
  16. }  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
登录过滤器(Struts2)
java SSH中防止非法登录 过滤器的使用
Spring MVC防御CSRF、XSS和SQL注入攻击
J2EE设计模式浅谈(2)
Servlet中读取参数中文乱码的一种解决方法
Filter 过滤器
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服