场景与实现逻辑

  • 我的登录接口,在输入账号密码成功后进行中间页
  • 中间页可以通过添加Authenticator的实现类来写逻辑
  • authenticate方法是渲染页面的,action方法是提交表单后的逻辑
  • context.success()方法表示认证成功,将进行重写向操作
  • 可以通过Response.status(302).header(HttpHeaders.LOCATION, modifyPasswordPage).build()实现自定义的重定向地址
  • 在kc配置中,复制一个brower认证流,为账号密码模块添加一个行为(execution)

核心代码

  @Override
public void authenticate(AuthenticationFlowContext context) { if (context.getAuthenticationSession().getUserSessionNotes().containsKey("password")) {
String password = context.getAuthenticationSession().getUserSessionNotes().get("password").toLowerCase();
if (password.matches(regex)) {
context.success();
return;
}
} Response challenge = context.form().createForm("login-simple-password-alert.ftl");
context.challenge(challenge);
} @Override
public void action(AuthenticationFlowContext context) {
MultivaluedMap<String, String> formData = context.getHttpRequest().getDecodedFormParameters();
if (formData.containsKey("submitType") && formData.get("submitType").get(0).equals("1")) {
AuthenticatorConfigModel authenticatorConfigModel =
KeycloakUtil.getAuthenticatorConfigByAlias(context.getSession(), V6_CONFIG);
String mainSite = "https://www.abc.com";
if (authenticatorConfigModel.getConfig().containsKey(MAIN_SITE) &&
StringUtils.isNotBlank(authenticatorConfigModel.getConfig().get(MAIN_SITE))) {
mainSite = authenticatorConfigModel.getConfig().get(MAIN_SITE);
}
if (mainSite.endsWith("/")) {
mainSite = mainSite.substring(0, mainSite.length() - 1);
}
context.success();
String modifyPasswordPage = mainSite + "/usercenter/info";
Response response = Response.status(302)
.header(HttpHeaders.LOCATION, modifyPasswordPage).build();
context.challenge(response);
return;
}
context.success();
}

登录中间页面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如下:
<form id="kc-form-login" action="${url.loginAction}" method="post">
<input type="hidden" id="submitType" name="submitType" value="0">
<a class="btn-register" href="javascript:void(0)" onclick="submit(0)">默认登录后的跳换</a>
<a class="btn-register" href="javascript:void(0)" onclick="submit(1)">登录后去个人中心</a>
</form> <script>
function submit(val) {
document.getElementById("submitType").value=val;
document.getElementById('kc-form-login').submit();
}
</script>

自定义登录逻辑的KC配置如下

keycloak~在认证的action中自定义重定向地址的更多相关文章

  1. struts 中自定义action访问方法

    struts中action类继承了ActionSupport  默认实现了execute()方法 struts.xml配置文件中 然后可以配置如下映射: <package name =" ...

  2. 24SpringMvc中的重定向和转发-解释return "redirect:/cargo/contractproduct/tocreate.action";

    我在做JK项目时发现:我们在增加一个货物时.会先根据提交的Action()进入到一个新增页面 //跳转到新增的页面 @RequestMapping("/cargo/contractprodu ...

  3. Asp.net Core 系列之--5.认证、授权与自定义权限的实现

    ChuanGoing 2019-11-24 asp.net core系列已经来到了第五篇,通过之前的基础介绍,我们了解了事件订阅/发布的eventbus整个流程,初探dapper ORM实现,并且简单 ...

  4. Action中动态方法的调用 Action中通配符的使用 Result的配置

       Action中动态方法的调用 动态方法调用(Dynamic Method Invocation,DMI) 标识符:! 一.通过以下选中的文件来查看是否禁止调用动态方法

  5. C#开发微信门户及应用(14)-在微信菜单中采用重定向获取用户数据

    我曾经在系列文章中的<C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍>中介绍了微信菜单里面的重定向操作,通过这个重定向操作,我们可以获取一个code值,然后获取用户的open ...

  6. 第三章Struts2 Action中动态方法调用、通配符的使用

    01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...

  7. 在Action 中访问web资源

    1.什么是web资源: HttpServletRequest,HttpSession,ServletContext等原生的Servlet API. 2.为什么要访问web资源? B/S应用的Contr ...

  8. struts2配置文件中Action中的各属性的含义

    StrutsApacheBeanJSPServlet attribute:      这个属性用来指定ActionForm保存到指定上下文时所使用的属性名.如果不指定attribute属性的值,将使用 ...

  9. Struts2 Action中动态方法调用、通配符的使用

    一.Struts2执行过程图: 二.struts2配置文件的加载顺序 struts-default.xml---struts-plugin.xml---struts.xml 具体步骤: 三.Actio ...

  10. struts2中怎么把action中的值传递到jsp页面

    对于如何把struts2的action中的值传到jsp页面中,主要的方法有2种: 使用转发视图利用request域中储存所需的值 使用重定向时存储数据进入session使其在jsp中可以获得 下面,让 ...

随机推荐

  1. win32 - 内存映射(CreateFileMapping)

    目标:创建一个app,使用CreateToolhelp32Snapshot扫描所有的进程,并将进程的pid和exe名字映射到内存中,再在另一个app中使用OpenFileMapping打开该映射读取相 ...

  2. redis7源码分析:redis 启动流程

    1. redis 由 server.c 的main函数启动 int main(int argc, char **argv) { ... // 上面的部分为读取配置和启动命令参数解析,看到这一行下面为启 ...

  3. sort.interface接口

    一个内置的排序算法需要知道三个东西:序列的长度,表示两个元素比较的结果,一种交换两个元素的方式:这就是sort.Interface的三个方法: package sort type Interface ...

  4. ubuntu18.04下nginx配合fastdfs使用的安装和配置

    前期准备 1.安装依赖包 # 新装的ubuntu缺少gcc编译,需要先安装这个 sudo apt-get install build-essential 1.解压缩 libfastcommon-mas ...

  5. 从零开始写 Docker(四)---使用 pivotRoot 切换 rootfs 实现文件系统隔离

    change-rootfs-by-pivot-root.png 本文为从零开始写 Docker 系列第四篇,在mydocker run 基础上使用 pivotRoot 系统调用切换 rootfs 实现 ...

  6. Java 常用类 String类与其他结构之间的转换-----String 与 byte[]之间的转换

    1 /** 2 * 3 * String 与 byte[]之间的转换 4 * 编码:String ---> byte[]:调用String的getBytes() 5 * 解码:byte[]--- ...

  7. C++ 错误 具有类型“const sort”的表达式会丢失一些 const-volatile 限定符以调用“bool sort::operator ()(int,int)” 如下:环境 vs2019 内容:set内置函数排序

    C++ 错误 具有类型"const sort"的表达式会丢失一些 const-volatile 限定符以调用"bool sort::operator ()(int,int ...

  8. koa 文件下载 pdf预览 两个接口 - nodejs - chromeDownload chromePreview

    koa 文件下载 pdf预览 两个接口 - nodejs - chromeDownload chromePreview chrome.js const router = require("k ...

  9. python处理txt文件常用方法总结

    一 打开txt的正确方式 一般人会用到怎么快速打开txt,下面分享两种方式: f = open("data.txt","r") #设置文件对象 f.close( ...

  10. Android WebView获取html源码

    通过执行js语句来获取 val code = """ document.documentElement.outerHTML """.trim ...