【发布时间】:2015-06-29 21:44:57
【问题描述】:
在我的带有 RESTful Web 服务的 Spring Boot 应用程序中,我已经配置了 Spring Security 以及 Spring Social 和 SpringSocialConfigurer。
现在我有两种身份验证/授权方式 - 通过用户名/密码和社交网络,例如 Twitter。
为了在我的 Spring MVC REST 控制器中通过我自己的 RESTful 端点实现身份验证/授权,我添加了以下方法:
@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 端点调用后必须返回给客户端的确切内容。我认为返回完整的身份验证对象是多余的。
认证成功后返回给客户端的内容是什么?
您能告诉我如何正确实现这种登录方法吗?
另外,如果是 RESTfull 登录,我将拥有 UsernamePasswordAuthenticationToken,如果通过 Twitter 登录,我将拥有 SocialAuthenticationToken 在同一个应用程序中可以有不同的令牌吗?
【问题讨论】:
标签: spring spring-mvc authentication spring-security spring-social