iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringSecurity自定义AuthenticationProvider无法@Autowire的解决
  • 256
分享到

SpringSecurity自定义AuthenticationProvider无法@Autowire的解决

2024-04-02 19:04:59 256人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

自定义AuthenticationProvider无法@Autowire的解决 在AuthenticationProvider中使用@Autowired注入时始终报Null问题 找了

自定义AuthenticationProvider无法@Autowire的解决

在AuthenticationProvider中使用@Autowired注入时始终报Null问题

找了半天发现应该在SecurityConfig配置类中


@EnableWEBSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

在设置AuthenticationProvider时

应该使用@Bean的方式设置


@Bean
    CustomAuthenticationProvider customAuthenticationProvider() {
        return new CustomAuthenticationProvider();
    }   
@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(customAuthenticationProvider());
    }

之前的错误的设置方式是


@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(new CustomAuthenticationProvider());
    }

好了,这就可以实现AuthenticationProvider时自由的使用@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;
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: SpringSecurity自定义AuthenticationProvider无法@Autowire的解决

本文链接: https://www.lsjlt.com/news/160337.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作