通过REST端点的Spring安全身份验证/授权

通过REST端点的Spring安全身份验证/授权,spring,spring-mvc,authentication,spring-security,spring-social,Spring,Spring Mvc,Authentication,Spring Security,Spring Social,在使用RESTfulWebServices的Spring引导应用程序中,我已将Spring安全性与Spring社交和SpringSocialConfigure一起配置 现在我有两种身份验证/授权方式——通过用户名/密码和社交网络,比如Twitter 为了在Spring MVC REST controller中通过我自己的RESTful端点实现身份验证/授权,我添加了以下方法: @RequestMapping(value = "/login", method = RequestMethod.POS

在使用RESTfulWebServices的Spring引导应用程序中,我已将Spring安全性与Spring社交和
SpringSocialConfigure
一起配置

现在我有两种身份验证/授权方式——通过用户名/密码和社交网络,比如Twitter

为了在Spring MVC REST controller中通过我自己的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
在同一个应用程序中使用不同的令牌可以吗?

Restful调用应始终返回响应代码。在你的情况下,应该是200好。一旦发生故障,则未经授权。拥有不同的代币是绝对好的,无论如何你不能使用相同的代币


我个人更喜欢通过Spring安全过滤器而不是控制器来处理登录端点,因为您可以更好地控制流。

您可以通过覆盖
SimpleRuthenticationSuccessHandler中的方法来配置成功身份验证时返回的内容


此外,您还可以处理故障响应:



关于如何通过Spring安全过滤器处理登录端点,是否有任何文档或指南?当然有。您可以搜索SpringSecurityJava配置示例,并查找自定义身份验证入口点实现或登录过滤器。这里有一个例子:谢谢你的回答。这正是我在当前应用程序中已经使用的。似乎Spring Boot 2.x中没有提供
NoRedirectStrategy
,我可以从哪里获得它?@Leoniddasko NoRedirectStrategy不是Spring提供的。这是我的重定向策略接口的实现。您可以在上面的代码中找到相同的代码。有一个受保护的NoRedirectStrategy类实现重定向策略接口。但是,如果您不想使用它,可以使用spring提供的DefaultRedirectStrategy。
public class CustomAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    public CustomAuthenticationSuccessHandler() {
        super();
        setRedirectStrategy(new NoRedirectStrategy());
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {

        super.onAuthenticationSuccess(request, response, authentication);
        ObjectMapper mapper = new ObjectMapper();

        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().print(mapper.writeValueAsString(objectToBereturned);
        response.getWriter().flush();
    }

    protected class NoRedirectStrategy implements RedirectStrategy {

        @Override
        public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url)
                throws IOException {
            // any redirect if required. leave the implementation black if not needed
        }

    }
}
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
            AuthenticationException exception) throws IOException, ServletException {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    }
}