在我们的java web应用程序中,我们已经从adal4j迁移到msal4j。
都工作得很好,但最大的区别是,当用户已经登录(可能在其他应用程序,但相同的浏览器会话),我们总是看到"选择用户"页面和用户不会自动登录,并像以前一样使用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)
但是我收到一个错误
任何想法?
•即使在启用了SSO之后,您可能会在浏览器中切换到MSAL4J后选择用户帐户,因为clearing the token cache is enabled in your code
或MsalInteractionRequiredException选项被抛出并相应地指定,因此应用程序以交互方式请求令牌.
因此,请检查哪些帐户信息存储在缓存中如下:-
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类,请参考下面的官方文档链接获取acquiretokensilent以及其他导致这种行为的原因。同样,参考下面给出的示例代码作为参考:-
https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-error-handling-java msalinteractionrequiredexception
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
}
}