Multi-Window Support

在Android 7.0之后,多个app可以同时在屏幕上显示。这并不改变app的生命周期。
要支持分屏,首先在<activity>下面添加resizeableActivity
android:resizeableActivity=["true" | "false"]
要实现PictureInPicture,需要设定supportsPictureInPicture
android:supportsPictureInPicture=["true" | "false"]
一些System UI的客户化会被disable,如不能隐藏status bar
会忽略screenOrientation
Activity 提供一下方法用于support muti-window
IsInMutiwindowMode(),isInPictureInPictureMode()
onMultiwindowModeChanged(),onPictureInPictureModeChanged()
当两个Activity share 屏幕的时候,可以把数据从一个Activity 拖到另一个Activity。为实现这个feature,提供一下方法。
DragAndDropPermissions()
View.startDragAndDrop()
view.cancelDragAndDrop()
View.updateDragShadow()
Activity.requestDragAndDropPermisssions()
// Required for Angular multi-browser support import { BrowserModule } from '@angular/platform-browser'; // Required for Angular import { NgModule } from '@angular/core'; // Required modules and components for this application import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { ProfileComponent } from './profile/profile.component'; import { HomeComponent } from './home/home.component'; // HTTP modules required by MSAL import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; // Required for MSAL import { IPublicClientApplication, PublicClientApplication, InteractionType, BrowserCacheLocation, LogLevel } from '@azure/msal-browser'; import { MsalGuard, MsalInterceptor, MsalBroadcastService, MsalInterceptorConfiguration, MsalModule, MsalService, MSAL_GUARD_CONFIG, MSAL_INSTANCE, MSAL_INTERCEPTOR_CONFIG, MsalGuardConfiguration, MsalRedirectComponent } from '@azure/msal-angular'; const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 || window.navigator.userAgent.indexOf('Trident/') > -1; export function MSALInstanceFactory(): IPublicClientApplication { return new PublicClientApplication({ auth: { // 'Application (client) ID' of app registration in the Microsoft Entra admin center - this value is a GUID clientId: "Enter_the_Application_Id_Here", // Full directory URL, in the form of https://login.microsoftonline.com/<tenant> authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here", // Must be the same redirectUri as what was provided in your app registration. redirectUri: "http://localhost:4200", }, cache: { cacheLocation: BrowserCacheLocation.LocalStorage, storeAuthStateInCookie: isIE } }); } // MSAL Interceptor is required to request access tokens in order to access the protected resource (Graph) export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration { const protectedResourceMap = new Map<string, Array<string>>(); protectedResourceMap.set('https://graph.microsoft.com/v1.0/me', ['user.read']); return { interactionType: InteractionType.Redirect, protectedResourceMap }; } // MSAL Guard is required to protect routes and require authentication before accessing protected routes export function MSALGuardConfigFactory(): MsalGuardConfiguration { return { interactionType: InteractionType.Redirect, authRequest: { scopes: ['user.read'] } }; } // Create an NgModule that contains the routes and MSAL configurations @NgModule({ declarations: [ AppComponent, HomeComponent, ProfileComponent ], imports: [ BrowserModule, AppRoutingModule, HttpClientModule, MsalModule ], providers: [ { provide: HTTP_INTERCEPTORS, useClass: MsalInterceptor, multi: true }, { provide: MSAL_INSTANCE, useFactory: MSALInstanceFactory }, { provide: MSAL_GUARD_CONFIG, useFactory: MSALGuardConfigFactory }, { provide: MSAL_INTERCEPTOR_CONFIG, useFactory: MSALInterceptorConfigFactory }, MsalService, MsalGuard, MsalBroadcastService ], bootstrap: [AppComponent, MsalRedirectComponent] }) export class AppModule { },修改上面代码,让我使用app.config.ts,不给login页面添加守卫,也可以调用登录方法
最新发布
03-25
<think>嗯,用户希望将现有的Angular模块代码迁移到使用app.config.ts,并且不想给登录页面添加守卫,同时仍然能够调用登录方法。首先,我需要理解用户的需求。用户可能是在使用Angular和MSAL进行身份验证,现在想利用Angular的新特性,比如应用配置(app.config.ts),来更清晰地管理配置。同时,他们希望登录页面不受守卫保护,这样用户可以直接访问而无需先登录,但其他部分可能需要身份验证。 首先,我需要回顾Angular中的配置方式。在Angular中,应用配置通常通过providers数组在AppModule或通过独立的配置文件(如app.config.ts)来设置。用户提到的app.config.ts可能是Angular 14或更高版本引入的独立应用配置,允许在不使用NgModule的情况下配置providers。 接下来,用户希望修改现有的代码,将MSAL的配置移到app.config.ts中。这涉及到将原本在NgModule中的providers迁移到配置文件中。同时,用户不希望登录页面被守卫保护,这意味着需要调整路由配置,可能是在路由定义中不为登录路由设置MsalGuard。 另外,用户提到即使不添加守卫,也能调用登录方法。这可能涉及到在组件中显式调用MSAL的登录方法,而不是依赖守卫自动重定向到登录页面。需要确保在登录组件或服务中有方法触发登录流程,例如使用MsalService的loginRedirect或loginPopup方法。 现在,我需要检查原始代码。原始代码使用了NgModule,并在providers中配置了MSAL的相关服务,包括拦截器、实例、守卫配置等。用户希望将这些配置迁移到app.config.ts,这可能涉及到使用provideMsal实例化MSAL,并在配置中定义各个提供者。 然后,需要处理路由守卫的问题。原来的代码可能在路由配置中使用了MsalGuard来保护某些路由。如果用户不希望登录页面有守卫,需要确保该路由没有设置canActivate为MsalGuard。或者,可能在守卫的配置中排除登录路由,但这需要调整守卫的逻辑或路由定义。 接下来,如何将NgModule中的配置转移到app.config.ts?可能需要使用Angular的provide函数来创建提供者,例如provideMsal,并在其中配置MSAL实例、拦截器和守卫。同时,HTTP拦截器需要作为独立提供者添加。 此外,需要确保在app.config.ts中正确导出配置,并在main.ts中通过bootstrapApplication函数应用这些配置,同时确保组件和路由的正确设置。 需要注意的潜在问题包括:是否正确处理了所有MSAL的提供者,是否正确配置了拦截器和守卫,路由是否正确定义以避免自动跳转到登录页面,以及登录方法是否在需要的地方被正确调用。 可能还需要调整路由模块,将登录页面的路由的canActivate属性移除,或者设置不同的守卫策略。同时,在登录组件中,可能需要注入MsalService并调用登录方法,例如在按钮点击事件中触发登录流程。 最后,需要测试这些修改,确保应用能够正确加载,登录页面无需守卫即可访问,并且调用登录方法能够成功进行身份验证,同时其他受保护的路由仍然由守卫正确保护。</think>以下是修改后的代码实现,分步骤说明: 1. 首先创建`app.config.ts`文件,将MSAL配置迁移到这里: ```typescript // app.config.ts import { ApplicationConfig } from '@angular/core'; import { provideRouter } from '@angular/router'; import { routes } from './app.routes'; import { MSAL_INSTANCE, MSAL_GUARD_CONFIG, MSAL_INTERCEPTOR_CONFIG, MsalGuard, MsalInterceptor, MsalService, MsalBroadcastService, provideMsal } from '@azure/msal-angular'; import { IPublicClientApplication, PublicClientApplication, InteractionType, BrowserCacheLocation } from '@azure/msal-browser'; import { HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'; const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 || window.navigator.userAgent.indexOf('Trident/') > -1; export function MSALInstanceFactory(): IPublicClientApplication { return new PublicClientApplication({ auth: { clientId: "Enter_the_Application_Id_Here", authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here", redirectUri: "http://localhost:4200", }, cache: { cacheLocation: BrowserCacheLocation.LocalStorage, storeAuthStateInCookie: isIE } }); } const protectedResourceMap = new Map<string, Array<string>>(); protectedResourceMap.set('https://graph.microsoft.com/v1.0/me', ['user.read']); export const msalConfig = { interactionType: InteractionType.Redirect, // 定义交互类型 protectedResourceMap // 需要保护的API资源 }; export const guardConfig = { interactionType: InteractionType.Redirect, authRequest: { scopes: ['user.read'] } }; export const appConfig: ApplicationConfig = { providers: [ provideRouter(routes), provideHttpClient(withInterceptorsFromDi()), provideMsal(), { provide: MSAL_INSTANCE, useFactory: MSALInstanceFactory }, { provide: MSAL_INTERCEPTOR_CONFIG, useValue: msalConfig }, { provide: MSAL_GUARD_CONFIG, useValue: guardConfig }, { provide: HTTP_INTERCEPTORS, useClass: MsalInterceptor, multi: true }, MsalService, MsalGuard, MsalBroadcastService ] }; ``` 2. 修改`app.module.ts`保留核心组件声明: ```typescript // app.module.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { ProfileComponent } from './profile/profile.component'; import { HomeComponent } from './home/home.component'; import { MsalRedirectComponent } from '@azure/msal-angular'; @NgModule({ declarations: [ AppComponent, HomeComponent, ProfileComponent ], imports: [ BrowserModule ], bootstrap: [AppComponent, MsalRedirectComponent] }) export class AppModule { } ``` 3. 路由配置示例(`app.routes.ts`): ```typescript import { Routes } from '@angular/router'; import { HomeComponent } from './home/home.component'; import { ProfileComponent } from './profile/profile.component'; export const routes: Routes = [ { path: 'profile', component: ProfileComponent, canActivate: [MsalGuard] // 仅保护需要认证的路由 }, { path: 'home', component: HomeComponent // 不添加守卫 }, { path: '', redirectTo: 'home', pathMatch: 'full' } ]; ``` 4. 在任意组件中手动触发登录(示例): ```typescript // home.component.ts import { Component } from '@angular/core'; import { MsalService } from '@azure/msal-angular'; @Component({ selector: 'app-home', template: ` <button (click)="login()">手动登录</button> ` }) export class HomeComponent { constructor(private msalService: MsalService) {} login() { this.msalService.loginRedirect(); // 使用重定向方式 // 或使用弹出窗口方式 // this.msalService.loginPopup(); } } ``` 关键修改点说明: 1. 配置分离:将MSAL相关配置全部迁移到`app.config.ts`,使用`provideMsal()`简化配置 2. 守卫控制:只在需要保护的路由(如`/profile`)添加`MsalGuard`,登录页面保持开放 3. 手动登录:通过注入`MsalService`服务,在按钮点击事件中主动触发登录流程 4. 兼容性处理:保留`MsalRedirectComponent`用于处理登录后的重定向回调 验证要点: - 确保`app.config.ts`中的clientId和authority已替换为实际值 - 检查路由配置中没有给登录页面添加`MsalGuard` - 手动登录按钮点击后应触发身份验证流程 - 受保护路由(如`/profile`)在未登录时访问会自动跳转
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值