In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "SpringBoot custom Shiro filter can not use @ Autowired how to do", the content is easy to understand, clear, hope to help you solve the doubt, the following let the editor lead you to study and learn "SpringBoot custom Shiro filter can not use @ Autowired" this article.
Integrate Shiro in Spring Boot and use JWT for interface authentication.
In order to filter Token uniformly, a JwtTokenFilter filter is customized.
During this period, I encountered the following problems, which are recorded here one by one for future reference.
Problem 1: JwtTokenFilter cannot use @ Autowired
Because you have defined a JWT Token utility class, which is used to parse and create Token,JwtTokenFilter, you can manually new a new instance here, but because the JWT signature key and expiration time are defined in the Spring configuration file, you want to use the Spring @ ConfigurationProperties annotation for worthy injection, so you must not manually new a new instance.
So leave the JwtTokenFilter filter to Spring to manage in the ShiroConfiguration configuration file:
@ Beanpublic JwtTokenFilter JwtTokenFilter () {return new JwtTokenFilter ();}
Start the project for testing, and the JwtUtil class is successfully injected into the JwtTokenFilter filter, but another problem is encountered.
Problem 2: anon filter failure
After the problem is solved, the login interface always shows that authentication is required, so you can only change the JwtTokenFilter defined in ShiroFilterFactoryBean to the original manual new:
@ Bean (name = "shiroFilter") public ShiroFilterFactoryBean shiroFilterFactoryBean () {ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean (); shiroFilterFactoryBean.setSecurityManager (securityManager ()); / / Register custom filter Map filterMap = new LinkedHashMap (8); / / New instance filterMap.put ("authc", new JwtTokenFilter ()) can only be created using new; shiroFilterFactoryBean.setFilters (filterMap); Map filterChains = new LinkedHashMap (8); filterChains.put ("/ v1/admin/login", "anon") FilterChains.put ("/ * *", "authc"); shiroFilterFactoryBean.setFilterChainDefinitionMap (filterChains); return shiroFilterFactoryBean;}
Then create a context management tool class for Spring, with the following code:
Package com.nwgdk.ums.common.util;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;/** * Spring context utility class * * @ author nwgdk * / @ Componentpublic class SpringContextUtil implements ApplicationContextAware {private static ApplicationContext applicationContext; @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {SpringContextUtil.applicationContext = applicationContext;} / * get context * / public static ApplicationContext getApplicationContext () {return applicationContext } / * get bean * / public static Object getBean (String name) {return applicationContext.getBean (name) in context by bena name;} / * get bean * / public static Object getBean (Class requiredType) {return applicationContext.getBean (requiredType);}} in context by type
Next, get the JwtUtil utility class in the JwtTokenFilter filter through the above utility class:
If (StringUtils.isNotEmpty (jwtToken)) {if (jwtUtil = = null) {jwtUtil = (JwtUtil) SpringContextUtil.getBean ("jwtUtil");}}
Start the project for testing and log in successfully.
The above is all the content of the article "what to do if the SpringBoot custom Shiro filter cannot be used with @ Autowired". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.