KeyCloak的getProvider()方法返回null。

huangapple go评论80阅读模式
英文:

KeyCloak getProvider() return null

问题

我的KeycloakSmsAuthenticatorUtil类有一个getMessage(AuthenticationFlowContext context, String key)方法,该方法使用themeProvider获取主题,但是keycloakSession.getProvider()方法总是返回null。

public class KeycloakSmsAuthenticatorUtil {

    private static Logger logger = Logger.getLogger(KeycloakSmsAuthenticatorUtil.class);

    ...
    
    public static String getMessage(AuthenticationFlowContext context, String key) {
        String result = null;

        try {
            ThemeProvider themeProvider = context.getSession().getProvider(ThemeProvider.class, "extending");
        
            if (themeProvider == null) {
                logger.warn("THEME PROVIDER IS NULL");
            }
         
            Theme currentTheme = themeProvider.getTheme(context.getRealm().getLoginTheme(), Theme.Type.LOGIN);
            Locale locale = context.getSession().getContext().resolveLocale(context.getUser());
            result = currentTheme.getMessages(locale).getProperty(key);
        } catch (IOException e) {
            logger.warn(key + "not found in messages");
        }

        return result;
    }
}

Themeprovider为null,因此当接收到currentTheme后,程序会在下一行崩溃。

.[0m.[33m16:43:40,550 警告 [six.six.keycloak.authenticator.KeycloakSmsAuthenticatorUtil] (default task-2) THEME PROVIDER IS NULL
.[0m.[33m16:43:40,551 警告 [org.keycloak.services] (default task-2) KC-SERVICES0013: 认证失败: java.lang.NullPointerException
        在 deployment.keycloak-sms-authenticator-sns-test.jar//six.six.keycloak.authenticator.KeycloakSmsAuthenticatorUtil.getMessage(KeycloakSmsAuthenticatorUtil.java:173) 处发生

如何获得非空的themeProvider?
错误出现在ThemeProvider中,而不是上下文或keycloakSession中。

英文:

my KeycloakSmsAuthenticatorUtil class has a getMessage(AuthenticationFlowContext context, String key) method that uses the themeProvider to get the theme, but the keycloakSession.getProvider() always returns null.

public class KeycloakSmsAuthenticatorUtil {

private static Logger logger = Logger.getLogger(KeycloakSmsAuthenticatorUtil.class);

...

public static String getMessage(AuthenticationFlowContext context, String key) {
    String result = null;

    try {
        ThemeProvider themeProvider = context.getSession().getProvider(ThemeProvider.class, "extending");
    
    if (themeProvider == null) {
            logger.warn("THEME PROVIDER IS NULL");
        }
     
        Theme currentTheme = themeProvider.getTheme(context.getRealm().getLoginTheme(), Theme.Type.LOGIN);
        Locale locale = context.getSession().getContext().resolveLocale(context.getUser());
        result = currentTheme.getMessages(locale).getProperty(key);
    } catch (IOException e) {
        logger.warn(key + "not found in messages");
    }

    return result;
}

Themeprovider is null, so the program crashes on the next line when the currentTheme is received.

.[0m.[33m16:43:40,550 WARN  [six.six.keycloak.authenticator.KeycloakSmsAuthenticatorUtil] (default task-2) THEME PROVIDER IS NULL
.[0m.[33m16:43:40,551 WARN  [org.keycloak.services] (default task-2) KC-SERVICES0013: Failed authentication: java.lang.NullPointerException
        at deployment.keycloak-sms-authenticator-sns-test.jar//six.six.keycloak.authenticator.KeycloakSmsAuthenticatorUtil.getMessage(KeycloakSmsAuthenticatorUtil.java:173)

how can i get a non nullable themeProvider?
the error is in the ThemeProvider, not in the context or keycloakSession.

答案1

得分: 0

我使用了ThemeManager替换了ThemeProvider,这帮助了。

public static String getMessage(AuthenticationFlowContext context, String key) {
        String result = null;
        try {
            KeycloakSession keycloakSession = context.getSession();

            RealmModel realmModel = context.getRealm();

            String loginTheme = realmModel.getLoginTheme();

            ThemeManager themeManager = keycloakSession.theme();

            Theme currentTheme = themeManager.getTheme(loginTheme, Theme.Type.LOGIN);

            Locale locale = 
 context.getSession().getContext().resolveLocale(context.getUser());
            result = currentTheme.getMessages(locale).getProperty(key);
          
        } catch (IOException e) {
            logger.warn(key + "在消息中未找到");
        }

        return result;
    }
英文:

I replaced ThemeProvider with ThemeManager, it helped.

public static String getMessage(AuthenticationFlowContext context, String key) {
        String result = null;
        try {
            KeycloakSession keycloakSession = context.getSession();

            RealmModel realmModel = context.getRealm();

            String loginTheme = realmModel.getLoginTheme();

            ThemeManager themeManager = keycloakSession.theme();

            Theme currentTheme = themeManager.getTheme(loginTheme, Theme.Type.LOGIN);

            Locale locale = 
 context.getSession().getContext().resolveLocale(context.getUser());
            result = currentTheme.getMessages(locale).getProperty(key);
          
        } catch (IOException e) {
            logger.warn(key + "not found in messages");
        }

        return result;
    }

huangapple
  • 本文由 发表于 2020年8月21日 17:05:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63519801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定