真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

SpringBoot自定義Shiro過(guò)濾器無(wú)法使用@Autowired怎么辦

這篇文章主要為大家展示了“SpringBoot自定義Shiro過(guò)濾器無(wú)法使用@Autowired怎么辦”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“SpringBoot自定義Shiro過(guò)濾器無(wú)法使用@Autowired怎么辦”這篇文章吧。

目前成都創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、興業(yè)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

在 Spring Boot 中集成 Shiro,并使用 JWT 進(jìn)行接口認(rèn)證。

為了統(tǒng)一對(duì) Token 進(jìn)行過(guò)濾,所以自定義了一個(gè) JwtTokenFilter 過(guò)濾器。

期間遇到了以下幾個(gè)問(wèn)題,這里逐一進(jìn)行記錄,以備日后查閱。

問(wèn)題一:JwtTokenFilter 無(wú)法使用 @Autowired

因?yàn)樽远x了一個(gè) JWT Token 工具類,用來(lái)解析和創(chuàng)建 Token,JwtTokenFilter 中需要用到此工具類,這里本來(lái)可以直接手動(dòng)進(jìn)行 new 一個(gè)新的實(shí)例,但由于在 Spring 配置文件中定義了 JWT 簽名密鑰和過(guò)期時(shí)間,所以想使用 Spring @ConfigurationProperties 注解進(jìn)行值得注入,所以這里必須不能手動(dòng) new 一個(gè)新的實(shí)例。

所以在 ShiroConfiguration 配置文件中將 JwtTokenFilter 過(guò)濾器交由 Spring 管理:

@Beanpublic JwtTokenFilter JwtTokenFilter() {  return new JwtTokenFilter();}

啟動(dòng)項(xiàng)目進(jìn)行測(cè)試,JwtTokenFilter 過(guò)濾器中 JwtUtil 類成功注入,但又遇到了另外一個(gè)問(wèn)題。

問(wèn)題二:anon 過(guò)濾器失效

在問(wèn)題一解決后,登錄接口一直顯示需要認(rèn)證,所以在只能將 ShiroFilterFactoryBean 中定義的 JwtTokenFilter 又改為原先手動(dòng) new:

@Bean(name = "shiroFilter")public ShiroFilterFactoryBean shiroFilterFactoryBean() {  ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();  shiroFilterFactoryBean.setSecurityManager(securityManager());  // 注冊(cè)自定義過(guò)濾器  Map filterMap = new LinkedHashMap<>(8);  // 這里只能使用 new 新建實(shí)例  filterMap.put("authc", new JwtTokenFilter());  shiroFilterFactoryBean.setFilters(filterMap);  Map filterChains = new LinkedHashMap<>(8);  filterChains.put("/v1/admin/login", "anon");  filterChains.put("/**", "authc");  shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChains);  return shiroFilterFactoryBean;}

接著創(chuàng)建一個(gè) Spring 的上下文管理工具類,代碼如下:

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 上下文工具類 * * @author nwgdk */@Componentpublic class SpringContextUtil implements ApplicationContextAware {  private static ApplicationContext applicationContext;  @Override  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {    SpringContextUtil.applicationContext = applicationContext;  }  /**   * 獲取上下文   */  public static ApplicationContext getApplicationContext() {    return applicationContext;  }  /**   * 通過(guò) bena 名稱獲取上下文中的 bean   */  public static Object getBean(String name) {    return applicationContext.getBean(name);  }  /**   * 通過(guò)類型獲取上下文中的bean   */  public static Object getBean(Class requiredType) {    return applicationContext.getBean(requiredType);  }}

接著,在 JwtTokenFilter 過(guò)濾器中通過(guò)以上工具類獲取 JwtUtil 工具類:

if (StringUtils.isNotEmpty(jwtToken)) {  if (jwtUtil == null) {    jwtUtil = (JwtUtil) SpringContextUtil.getBean("jwtUtil");  }}

啟動(dòng)項(xiàng)目進(jìn)行測(cè)試,成功登錄。

以上是“SpringBoot自定義Shiro過(guò)濾器無(wú)法使用@Autowired怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


分享文章:SpringBoot自定義Shiro過(guò)濾器無(wú)法使用@Autowired怎么辦
文章起源:http://weahome.cn/article/gcjsio.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部