Spring Security认证提供程序示例详解

 更新时间:2019年05月18日 11:11:45   作者:程序猿Knight  
这篇文章主要给大家介绍了关于Spring Security认证提供程序的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Security具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

1.简介

本教程将介绍如何在Spring Security中设置身份验证提供程序,与使用简单UserDetailsService的标准方案相比,提供了额外的灵活性。

2. The Authentication Provider

Spring Security提供了多种执行身份验证的选项 - 所有这些都遵循简单的规范 - 身份验证请求由Authentication Provider处理,并且返回具有完整凭据的完全身份验证的对象。

标准和最常见的实现是DaoAuthenticationProvider - 它从一个简单的只读用户DAO检索用户详细信息 - UserDetailsService。此UserDetailsService只能访问用户名,用来检索完整的用户实体 - 在很多情况下,这就足够了。

更多常见的场景仍然需要访问完整的身份验证请求才能执行身份验证过程。例如,在针对某些外部第三方服务(例如Crowd)进行身份验证时,将需要来自身份验证请求的用户名和密码。

对于这些更高级的方案,我们需要定义自定义身份验证提供程序:

@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);
 }
}

请注意,在返回的Authentication对象上设置的授予权限是空的 - 这是因为权限当然是特定于应用程序的。

3.注册Authentication Provider

既然定义了身份验证提供程序,我们需要使用可用的命名空间支持在XML安全配置中指定它:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
 xmlns="http://www.springframework.org/schema/security"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:beans="http://www.springframework.org/schema/beans"
 xsi:schemaLocation="
 http://www.springframework.org/schema/security 
 http://www.springframework.org/schema/security/spring-security-4.0.xsd
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
 
 <http use-expressions="true">
  <intercept-url pattern="/**" access="isAuthenticated()"/>
  <http-basic/>
 </http>
 
 <authentication-manager>
  <authentication-provider
   ref="customAuthenticationProvider" />
 </authentication-manager>
 
</beans:beans>

4. Java Configuration

接下来,我们来看看相应的Java配置:

@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();
 }
}

5. 测试认证

无论是否在后端使用此自定义身份验证提供程序,从客户端请求身份验证基本相同 - 我们可以使用简单的curl命令发送经过身份验证的请求:

curl --header "Accept:application/json" -i --user user1:user1Pass 
 http://localhost:8080/spring-security-custom/api/foo/1

请注意 - 出于本示例的目的 - 我们已使用基本身份验证保护REST API。

我们从服务器返回预期的200 OK

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=B8F0EFA81B78DE968088EBB9AFD85A60; Path=/spring-security-custom/; HttpOnly
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 02 Jun 2013 17:50:40 GMT

六,总结

在本文中,我们讨论了Spring Security的自定义身份验证提供程序的示例。

可以在GitHub项目中找到本教程的完整实现 - 这是一个基于Maven的项目,因此它应该很容易导入和运行。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。

相关文章

  • Java设计模式之备忘录模式实现对象状态的保存和恢复

    Java设计模式之备忘录模式实现对象状态的保存和恢复

    本文介绍Java设计模式之备忘录模式,该模式可以实现对象状态的保存和恢复。通过详细讲解备忘录模式的原理、实现方法和应用场景,帮助读者深入理解该设计模式,并提供示例代码和技巧,便于读者实际应用
    2023-04-04
  • Spring Boot编写拦截器教程实例解析

    Spring Boot编写拦截器教程实例解析

    这篇文章主要介绍了Spring Boot编写拦截器教程实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Java中LinkedList数据结构的详细介绍

    Java中LinkedList数据结构的详细介绍

    这篇文章主要介绍了Java中LinkedList,Linked List 是 java.util 包中 Collection 框架的一部分,文中提供了详细的代码说明,需要的朋友可以参考下
    2023-05-05
  • 解读JSONArray删除元素的两种方式

    解读JSONArray删除元素的两种方式

    这篇文章主要介绍了解读JSONArray删除元素的两种方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 最通俗的白话讲解JDK源码中的ThreadLocal

    最通俗的白话讲解JDK源码中的ThreadLocal

    ThreadLocal是JDK包提供的,它提供线程本地变量,如果创建一乐ThreadLocal变量,那么访问这个变量的每个线程都会有这个变量的一个副本,在实际多线程操作的时候,操作的是自己本地内存中的变量,从而规避了线程安全问题,感兴趣的朋友快来看看吧
    2022-01-01
  • springboot+vue2+elementui实现时间段查询方法

    springboot+vue2+elementui实现时间段查询方法

    这篇文章主要介绍了springboot+vue2+elementui实现时间段查询方法,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-05-05
  • java+mysql实现登录和注册功能

    java+mysql实现登录和注册功能

    这篇文章主要为大家详细介绍了java+mysql实现登录和注册功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Struts2访问Servlet的三种方式

    Struts2访问Servlet的三种方式

    这篇文章主要介绍了Struts2访问Servlet的三种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • java连连看游戏菜单设计

    java连连看游戏菜单设计

    这篇文章主要为大家详细介绍了java连连看游戏菜单部分的设计代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • 5个主流的Java开源IDE工具详解

    5个主流的Java开源IDE工具详解

    这篇文章主要介绍了5个主流的Java开源IDE工具,无论如何,Java在当今使用的编程语言中始终排在前三名,在TIOBE索引中涉及700万到1000万的程序员和开发者
    2020-07-07

最新评论