Spring boot uses interceptors, annotations for permission filtering

Source: Internet
Author: User

The permission filter here, with the annotations to use, I want to add a specific annotation method, will be verified (if not required, can be judged the logic of the annotation is removed, as usual) 1: Define Annotations:/*** Permission Check */@Documented@Target(ElementType. METHOD) @Retention(retentionpolicy. RUNTIME) public @interface rolecheck { roleenum[] Role ();}The value of the role defined in Userroleenum 2: CreateRoleinterceptor class, overriding the Pehandle method, writing the specific checksum logic in the method/*** for role verification * */@Servicepublic class Roleinterceptor extends basehandlerinterceptoradapter { @Override Publicboolean prehandle (httpservletrequest request HttpServletResponse handler" throws Exception {     handlermethod handlermethod = ( Handlermethod) handler< Span style= "FONT-SIZE:11PX; Font-family:monaco; Line-height:normal ">;          rolecheck rolecheck = handlermethod.class); //If there is no rolecheck annotation on the method, the checksum is passed if (rolecheck = = null) { return true;         }//This method does not give permission and does not pass        < Span style= "FONT-SIZE:11PX; Font-family:monaco; Line-height:normal; Color: #931a68 ">if (rolecheck.role () = = null) { return false;         }    }}3: Add annotations to the method you want to intercept@RequestMapping(value = {"/getstudent"}) @RoleCheck(role = {Roleenum. TEACHER}) public string getstudentinfo ( @RequestParam String studentno) { return studentmanager. Getstudentinfobyno (StudentNo); }2: Create a custom adapter class that inheritsWebmvcconfigureradapter class@Configurationpublic class Defineadapter extends webmvcconfigureradapter { @Autowiredpublic roleinterceptor roleinterceptor; @Override     Public void addinterceptors (interceptorregistry registry) {          registry" / Getstudent Super. Addinterceptors (registry);     }}rewrite the Addinterceptors method to add the URL you want to intercept. after the Webmvcconfigureradapter is inherited, the interceptor will be added when the project starts.

Spring boot uses interceptors, annotations for permission filtering

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.