Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use filter to set the URL to be excluded in springboot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people have no idea about how to use filter to set up the URL to be excluded in springboot. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Use filter to set URL@WebFilter (urlPatterns = "/ *") @ Order (value = 1) public class TestFilter implements Filter {private static final Set ALLOWED_PATHS = Collections.unmodifiableSet (Arrays.asList ("/ main/excludefilter", "/ login", "/ logout", "/ register"); @ Override public void init (FilterConfig filterConfig) throws ServletException {System.out.println ("init-filter") } @ Override public void doFilter (ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String path = request.getRequestURI () .substring (request.getContextPath () .length ()) .replaceAll ("[/] + $", "); boolean allowedPath = ALLOWED_PATHS.contains (path) If (allowedPath) {System.out.println ("here is the url entry method that does not need to be processed"); chain.doFilter (req, res);} else {System.out.println ("here is the url entry method that needs to be processed") @ Override public void destroy () {System.out.println ("destroy-filter");}}

The smaller the value in @ Order, the higher the priority.

ALLOWED_PATHS

This is a collection that stores the URL that needs to be discharged to determine whether it is the URL that needs to be excluded.

As to why @ WebFilter is used in SpringBoot but the filter doesn't work: be sure to add the @ Configuration annotation, @ Service actually works, and so on.

Filter specifies common problems with filtering URL

When using Filter to filter and intercept some self-specified URL

The following errors often occur

1. It is obvious that the / app/online path is filtered in @ WebFilter (urlPatterns= {"/ app/online"}), but after running, it is found that this WebFilter filter filters all URL.

2. After running, it is found that the filter is not initialized and not loaded.

The following is a summary of using the correct

Appropriate annotations to configure filter:

1. Specify the path

Add a comment @ WebFilter (urlPatterns= {"/ app/online"}) to class

Then add the annotation @ ServletComponentScan to the startup class (* * Application.java)

That's it.

The code is as follows:

2. Filter all paths

Add @ Component or @ Configuration to class

If @ Component or @ Configuration is added, and @ WebFilter () is added, the Filter will be initialized twice, and all paths + self-specified paths will be filtered, and unspecified URL will also be filtered.

/ / filter all paths @ Componentpublic class WebFilter implements Filter () {/ / override three methods. @ Override public void init (FilterConfig filterConfig) throws ServletException {System.out.println ("initialize filter");}} after reading the above, have you learned how to use filter to set the URL to be excluded in springboot? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report