平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

50 lines
1.3 KiB

  1. //
  2. using Microsoft.AspNetCore.Authentication.JwtBearer;
  3. namespace SafeCampus.Web.Core;
  4. /// <summary>
  5. /// 认证相关组件
  6. /// </summary>
  7. public sealed class AuthComponent : IServiceComponent
  8. {
  9. public void Load(IServiceCollection services, ComponentContext componentContext)
  10. {
  11. // JWT配置
  12. services.AddJwt<JwtHandler>(enableGlobalAuthorize: true, jwtBearerConfigure: options =>
  13. {
  14. //signalr jwt配置
  15. options.Events = new JwtBearerEvents
  16. {
  17. OnMessageReceived = context =>
  18. {
  19. var accessToken = context.Request.Query["access_token"];
  20. // If the request is for our hub...
  21. var path = context.HttpContext.Request.Path;
  22. if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/hubs"))
  23. {
  24. // Read the token out of the query string
  25. context.Token = accessToken;
  26. }
  27. return Task.CompletedTask;
  28. }
  29. };
  30. });
  31. // 允许跨域
  32. services.AddCorsAccessor();
  33. //注册自定义授权筛选器
  34. services.AddMvcFilter<MyAuthorizationFilter>();
  35. }
  36. }