AuthenticationManager、AuthenticationProvider、UserDetailsService的关系与区别

一、AuthenticationManager里面保存有很多实例,其中一个就是AuthenticationProvider;而每个AuthenticationProvider里面都有一个UserDetailsService。

二、默认情况下,实现AuthenticationManager,就是实现AuthenticationProvider。

三、身份验证是在AuthenticationProvider中执行。

        1、AuthenticationProvider使用UserDetailsService的UserDetails负责检索和验证用户名;

        2、AuthenticationProvider使用PasswordEncoder检查密码用户是否提供了与您从数据库中检索的哈希密码。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要配置AuthenticationManager和相应的AuthenticationProvider,需要按照以下步骤进行操作: 1.创建一个实现了AuthenticationProvider接口的类,比如MyAuthenticationProvider。在这个类中,需要实现authenticate方法,用于验证用户的身份信息。这个方法中,可以使用UserDetailsService来获取用户的账号、密码等信息,然后进行验证。 2.在Spring Security的配置类中,通过重载configure(AuthenticationManagerBuilder auth)方法来配置AuthenticationManager。在这个方法中,可以使用auth.authenticationProvider()方法将MyAuthenticationProvider添加到AuthenticationManager中。 3.在Spring Security的配置类中,还需要重载configure(HttpSecurity http)方法,用于配置请求的安全性。在这个方法中,可以使用http.authorizeRequests()方法来定义哪些请求需要进行身份验证,哪些请求可以匿名访问。同时,还可以使用http.formLogin()方法来配置登录表单的相关信息,比如登录页的URL、登录表单的提交URL等。 4.在Spring Security的配置类中,还需要重载configure(WebSecurity web)方法,用于配置Spring Security的Filter链。在这个方法中,可以使用web.ignoring()方法来忽略一些请求,比如静态资源、登录页面等。 下面是一个示例配置类的代码: @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private MyUserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(myAuthenticationProvider()); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/css/**", "/js/**", "/images/**").permitAll() .antMatchers("/login").anonymous() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .loginProcessingUrl("/login") .defaultSuccessUrl("/home") .failureUrl("/login?error=true") .permitAll() .and() .logout() .logoutUrl("/logout") .logoutSuccessUrl("/login?logout=true") .invalidateHttpSession(true) .deleteCookies("JSESSIONID") .permitAll(); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring() .antMatchers("/css/**", "/js/**", "/images/**"); } @Bean public MyAuthenticationProvider myAuthenticationProvider() { MyAuthenticationProvider authenticationProvider = new MyAuthenticationProvider(); authenticationProvider.setUserDetailsService(userDetailsService); return authenticationProvider; } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } 在上面的配置类中,使用了MyUserDetailsService类来获取用户的账号、密码等信息,使用BCryptPasswordEncoder类来加密密码。同时,还定义了一些请求的安全性,比如静态资源可以匿名访问,登录页面可以匿名访问,其他请求需要进行身份验证。登录表单的相关信息也进行了配置。最后,还定义了一个MyAuthenticationProvider的Bean,用于将其添加到AuthenticationManager中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值