我正在尝试创建一个安全模块,它将针对LDAP检查用户凭据(登录时),并且在成功登录时生成JWT以进一步请求服务器 .
目前我的模块工作方式如下:我有3个其余的API endpoints 来提供身份验证(登录,验证JWT,注销),这些身份验证不受保护,因为任何人都必须能够访问这些 endpoints ,还有1个userUpdate endpoints 通过JWTAuthenticationProvider使用spring security保护
所有关于JWT的东西都准备好了,现在我只需要创建一个方法来检查LDAP是否正确用户和密码 . 但我在理解我该怎么做时遇到一些麻烦
我已经拥有主用户并传递给ldap,但我发现ldap身份验证的大部分示例都是Spring安全性的,我不认为这样做的方法是这样的,因为我需要验证匹配的us / pass仅在登录时(并不保护我的 endpoints 具有安全性) .
任何人都可以告诉我如何进行验证?我不清楚的任何东西?请询问,评论和回答 .
谢谢
哦,一个编辑:
@Override
public AuthenticationResponse login(AuthenticationRequest authenticationRequest) {
checkNotNull(authenticationRequest, "The authenticationRequest is a required argument!");
AuthenticationResponse authenticationResponse = AuthenticationResponse.builder().build();
//currently a pseudo authentication, here is where i should authenticate against LDAP
Optional<Usuario> optionalUsuario = service.findByNombreUsuario(authenticationRequest);
if (optionalUsuario.isPresent()) {
Usuario usuario = optionalUsuario.get();
String token = JwtTokenUtil.generateToken(authenticationRequest);
authenticationResponse.setAuthenticationToken(token);
repository.saveToken(UserToken.builder()
.nombreUsuario(usuario.getNombreUsuario())
.roles(usuario.getRoles())
.build(), token);
正如你所看到我打算只在登录时对ldap进行身份验证,并且只检查用户和传递是否正确,我将使用其他数据库管理角色和权限
另一个编辑:我使用spring安全性为ldap auth提供了一些基本的ldap结构,但我总是得到错误的凭据
再次编辑:我设法使它与spring安全性一起工作,但是(正如预期的那样)我的团队告诉我们需要实现没有spring安全性的身份验证,以便与我们的自定义角色加载器和令牌创建集成
1 回答
使用http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/ldap/authentication/LdapAuthenticationProvider.html进行身份验证并从LDAP获取角色,应该使用spring security来完成,我可能会错过smth但是你可以解释为什么你不想使用它,因为它是安全标准