多个登录端点Spring Security OAuth2 [英] Multiple Login endpoints Spring Security OAuth2

查看:38
本文介绍了多个登录端点Spring Security OAuth2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为不同的用户角色实现多个登录策略(带有Spring Boot2的Spring Security OAuth2),并且每个策略都应该使用不同的端点。我有三种用户类型REGULAR, EXTERNAL, CLIENT,分别是常规登录vía用户名/密码,外部登录通过DocentID/key登录,客户端之前做一些短信恶作剧获取当前密码,它用手机/密码登录。他们已经可以从常规网站登录,但他们将为每个角色提供移动应用程序。

我尝试使用@EnableAuthorizationServer创建多个AuthorizationServer实例,每个实例都带有配置,但它只获取最后一个实例。每个角色都有不同的UserDetailsServiceIml,并且恰好有一个在数据库中创建的应用程序。我想公开它们,以便客户端应用程序使用/client/oauth/...,常规应用程序使用/regular/oauth/...,外部应用程序使用/external/oauth/...。我如何实现这一点?

推荐答案

如果您使用的是Spring安全和OAuth2,并且希望获得许多不同的登录终结点,则可能需要自定义身份验证入口点

@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

    private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
        String clientId = request.getParameter("client_id");
        String redirectUrl = "/login";
        HttpSession session = request.getSession();
        session.setAttribute(SessionSaveAttribute.CLIENT_ID_ATR, clientId);
        //echoSessionAtr(request);
        redirectStrategy.sendRedirect(request, response, redirectUrl);
    }

}

因此,您可以通过设置条件来自定义登录终结点。

if(clientId=="REGULAR_CLIENT_ID"){
    redirectUrl = "regular/login"
} else if(clientId=="SPECIAL_CLIENT_ID"){
    redirctUrl = "...";
}

这篇关于多个登录端点Spring Security OAuth2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆