Spring boot中filter類不能注入@Autowired變數
阿新 • • 發佈:2018-11-07
問題描述
專案中的登入是用了shiro以及filter攔截器。輸入正確的賬號密碼之後卻不能正常登入。除錯程式碼發現是filter類中定義的變數userService沒有值,為null,該變數採用的方式是@Autowired自動注入。由此我認為shiro配置類還是有點問題。
解決辦法
在shiroConfig.java(shiro相關bean的定義類)中加入一個bean用來自動注入filter中的變數
@Bean
public FilterRegistrationBean myLoginRegistrationBean() {
FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
filterRegistration.setFilter(new DelegatingFilterProxy("shiroFilter"));
// 該值預設為false,表示生命週期由SpringApplicationContext管理,設定為true則表示由ServletContainer管理
filterRegistration.addInitParameter("targetFilterLifecycle", "true");
filterRegistration.setAsyncSupported(true );
filterRegistration.setEnabled(true);
filterRegistration.setDispatcherTypes(DispatcherType.REQUEST);
// filterRegistration.addUrlPatterns("/*");// 可以自己靈活的定義很多,避免一些根本不需要被Shiro處理的請求被包含進來
// filterRegistration.setName("MyLoginFilter");
// filterRegistration.setOrder(1);
return filterRegistration;
}
這裡面需要注意的是:
filterRegistration.setFilter(new DelegatingFilterProxy("shiroFilter"));
這行程式碼,裡面的shiroFilter表示另一個bean。如果你只有一個filter類,可以直接將shiroFilter替換為你的filter物件,如:
filterRegistration.setFilter(new MyUserFilter());
更正:
上面那行單獨給filter注入值的寫法不對!,應該是先給這個filter定義一個bean:
@Bean(name = "myUserFilter")
public MyUserFilter myUserFilterBean() {
MyUserFilter bean=new MyUserFilter();
return new MyUserFilter();
}
然後:
filterRegistration.setFilter(myUserFilterBean());
因為博主專案中有三個filter類,所以不能用第二種new 物件定義,即時寫了三個這種bean,還是隻有一個filter中的變數能夠被注入!所以只能用new DelegatingFilterProxy(“shiroFilter”)方式,因為shiroFilter中已經有了三個filter的定義。其餘的相關bean如下:
@Bean(name = "formAuthenticationFilter")
public MyLoginFilter myLoginFilterBean() {
MyLoginFilter bean=new MyLoginFilter();
bean.setUsernameParam("username");
bean.setPasswordParam("password");
bean.setRememberMeParam("rememberMe");
bean.setLoginUrl("/admin/login");
bean.setSuccessUrl("/admin/index.view");
return bean;
}
@Bean(name = "sysUserFilter")
public SysUserFilter sysUserFilterBean() {
// SysUserFilter bean=new SysUserFilter();
return new SysUserFilter();
}
@Bean(name = "myUserFilter")
public MyUserFilter myUserFilterBean() {
// MyUserFilter bean=new MyUserFilter();
return new MyUserFilter();
}
@SuppressWarnings("null")
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) {
logger.info("注入Shiro的Web過濾器-->shiroFilter", ShiroFilterFactoryBean.class);
ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
bean.setSecurityManager(securityManager);
bean.setLoginUrl("/admin/login");
Map<String, Filter> filters = bean.getFilters();//獲取filters
filters.put("authc", myLoginFilterBean()); //filters中放入三個filter
filters.put("sysUser", sysUserFilterBean());
filters.put("user", myUserFilterBean());
bean.setFilters(filters);
LinkedHashMap<String, String> filterChainDefinitionMap=new LinkedHashMap<>();
filterChainDefinitionMap.put("/admin/login", "authc");
filterChainDefinitionMap.put("/admin/api/**", "sysUser");
filterChainDefinitionMap.put("/admin/**", "user");
bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return bean;
}