当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Security认证提供程序示例详解

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

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的项目,因此它应该很容易导入和运行。

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网