SSM project, using interceptors for permission control

Source: Internet
Author: User
Tags tojson

Interceptor definition

public class Permissioninterceptor extends Handlerinterceptoradapter {@Autowired private adminuserroleservice ad

    Minuserroleservice; @Override public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler) throws Ex
        ception {Adminuser Adminuser = (adminuser) request.getsession (). getattribute ("Adminuser"); If the user has not logged in, let the user log in if (Adminuser = null) {//returns the insufficient information in JSON format if (commonutils.isempty) (requ
            Est.getheader ("X-requested-with")) {Response.getwriter (). Print ("Need to log back in");
            else {response.getwriter (). Print (Jsonutils.tojson ajaxresult.errorinstance ("need to log back in"));
        return false;
        }//Request path String Servletpath = Request.getservletpath ();
        Check permissions Boolean result = Adminuserroleservice.checkpermission (Adminuser.getid (), Servletpath);
 if (Result) {return true;       else {//returns insufficient information in JSON format if (Commonutils.isempty request.getheader ("X-requested-with"))
            {Response.getwriter (). Print ("Insufficient permissions");
            else {response.getwriter (). Print (Jsonutils.tojson (ajaxresult.errorinstance ("Insufficient Permissions"));
        return false; }
    }
}

Interceptor Configuration

<mvc:interceptors>  
                       
            <!--Administrator rights interceptor-->
            <mvc:interceptor>
                <mvc:mapping path= "/ adminuser/* "/>
                ..... <mvc:exclude-mapping path= "/adminuser/login.do"/>
                <mvc:exclude-mapping path= "/adminUser/logout.do"/ >
                <mvc:exclude-mapping path= "/adminuser/updatepassword.do"/>
                <bean class= " Com.rupeng.web.interceptor.PermissionInterceptor "/>
            </mvc:interceptor>
        
        </mvc:interceptors >




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.