在我的Spring Boot应用程序中,我已经配置了Spring Security以及Spring Social和SpringSocialConfigurer
来实现RESTful web服务。
目前我有两种身份验证/授权方式——通过用户名/密码和通过社交网络,例如Twitter。
为了通过我的自己的RESTful端点在Spring MVC REST控制器中实现身份验证/授权,我添加了以下方法:
@RequestMapping(value = "/login", method = RequestMethod.POST)
public Authentication login(@RequestBody LoginUserRequest userRequest) {
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(userRequest.getUsername(), userRequest.getPassword()));
boolean isAuthenticated = isAuthenticated(authentication);
if (isAuthenticated) {
SecurityContextHolder.getContext().setAuthentication(authentication);
}
return authentication;
}
private boolean isAuthenticated(Authentication authentication) {
return authentication != null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated();
}
但是我不确定每次成功调用/login
端点后应该向客户端返回什么。 我认为返回完整的身份验证对象是多余的。
在身份验证成功的情况下,应向客户端返回什么?
请告诉我如何正确实现此登录方法。
此外,在RESTful登录的情况下,我将拥有UsernamePasswordAuthenticationToken
,而在通过Twitter登录的情况下,我将拥有SocialAuthenticationToken
。在同一应用程序中使用不同的令牌是否可以接受?
NoRedirectStrategy
不可用,我应该从哪里获取它? - Leonid Dashko