spring-boot - 带有 msal 的 Angular 将无效令牌发送到后端
问题描述
我有一个 Angular 10 应用程序和 msal 安全库。
我使用 msal 浏览器 2.7.0
app.module.ts 的一部分
import { MsalInterceptorConfig } from './lib/msal/msal.interceptor.config';
import { MsalBroadcastService, MsalGuard, MsalInterceptor, MsalService, MSAL_INSTANCE } from './lib/msal';
import { MSAL_GUARD_CONFIG, MSAL_INTERCEPTOR_CONFIG } from './lib/msal/constants';
import { MsalGuardConfiguration } from './lib/msal/msal.guard.config';
function MSALInstanceFactory(): IPublicClientApplication {
return new PublicClientApplication({
auth: {
clientId: 'd1851226-abe8-4da6-8332-af8700e0d999',
redirectUri: 'https://localhost:4200/find',
authority: 'https://login.microsoftonline.com/4fc25658-87c2-42bf-913c-cb9ae315a768',
postLogoutRedirectUri: window.location.origin
}
});
}
function MSALInterceptorConfigFactory(): MsalInterceptorConfig {
const protectedResourceMap = new Map<string, Array<string>>();
protectedResourceMap.set('https://graph.microsoft.com/v1.0/me', ['user.read']);
protectedResourceMap.set('http://localhost:8080/v2/**', ['openid' ]);
protectedResourceMap.set('http://localhost:8081/v2/**', ['openid' ]);
return {
interactionType: InteractionType.Popup,
// interactionType: InteractionType.Redirect,
protectedResourceMap,
};
}
@NgModule({
declarations: [
AppComponent,
FormValidationHintComponent
],
imports: [
AppRoutingModule,
BrowserModule,
FontAwesomeModule,
HttpClientModule,
DataTablesModule,
ModalModule,
FormsModule,
ReactiveFormsModule,
BrowserAnimationsModule,
ToastrModule.forRoot({
preventDuplicates: true
}),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: WebpackTranslateLoader
},
defaultLanguage: 'en'
})
],
providers: [
DatePipe,
{
provide: HTTP_INTERCEPTORS,
useClass: AppHttpInterceptor,
multi: true
},
{
provide: APP_INITIALIZER,
useFactory: initializeApp,
multi: true,
deps: [HttpBackend, ConfigurationService]
},
{
provide: HTTP_INTERCEPTORS,
useClass: MsalInterceptor,
multi: true
},
{
provide: MSAL_INSTANCE,
useFactory: MSALInstanceFactory
},
{
provide: MSAL_GUARD_CONFIG,
useValue: {
//interactionType: InteractionType.Redirect,
interactionType: InteractionType.Popup
} as MsalGuardConfiguration
},
{
provide: MSAL_INTERCEPTOR_CONFIG,
useFactory: MSALInterceptorConfigFactory
},
MsalService,
MsalGuard,
MsalBroadcastService
],
bootstrap: [AppComponent]
})
export class AppModule {}
访问令牌它被发送到后端,但是当它响应无效令牌时。我在 jwt.io 上测试过,它说签名无效。
如果我使用 id 令牌,后端响应正确。
我的后端部分
@Configuration
@EnableWebSecurity(debug = true)
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter);
return jwtAuthenticationConverter;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues())
.and() // (1)
.authorizeRequests().antMatchers("/api/**").hasRole("level1")
.anyRequest().authenticated() // (2)
.and()
.oauth2ResourceServer().jwt() // (3)
.jwtAuthenticationConverter(jwtAuthenticationConverter());
}
}
在我的 application.properties 中,我有
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://login.microsoftonline.com/4fc25658-87c2-42bf-913c-cb9ae315a768/discovery/v2.0/token
解决方案
推荐阅读
- python - Telegram bot - 如何从解析的 csv 中返回带有值的答案
- python - 在 pygame 中仍然没有检测到碰撞
- sql - Postgres 将 jsonb 值连接/聚合成一个字符串
- android - gridview.count 不会滚动颤动
- javascript - 我可以使用 webcal 访问 ics 文件并直接添加事件而无需订阅吗?
- python - 我们如何使用 python 在 Windows 中找到活动窗口名称?
- android - 在 AppAuth for Android 中结束会话而不卸载应用程序
- reactjs - 出现错误:TypeError: tag[primaryAttributeKey].toLowerCase 不是 React Helmet 中的函数
- node.js - 更新温斯顿记录器
- angular - 删除角度材料中列表项之间的填充