Spring Boot Framework Learning 8-"dry" Spring Boot Web Development (4)-Custom Interceptor handling permissions

Source: Internet
Author: User

The main contents of this chapter:

With previous learning, we learned and quickly completed the first spring boot application. Spring Boot Enterprise Framework, then how does spring boot read static resources? such as JS folder, CSS files and png/jpg pictures? How do I customize a message converter? How do I customize the configuration of spring MVC? These are all we need in the company. How do we solve this? This is explained in detail in the next section. Okay, now open Spring Boot's web Development Section Fourth

This section mainly:

1: Custom Message Converters

This article is "Edith accompany you learn series-Framework learning Spring Boot Framework Learning" in the eighth Spring Boot framework Learning 8-spring Boot Web Development (4)-Custom Spring MVC configuration

Disclaimer: This article is Edith Java (www.kaigejava.com) original, without permission, no reprint!

The main contents of this section:

1: Learn to customize the configuration of Spring MVC by adding interceptors

Three ways to 2:handlerinterceptor an object

One: Learn to customize the configuration of Spring MVC by adding interceptors

1.1: Requirements Description

Sometimes, we don't need to configure spring MVC ourselves instead of using springt boot

The default approval. For example, in the background, users must log in before they can access the corresponding page. At this point, we need to add an interceptor for processing. What about this situation?

1.2: How to add an interceptor

Need to add interceptors, this time you have to extend by inheriting webmvcconfigureadapter and then overriding the methods in the parent class.

Such as:

650) this.width=650; "Src=" http://image.uc.cn//s/wemedia/s/upload/2017/ Bbfbb115ec2284ebe72e73d45cb9ef57x768x343x10.png "alt=" bbfbb115ec2284ebe72e73d45cb9ef57x768x343 "/>

650) this.width=650; "Src=" http://image.uc.cn//s/wemedia/s/upload/2017/ E8101bdc19ffc6dc5dcf736c33e9142dx795x625x17.png "alt=" e8101bdc19ffc6dc5dcf736c33e9142dx795x625 "/>

To view the results of a run:

User not logged in:

650) this.width=650; "Src=" http://image.uc.cn//s/wemedia/s/upload/2017/ 69ceb127de4a506d22a5d2993e9cd2a3x672x80x3.png "alt=" 69ceb127de4a506d22a5d2993e9cd2a3x672x80x "/>

After looking at add user user=123 in the address after execution:

650) this.width=650; "Src=" http://image.uc.cn//s/wemedia/s/upload/2017/ E70ee384e7058c2a2cd0e75ee20ff87fx718x128x8.png "alt=" e70ee384e7058c2a2cd0e75ee20ff87fx718x128 "/>

Two: Three ways to Handlerinterceptor objects

2.1:prehandle (httpservletrequest request,httpservletresponse response, Object handler)

The Prehandle method is called before the controller is processed, as you can see from the user who is logged in, but Edith the red selection. The source code is as follows:

650) this.width=650; "Src=" http://image.uc.cn//s/wemedia/s/upload/2017/ 9eebba456644ed887de3306276ba75d1x855x405x17.png "alt=" 9eebba456644ed887de3306276ba75d1x855x405 "/>

The approximate meaning is that when returning true then execute the chain behind. If False is returned, the request is terminated directly.

is personal understanding:

650) this.width=650; "Src=" http://image.uc.cn//s/wemedia/s/upload/2017/ 797b4c3f1ed85dae27020cd3a63fb8f0x628x288x13.png "alt=" 797b4c3f1ed85dae27020cd3a63fb8f0x628x288 "/>

So, if we want permission to handle:

650) this.width=650; "Src=" http://image.uc.cn//s/wemedia/s/upload/2017/ 330a6b1919f7394ae1f74e1b2ea4baabx864x497x18.png "alt=" 330a6b1919f7394ae1f74e1b2ea4baabx864x497 "/>

2.2:posthandle (HttpServletRequest request, httpservletresponse response, Object handler, Modelandview Modelandview) Method:

First look at the source:

650) this.width=650; "Src=" http://image.uc.cn//s/wemedia/s/upload/2017/ 275cbc98b1f1bfdcbaeeb33e67f26073x1013x445x19.png "alt=" 275cbc98b1f1bfdcbaeeb33e67f26073x1013x44 "/>

650) this.width=650; "Src=" http://image.uc.cn//s/wemedia/s/upload/2017/ Cb1b8843ecc378f3668c5ee4623d1344x640x350x16.png "alt=" cb1b8843ecc378f3668c5ee4623d1344x640x350 "/>

This is when the POST request is, the execution method. This is not explained in detail here.

2.3:aftercompletion (HttpServletRequest request, httpservletresponse response, Object handler, Exception ex).

650) this.width=650; "Src=" http://image.uc.cn//s/wemedia/s/upload/2017/ 2245626f43a6e92c02dfb34389c63615x859x549x19.png "alt=" 2245626f43a6e92c02dfb34389c63615x859x549 "/>

Understand:

650) this.width=650; "Src=" http://image.uc.cn//s/wemedia/s/upload/2017/ 94b067956951605d886cdec4e6115840x637x249x10.png "alt=" 94b067956951605d886cdec4e6115840x637x249 "/>

This section focuses on custom interceptors to handle some requests.

In the next section, we'll go to the JSP page to return to the 404 error code How to resolve and continue to jump after logging on the current browsing page function. You are welcome to continue your study.

Other articles in this series:

Spring Boot Framework Learn the important annotations of preschool mastery (1)-spring Java configuration method

Spring Boot Framework Learn important annotations for preschool Mastery (2)-Configure with Java configuration spring

Spring Boot Framework Learn important notes for preschool mastery (3)-read external resource profiles by annotation

Spring Boot Framework Learn important notes for preschool mastery (4)-read external resource profiles by annotation 2

Spring Boot Framework Learning 1-Understanding Spring boot and QuickStart

Spring Boot Framework Learning 2-spring boot core (1)

Spring Boot Framework Learning 3-spring boot core (2)

Spring Boot Framework Learning 5-spring Boot Web development (1)

Spring Boot Framework Learning 6-spring Boot Web Development (2)

Pring Boot Framework Learning 7-spring Boot Web Development (3)-Custom message converters

Welcome to the Gothic public: Edith Java

Welcome to visit Edith Personal website: www.kaigejava.com

The source of this article: HTTP://WWW.KAIGEJAVA.COM/ARTICLE/DETAIL/57

Spring Boot Series Tutorial Address: http://kaigejava.com/article/list?cateid=3



Spring Boot Framework Learning 8-"dry" Spring Boot Web Development (4)-Custom Interceptor handling permissions

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.