场景与实现逻辑
- 我的登录接口,在输入账号密码成功后进行中心页
- 中心页可以通过添加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 target="_blank" href="https://www.cnblogs.com/javascript:void(0)" onclick="submit(0)">默认登录后的跳换</a>
- <a target="_blank" href="https://www.cnblogs.com/javascript:void(0)" onclick="submit(1)">登录后去个人中心</a>
- </form>
复制代码 自定义登录逻辑的KC配置如下
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |