spring-boot - 如何在 Spring Boot Security 的另一个微服务中调用我的身份验证微服务
问题描述
我已经使用 Spring Security 实现了一个身份验证服务,它可以访问存储用户数据的数据库。现在我想实现另一个服务(打开一个全新的项目),我只指定我的身份验证服务的 url。我.loginProcessingUrl()
在 spring 安全配置中使用并更改了登录页面,但这不起作用。如何使用我的身份验证服务对新服务进行身份验证?
解决方案
您可以使用自定义AuthenticationProvider
为您进行身份验证。
这是一个准系统示例:
- 创建一个
CustomRemoteAuthenticationProvider
调用您的身份验证服务:
public class CustomRemoteAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
String username = authentication.getName();
String password = authentication.getCredentials().toString();
// call your authentication service
// ... and return a UsernamePasswordAuthenticationToken
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
- 将您的 bean 声明
CustomRemoteAuthenticationProvider
为 bean(您可以在 中执行此操作WebSecurityConfigurerAdapter
),它将被自动拾取并添加到AuthenticationManager
:
@Bean
public CustomRemoteAuthenticationProvider customRemoteAuthenticationProvider() {
return new CustomRemoteAuthenticationProvider();
}
注意:您可以通过@Component
直接添加到CustomRemoteAuthenticationProvider
. 此外,如果您想了解更多关于如何编写的想法,请查看 javadoc以AuthenticationProvider
获取 s 列表。通常用于对数据库进行身份验证。AuthenticationProvider
DaoAuthenticationProvider
JdbcDaoImpl
推荐阅读
- html - How to parse addresses from website specifying class in R?
- excel - 如何在excel中每21天增加一次日期
- webpack - 重复使用拆分包
- visual-studio-code - VS Code Insiders - 调试 Notebook Cell
- jquery - 通过下拉列表的'indexof'值获取对象键
- laravel-livewire - 通过 livewire 以自定义数据属性的值作为参数调用函数
- c# - C# XPath / 格式化变量类型 XmlNodeList 的小数位
- numpy - 复杂的间接切片索引:怎么做?
- session - Kong会话插件避免轮询请求更新
- object-detection - 用于对象检测的 ML.NET 预测速度改进