Spring security Spring Security是否提供了我可以传递用户名和密码的API;密码并获取身份验证对象?

Spring security Spring Security是否提供了我可以传递用户名和密码的API;密码并获取身份验证对象?,spring-security,Spring Security,Spring Security是否提供了任何这样的API,我可以在其中传递用户名和密码,并且它将为成功的身份验证返回身份验证对象,或者为不成功的身份验证返回AuthenticationCredentialsNotFoundException 让我详细说明我的要求: 我们的应用程序有一个HTTP API(比如,/createXXX.do),客户机使用用户名、密码和其他参数来访问它 现在我想验证并授权这个访问(来自对我的应用程序的HTTP点击) 我的计划设计如下: a) 我不会限制对HTTP API

Spring Security是否提供了任何这样的API,我可以在其中传递用户名和密码,并且它将为成功的身份验证返回身份验证对象,或者为不成功的身份验证返回AuthenticationCredentialsNotFoundException

让我详细说明我的要求:

我们的应用程序有一个HTTP API(比如,/createXXX.do),客户机使用用户名、密码和其他参数来访问它

现在我想验证并授权这个访问(来自对我的应用程序的HTTP点击)

我的计划设计如下: a) 我不会限制对HTTP API上下文的访问(即/createXXX.do)

b) 一旦请求到达我的doGet()/doPost(),我将从请求中检索用户名和密码,并希望使用一些spring安全API,如下所示:

验证验证XXXX(字符串用户名、字符串密码) 抛出AuthenticationCredentialsNotFoundException

c) 因此,上述API在内部将这些用户名/密码推送到现有的spring安全链,并返回成功身份验证的身份验证对象或不成功身份验证的AuthenticationCredentialsNotFoundException

d) 对于不成功的身份验证,我将捕获AuthenticationCredentialsNotFoundException并返回带有身份验证错误代码的HttpServletResponse

e) 为了成功地进行身份验证,基于来自身份验证对象的身份验证,我将允许或返回带有AUTHORIZATION_错误代码的HttpServletResponse

有人知道这样的spring安全API吗? 如有任何建议,我们将不胜感激


谢谢。

如果您只有一个身份验证源(只有LDAP或DB),您可以在安全上下文中配置org.springframework.security.authentication.AuthenticationProvider的一些实现。然后您可以使用它:

User user = new User(login, password, true, true, true, true, new ArrayList<GrantedAuthority>());
Authentication auth = new UsernamePasswordAuthenticationToken(user, password,new ArrayList<GrantedAuthority>());
try {
    auth = authenticationProvider.authenticate(auth);
} catch (BadCredentialsException e) {
    throw new CustomBadCredentialsException(e.getMessage(), e);
}

// but your need to push authorization object manually
SecurityContext sc = new SecurityContextImpl();
sc.setAuthentication(auth);
SecurityContextHolder.setContext(sc);
User User User=新用户(登录名、密码、true、true、true、true、new ArrayList());
Authentication auth=新用户名PasswordAuthenticationToken(用户、密码、新ArrayList());
试一试{
auth=authenticationProvider.authenticate(auth);
}捕获(BadCredentialsException e){
抛出新的CustomBadCredentialsException(e.getMessage(),e);
}
//但您需要手动推送授权对象
SecurityContext sc=新的SecurityContextImpl();
sc.setAuthentication(auth);
SecurityContextHolder.setContext(sc);
这是“低级”操纵。您可以使用Spring安全命名空间中的元素。它可以为您提供登录控制器,甚至是登录表单(并且它可以自动处理这种情况)