keycloak~在认证的action中自定义重定向地址

打印 上一主题 下一主题

主题 918|帖子 918|积分 2754

场景与实现逻辑


  • 我的登录接口,在输入账号密码成功后进行中心页
  • 中心页可以通过添加Authenticator的实现类来写逻辑
  • authenticate方法是渲染页面的,action方法是提交表单后的逻辑
  • context.success()方法表现认证成功,将进行重写向操作
  • 可以通过Response.status(302).header(HttpHeaders.LOCATION, modifyPasswordPage).build()实现自定义的重定向地址
  • 在kc配置中,复制一个brower认证流,为账号密码模块添加一个行为(execution)
核心代码
  1.   @Override
  2.   public void authenticate(AuthenticationFlowContext context) {
  3.     if (context.getAuthenticationSession().getUserSessionNotes().containsKey("password")) {
  4.       String password = context.getAuthenticationSession().getUserSessionNotes().get("password").toLowerCase();
  5.       if (password.matches(regex)) {
  6.         context.success();
  7.         return;
  8.       }
  9.     }
  10.     Response challenge = context.form().createForm("login-simple-password-alert.ftl");
  11.     context.challenge(challenge);
  12.   }
  13.   @Override
  14.   public void action(AuthenticationFlowContext context) {
  15.     MultivaluedMap<String, String> formData = context.getHttpRequest().getDecodedFormParameters();
  16.     if (formData.containsKey("submitType") && formData.get("submitType").get(0).equals("1")) {
  17.       AuthenticatorConfigModel authenticatorConfigModel =
  18.           KeycloakUtil.getAuthenticatorConfigByAlias(context.getSession(), V6_CONFIG);
  19.       String mainSite = "https://www.abc.com";
  20.       if (authenticatorConfigModel.getConfig().containsKey(MAIN_SITE) &&
  21.           StringUtils.isNotBlank(authenticatorConfigModel.getConfig().get(MAIN_SITE))) {
  22.         mainSite = authenticatorConfigModel.getConfig().get(MAIN_SITE);
  23.       }
  24.       if (mainSite.endsWith("/")) {
  25.         mainSite = mainSite.substring(0, mainSite.length() - 1);
  26.       }
  27.       context.success();
  28.       String modifyPasswordPage = mainSite + "/usercenter/info";
  29.       Response response = Response.status(302)
  30.           .header(HttpHeaders.LOCATION, modifyPasswordPage).build();
  31.       context.challenge(response);
  32.       return;
  33.     }
  34.     context.success();
  35.   }
复制代码
登录中心页面login-simple-password-alert.ftl


  • src\main\resources\theme\custom\login\
  • 这个目次下有皮肤文件login.ftl和中心页文件login-simple-password-alert.ftl
  • kc后台为指定客户端或者领域设置登录皮肤为custom
  • login-simple-password-alert.ftl如下:
  1. <form id="kc-form-login" action="${url.loginAction}" method="post">
  2.   <input type="hidden" id="submitType" name="submitType" value="0">
  3.   <a  target="_blank" href="https://www.cnblogs.com/javascript:void(0)" onclick="submit(0)">默认登录后的跳换</a>
  4.   <a  target="_blank" href="https://www.cnblogs.com/javascript:void(0)" onclick="submit(1)">登录后去个人中心</a>
  5. </form>
复制代码
自定义登录逻辑的KC配置如下



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81429

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表