Bladeren bron

[webapp,manager] 路由守卫,升级sureness

tomsun28 4 jaren geleden
bovenliggende
commit
a22548a06d

+ 1 - 1
manager/pom.xml

@@ -108,7 +108,7 @@
         <dependency>
             <groupId>com.usthe.sureness</groupId>
             <artifactId>spring-boot-starter-sureness</artifactId>
-            <version>1.0.0-beta.2</version>
+            <version>1.0.6</version>
         </dependency>
     </dependencies>
 

+ 2 - 2
manager/src/main/resources/sureness.yml

@@ -7,11 +7,11 @@
 resourceRole:
   - /account/auth/refresh===post===[role1,role2,role3,role4]
 
-# load api resource which do not need be protected, means them need be excluded.
+# load api resource which do not need protected, means them need be excluded.
 # these api resource can be access by everyone
 excludedResource:
-  - /**===*
   - /account/auth/form===post
+  - /i18n/**===get
   - /**/*.html===get
   - /**/*.js===get
   - /**/*.css===get

+ 27 - 0
web-app/src/app/core/guard/detect-auth-guard.ts

@@ -0,0 +1,27 @@
+import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from "@angular/router";
+import {Observable} from "rxjs";
+import {Injectable} from "@angular/core";
+import {LocalStorageService} from "../../service/local-storage.service";
+import {NzNotificationService} from "ng-zorro-antd/notification";
+
+@Injectable({
+  providedIn: 'root'
+})
+export class DetectAuthGuard implements CanActivate {
+
+  constructor(private localStorageSvc : LocalStorageService,
+              private notifySvc: NzNotificationService,
+              private router: Router) { }
+
+  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
+    Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
+    let activate = this.localStorageSvc.hasAuthorizationToken();
+    if (!activate) {
+      setTimeout(() => {
+        this.notifySvc.warning('请先登陆!','')
+        this.router.navigateByUrl('/passport/login');
+      });
+    }
+    return activate;
+  }
+}

+ 2 - 2
web-app/src/app/routes/routes-routing.module.ts

@@ -1,6 +1,5 @@
 import { NgModule } from '@angular/core';
 import { RouterModule, Routes } from '@angular/router';
-import { SimpleGuard } from '@delon/auth';
 import { environment } from '@env/environment';
 // layout
 import { LayoutBasicComponent } from '../layout/basic/basic.component';
@@ -14,13 +13,14 @@ import { UserLockComponent } from './passport/lock/lock.component';
 import { UserLoginComponent } from './passport/login/login.component';
 import { UserRegisterResultComponent } from './passport/register-result/register-result.component';
 import { UserRegisterComponent } from './passport/register/register.component';
+import {DetectAuthGuard} from "../core/guard/detect-auth-guard";
 
 const routes: Routes = [
   {
     path: '',
     component: LayoutBasicComponent,
     // 路由守卫 在路由之前判断是否有认证或者权限进入此路由
-    // canActivate: [SimpleGuard],
+    canActivate: [DetectAuthGuard],
     children: [
       // todo 根据路由自动生成面包屑
       { path: '', redirectTo: 'dashboard', pathMatch: 'full'},

+ 4 - 0
web-app/src/app/service/local-storage.service.ts

@@ -35,6 +35,10 @@ export class LocalStorageService {
     return this.putData(Authorization, token);
   }
 
+  public hasAuthorizationToken() {
+    return localStorage.getItem(Authorization) === null;
+  }
+
   public clear() {
     localStorage.clear();
   }