Java solves the problem of entering url access action in the browser address bar and the simple implementation of blocking method Filtering

Source: Internet
Author: User

 

For Struts2, Spring3, and Hibernate3 integrated projects, user request control is very important, and some operations need to be performed after the user logs on. If no restrictions are imposed, the action can be directly entered in the browser to execute the corresponding action.

① Question about directly entering url in the address bar of the browser to access the action

② Filter methods that do not want to be intercepted

For example, for the following link:

 

 
 
  1. http://localhost:8080/absSys/delete.action?id=1 

Submit the above address directly in the browser, and the delete operation can still be executed! Therefore, requests submitted by users must be intercepted. If the user does not log on, the logon page is displayed.

I. Question about directly entering url in the address bar of the browser to access the action

Struts2 provides an interceptor. When writing our own interceptor, we only need to inherit the abstract class AbstractInterceptor and then override intercept.

 
 
  1. Import java. util. Map;
  2. Import com. opensymphony. xwork2.ActionContext;
  3. Import com. opensymphony. xwork2.ActionInvocation;
  4. Import com. opensymphony. xwork2.interceptor. AbstractInterceptor;
  5.  
  6. Public class LoginInterceptor extends actinterceptor {
  7.  
  8. Private static final long serialVersionUID = 1L;
  9.  
  10. Public void destroy (){
  11.  
  12. System. out. println ("Destory ");
  13. }
  14.  
  15. Public void init (){
  16.  
  17. System. out. println ("Init ");
  18. }
  19.  
  20. /**
  21. * @ Return result
  22. **/
  23. Public String intercept (ActionInvocation invocation) throws Exception {
  24. System. out. println ("before the action is executed ");
  25. String name = invocation. getInvocationContext (). getName ();
  26. System. out. println ("Request Method:" + name );
  27. ActionContext ac = invocation. getInvocationContext ();
  28. Map <String, Object> session = ac. getSession ();
  29. Boolean allow = name. equals ("infolist") | name. equals ("xwgg ")
  30. | Name. equals ("pxdt") | name. equals ("noticethrid ")
  31. | Name. equals ("gqpx") | name. equals ("gp ")
  32. | Name. equals ("np") | name. equals ("sp ")
  33. | Name. equals ("wypx") | name. equals ("zgks ")
  34. | Name. equals ("shpx") | name. equals ("fwxz ")
  35. | Name. equals ("noticesec") | name. equals ("filedown ");
  36. If (name. equals ("login") | allow ){
  37. // If the user wants to log on or execute the allow method, the user will not intercept it so that it passes
  38. // Invocation. invoke () continues to run the interceptor for subsequent processing
  39.  
  40. Return invocation. invoke ();
  41.  
  42. } Else {
  43. If (session. isEmpty () | session = null ){
  44. // If the session is empty, log on to the user.
  45. Return "login ";
  46. } Else {
  47. String userId = session. get ("userId"). toString ();
  48. If (userId = null ){
  49. // The session is not empty, but there is no user information in the session.
  50. // Let the user log on
  51. Return "login ";
  52. } Else {
  53. // The user has logged on. login successful
  54. Return invocation. invoke ();
  55. }
  56. }
  57. }
  58. }
  59. }

 

2. filter methods that do not want to be intercepted

In the default configuration, all methods are intercepted. For some methods that do not need to be intercepted, special processing is required.

 

In the LoginInterceptor class:

 

 
 
  1. String name = invocation.getInvocationContext().getName(); 

 

This name is the method for obtaining the request. Because we may not need to intercept some methods, let them directly continue the subsequent processing operations. In this common situation, we do not need to intercept the query action on the homepage of the front-end. We can define methods that do not need to be intercepted in allow or in the configuration file. This article defines a boolean variable allow for convenience.

 

 
 
  1. if (name.equals("login") || allow) { 
  2.             return invocation.invoke(); 
  3.         } 

Invocation. invoke (); continues subsequent operations.

 

3. Modify the Struts. xml configuration file and add the interceptor Configuration

The Struts. xml configuration is as follows:

 

 
 
  1. <package name="author" namespace="/author" extends="struts-default"> 
  2.  
  3.         <interceptors> 
  4.             <interceptor name="login" class="com.xxx.util.LoginInterceptor"></interceptor> 
  5.             <interceptor-stack name="loginCheck"> 
  6.                 <interceptor-ref name="login"></interceptor-ref> 
  7.                 <interceptor-ref name="defaultStack"></interceptor-ref> 
  8.             </interceptor-stack> 
  9.         </interceptors> 
  10.         <default-interceptor-ref name="loginCheck"></default-interceptor-ref> 
  11.  
  12.         <global-results> 
  13.             <result name="login" type="redirect">/login.jsp</result> 
  14.             <result name="illegal" type="redirect">/illegal.jsp</result> 
  15.         </global-results> 
  16.          
  17.     </package> 

The configuration of the interceptor can be added to the package to be intercepted or placed in a package, then other packages inherit our author package.

 

 
 
  1. <package name="User" namespace="/user" extends="struts-default,author"> 

 

Ps: You can also configure it in the specified action.

 
 
  1. <interceptor-ref name="loginCheck"></interceptor-ref>  

This interceptor, but this configuration is required for every action to be intercepted. It is complicated for a large number of configuration files, so we only need to define a package configuration.

In addition, the interceptor method filtering can also inheritMethodFilterInterceptor class to implement,

See method filtering of Struts2 interceptor

The method mentioned in this article is relatively simple.MethodFilterInterceptorThe method of this class is more complicated.

This article from the ghost Conan technology blog, please be sure to keep this source http://enetq.blog.51cto.com/479739/1179856

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.