网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringSecurity如何自定义验证方法

这篇文章主要介绍Spring Security如何自定义验证方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

在济阳等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、网站制作 网站设计制作按需定制开发,公司网站建设,企业网站建设,成都品牌网站建设,全网营销推广,成都外贸网站制作,济阳网站建设费用合理。

Spring Security的本质

Spring Security 本质上是一连串的 Filter , 然后又以一个独立的 Filter 的形式插入到 Filter Chain 里,其名为 FilterChainProxy 。 如图所示。

Spring Security如何自定义验证方法 

实际上 FilterChainProxy 下面可以有多条 Filter Chain ,来针对不同的URL做验证,而 Filter Chain 中所拥有的 Filter 则会根据定义的服务自动增减。所以无需要显示再定义这些 Filter ,除非想要实现自己的逻辑。

Spring Security如何自定义验证方法 

关键类

Authentication

Authentication 是一个接口,用来表示用户认证信息,在用户登录认证之前相关信息会封装为一个 Authentication 具体实现类的对象,在登录认证成功之后又会生成一个信息更全面,包含用户权限等信息的 Authentication 对象,然后把它保存在 SecurityContextHolder 所持有的 SecurityContext 中,供后续的程序进行调用,如访问权限的鉴定等。

AuthenticationManager

用来做验证的最主要的接口为 AuthenticationManager ,这个接口只有一个方法:

public interface AuthenticationManager {
 Authentication authenticate(Authentication authentication)
 throws AuthenticationException;
}

其中 authenticate() 方法运行后可能会有三种情况:

验证成功,返回一个带有用户信息的 Authentication 。

验证失败,抛出一个 AuthenticationException 异常。

无法判断,返回 null 。

ProviderManager

ProviderManager 是上面的 AuthenticationManager 最常见的实现,它不自己处理验证,而是将验证委托给其所配置的 AuthenticationProvider 列表,然后会依次调用每一个 AuthenticationProvider 进行认证,这个过程中只要有一个 AuthenticationProvider 验证成功,就不会再继续做更多验证,会直接以该认证结果作为 ProviderManager 的认证结果。

Spring Security如何自定义验证方法 

认证过程

用户使用用户名和密码进行登录。

Spring Security 将获取到的用户名和密码封装成一个 Authentication 接口的实现类,比如常用的 UsernamePasswordAuthenticationToken 。

将上述产生的 Authentication 对象传递给 AuthenticationManager 的实现类 ProviderManager 进行认证。

ProviderManager 依次调用各个 AuthenticationProvider 进行认证,认证成功后返回一个封装了用户权限等信息的 Authentication 对象。

将 AuthenticationManager 返回的 Authentication 对象赋予给当前的 SecurityContext 。

自定义验证

有了以上的知识储备后就可以来自定义验证方法了。通过上面可以看出,实际上真正来做验证操作的是一个个的 AuthenticationProvider ,所以如果要自定义验证方法,只需要实现一个自己的 AuthenticationProvider 然后再将其添加进 ProviderManager 里就行了。

自定义AuthenticationProvider

@Component
public class CustomAuthenticationProvider
 implements AuthenticationProvider {
 @Override
 public Authentication authenticate(Authentication authentication) 
 throws AuthenticationException {
 String name = authentication.getName();
 String password = authentication.getCredentials().toString();
 if (shouldAuthenticateAgainstThirdPartySystem()) {
  // use the credentials
  // and authenticate against the third-party system
  return new UsernamePasswordAuthenticationToken(
  name, password, new ArrayList<>());
 } else {
  return null;
 }
 }
 @Override
 public boolean supports(Class authentication) {
 return authentication.equals(
  UsernamePasswordAuthenticationToken.class);
 }
}

其中的 supports() 方法接受一个 authentication 参数,用来判断传进来的 authentication 是不是该 AuthenticationProvider 能够处理的类型。

注册AuthenticationProvider

现在再将刚创建的 AuthenticationProvider 在 与ProviderManager 里注册,所有操作就完成了。

@Configuration
@EnableWebSecurity
@ComponentScan("org.baeldung.security")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
 @Autowired
 private CustomAuthenticationProvider authProvider;
 @Override
 protected void configure(
 AuthenticationManagerBuilder auth) throws Exception {
 auth.authenticationProvider(authProvider);
 }
 @Override
 protected void configure(HttpSecurity http) throws Exception {
 http.authorizeRequests().anyRequest().authenticated()
  .and()
  .httpBasic();
 }
}

以上是“Spring Security如何自定义验证方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


分享题目:SpringSecurity如何自定义验证方法
转载来于:http://bjjierui.cn/article/jggsde.html

其他资讯