SpringSecurity自定義AuthenticationProvider無法@Autowire的解決
自定義AuthenticationProvider無法@Autowire的解決
在AuthenticationProvider中使用@Autowired注入時(shí)始終報(bào)Null問題
找了半天發(fā)現(xiàn)應(yīng)該在SecurityConfig配置類中
@EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter{
在設(shè)置AuthenticationProvider時(shí)
應(yīng)該使用@Bean的方式設(shè)置
@Bean CustomAuthenticationProvider customAuthenticationProvider() { return new CustomAuthenticationProvider(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(customAuthenticationProvider()); }
之前的錯(cuò)誤的設(shè)置方式是
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(new CustomAuthenticationProvider()); }
好了,這就可以實(shí)現(xiàn)AuthenticationProvider時(shí)自由的使用@Autowired了
自定義AuthenticationProvider的簡單例子
xml 配置
<authentication-manager> <authentication-provider ref="myAuthenticationProvider" /> </authentication-manager> <beans:bean id="userDetailsService" class="net.mantis.security.auth.NMUserDetailsService"/> <beans:bean id="myAuthenticationProvider" class="net.mantis.security.auth.MyAuthenticationProvider"> <beans:property name="userDetailsService"> <beans:bean class="net.mantis.security.auth.NMUserDetailsService"> </beans:bean> </beans:property> </beans:bean>
net.mantis.security.auth.MyAuthenticationProvider
public class MyAuthenticationProvider implements AuthenticationProvider { UserDetailsService userDetailsService; public Authentication authenticate(Authentication authentication) throws AuthenticationException { //username System.out.println("user name: "+authentication.getName()); //password System.out.println("password: "+authentication.getCredentials()); System.out.println("getPrincipal: "+authentication.getPrincipal()); System.out.println("getAuthorities: "+authentication.getAuthorities()); System.out.println("getDetails: "+authentication.getDetails()); UserDetails userDetails = (UserDetails)this.userDetailsService.loadUserByUsername(authentication.getName()); UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken( userDetails, authentication.getCredentials(),userDetails.getAuthorities()); return result; } public boolean supports(Class authentication) { return true; } public void setUserDetailsService(UserDetailsService userDetailsService){ this.userDetailsService = userDetailsService; } }
net.mantis.security.auth.NMUserDetailsService
public class NMUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException { ArrayList list = new ArrayList(); list.add(new SimpleGrantedAuthority("ROLE_SUPERVISOR")); User details = new User("rod", "koala", list); return details; } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
idea全局設(shè)置Maven配置的實(shí)現(xiàn)步驟
本文主要介紹了idea全局設(shè)置Maven配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Java內(nèi)存溢出實(shí)現(xiàn)原因及解決方案
這篇文章主要介紹了Java內(nèi)存溢出實(shí)現(xiàn)原因及解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Spring?Boot?項(xiàng)目中?JPA?語法的基本使用方法
這篇文章主要介紹了?Spring?Boot?項(xiàng)目中?JPA?語法的基本使用方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10java時(shí)間戳轉(zhuǎn)日期格式的實(shí)現(xiàn)代碼
本篇文章是對java時(shí)間戳轉(zhuǎn)日期格式的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06SpringBoot中MockMVC單元測試的實(shí)現(xiàn)
Mock是一種用于模擬和替換類的對象的方法,以便在單元測試中獨(dú)立于外部資源進(jìn)行測試,本文主要介紹了SpringBoot中MockMVC單元測試的實(shí)現(xiàn),具有應(yīng)該的參考價(jià)值,感興趣的可以了解一下2024-02-02