Spring Security认证提供程序示例详解
作者:程序猿Knight 发布时间:2022-11-07 18:06:40
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的项目,因此它应该很容易导入和运行。
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。
来源:https://www.cnblogs.com/xjknight/p/10884857.html
猜你喜欢
- 目录简介:根据sun的官方文档描述:示例:@SuppressWarnings注解的作用一.@SuppressWarings注解示例1——抑制
- 一:JDBCTemplate简介Spring为各种持久化技术提供了简单操作的模板和回调API:ORM持久化技术模板类原生JDBCorg.sp
- 初步探索首先我们要了解equals方法是什么,hashcode方法是什么。equals方法equals 是java的obejct类的一个方法
- 1. 基本概念的理解`绝对路径`:你应用上的文件或目录在硬盘上真正的路径,如:URL、物理路径例如:c:/xyz/test.txt代表了te
- 前言我们知道在 Java 中读取一些配置文件信息,是在开发中十分常用的要求。例如:这里我们使用 JDBC 实例:连接My
- 前言 因为自己在做的一个小软件里面需要用到从A-Z排序的ListView,所以自然而然的想到了微信的联系人,我想要的就是那样的效果。本来没
- application.properties有以下这几条数据方法一:@Value注解+@Component建议properties少的时候用
- 一、背景介绍在微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端。我们可以使用JDK原生的URLCo
- JAVA中实现pdf转图片可以通过第三方提供的架包,这里介绍几种常用的,可以根据自身需求选择使用。一、icepdf。有收费版和开源版,几种方
- synchronized关键字,一般称之为”同步锁“,用它来修饰需要同步的方法和需要同步代码块,默认是当前对象作为锁的对象。同步锁锁的是同一
- springboot sqlSessionFactoryBean自定义1.新建一个配置类,加上configuration注解2.定制化Sql
- 大家好,我是老三,Spring是我们最常用的开源框架,经过多年发展,Spring已经发展成枝繁叶茂的大树,让我们难以窥其全貌。这节,我们回归
- 概念尽量使用合成/聚合,而不是使用继承实现复用。所谓的合成/聚合是指一个对象里持有另外一个类的对象,通过调用这些对象的方法得到复用已有功能的
- 重新认识 Java 的 System.in以前也写过不少命令行的程序,处理文件时总需要通过参数指定路径,直到今天看资料时发现了一种我自己从来
- @RequestMapping和@GetMapping @PostMapping的区别最近学习看一些代码,发现对于发送请求这件事,有的地方用
- 1 redis主从复制的概念多机环境下,一个redis服务接收写命令,当自身数据与状态发生变化,将其复制到一个或多个redis。这种模式称为
- 简介由于最近的项目需求,需要在把配置类导入到容器中,通过查询,使用@Import注解就能实现这个功能,@Import注解能够帮我们吧普通配置
- 一.Mybatis-Plus——sum聚合函数//总收益 Order order =new Orde
- 公司的svn的地址改变了,怎么办呢。自己本地的正在修改的项目怎么办呢?修改一下svn的服务器地址咯。1.就是先关闭ide,重新打开,然后选择
- 1.前言任何系统,我们不会傻傻的在每一个地方进行异常捕获和处理,整个系统一般我们会在一个的地方统一进行异常处理,spring boot全局异