在我们的java web应用程序中,我们已经从adal4j迁移到了msal4j。
所有这些都工作得很好,但最大的区别是,当用户已经被记录(可能在其他应用程序中,但在同一个浏览器会话中)时,我们总是看到"select user“页面,并且用户不会像以前使用adal4j那样自动记录和重定向uri。
这就是我们如何重定向到编辑页面的方式:
private static void redirectToAuthorizationEndpoint(IdentityContextAdapter contextAdapter) throws IOException {
final IdentityContextData context = contextAdapter.getContext();
final String state = UUID.randomUUID().toString();
final String nonce = UUID.randomUUID().toString();
context.setStateAndNonce(state, nonce);
contextAdapter.setContext(context);
final ConfidentialClientApplication client = getConfidentialClientInstance();
AuthorizationRequestUrlParameters parameters = AuthorizationRequestUrlParameters
.builder(props.getProperty("aad.redirectURI"), Collections.singleton(props.getProperty("aad.scopes"))).responseMode(ResponseMode.QUERY)
.prompt(Prompt.SELECT_ACCOUNT).state(state).nonce(nonce).build();
final String authorizeUrl = client.getAuthorizationRequestUrl(parameters).toString();
contextAdapter.redirectUser(authorizeUrl);
}
我试图删除.prompt(Prompt.SELECT_ACCOUNT)
,但收到了一个错误
有什么想法吗?
发布于 2022-05-26 11:24:59
·即使在启用SSO之后,您也可能在切换到浏览器中的MSAL4J之后获得选择用户帐户的选项,因为MSAL4J或clearing the token cache is enabled in your code
选项被抛出并相应地指定,因此应用程序以交互方式请求令牌。
因此,请检查哪些帐户信息存储在缓存中,如下所示:
ConfidentialClientApplication pca = new ConfidentialClientApplication.Builder(
labResponse.getAppId()).
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
build();
Set<IAccount> accounts = pca.getAccounts().join(); ’
然后,根据上述信息,if you want to remove the accounts whose prompts you don’t want to see during the user account selection such that the default account should get selected and signed in automatically, execute the below code
通过修改所需的信息:-
Set<IAccount> accounts = pca.getAccounts().join();
IAccount accountToBeRemoved = accounts.stream().filter(
x -> x.username().equalsIgnoreCase(
UPN_OF_USER_TO_BE_REMOVED)).findFirst().orElse(null);
pca.removeAccount(accountToBeRemoved).join();
·对于代码中的MsalInteractiveRequiredException类,请参阅下面的官方文档链接,以了解AcquireTokenSilently和其他对此行为负有责任的原因。此外,请参阅下面给出的示例代码,以供参考:-
IAuthenticationResult result;
try {
ConfidentialClientApplication application =
ConfidentialClientApplication
.builder("clientId")
.b2cAuthority("authority")
.build();
SilentParameters parameters = SilentParameters
.builder(Collections.singleton("scope"))
.build();
result = application.acquireTokenSilently(parameters).join();
}
catch (Exception ex){
if(ex instanceof MsalInteractionRequiredException){
// AcquireToken by either AuthorizationCodeParameters or DeviceCodeParameters
} else{
// Log and handle exception accordingly
}
}
https://stackoverflow.com/questions/72347173
复制相似问题