KeyCloak getProvider() return 空
KeyCloak getProvider() return null
我的 KeycloakSmsAuthenticatorUtil class 有一个使用 themeProvider 获取主题的 getMessage(AuthenticationFlowContext context, String key) 方法,但是 keycloakSession.getProvider() 总是 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 为空,所以程序在接收到当前主题时在下一行崩溃。
.[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)
如何获得不可为 null 的 themeProvider?
错误在 ThemeProvider 中,而不是在上下文或 keycloakSession 中。
我用 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 + "not found in messages");
}
return result;
}
我的 KeycloakSmsAuthenticatorUtil class 有一个使用 themeProvider 获取主题的 getMessage(AuthenticationFlowContext context, String key) 方法,但是 keycloakSession.getProvider() 总是 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 为空,所以程序在接收到当前主题时在下一行崩溃。
.[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)
如何获得不可为 null 的 themeProvider? 错误在 ThemeProvider 中,而不是在上下文或 keycloakSession 中。
我用 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 + "not found in messages");
}
return result;
}