From 1469e8544332c24d73a7b120d8b4ceb52ee852e8 Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Tue, 21 Dec 2021 15:39:53 +0800 Subject: [PATCH] =?UTF-8?q?[webapp,manager]=20=E8=B7=AF=E7=94=B1=E5=AE=88?= =?UTF-8?q?=E5=8D=AB=EF=BC=8C=E5=8D=87=E7=BA=A7sureness?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manager/pom.xml | 2 +- manager/src/main/resources/sureness.yml | 4 +-- .../src/app/core/guard/detect-auth-guard.ts | 27 +++++++++++++++++++ .../src/app/routes/routes-routing.module.ts | 4 +-- .../src/app/service/local-storage.service.ts | 4 +++ 5 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 web-app/src/app/core/guard/detect-auth-guard.ts diff --git a/manager/pom.xml b/manager/pom.xml index d291aa6..492e7a9 100644 --- a/manager/pom.xml +++ b/manager/pom.xml @@ -108,7 +108,7 @@ com.usthe.sureness spring-boot-starter-sureness - 1.0.0-beta.2 + 1.0.6 diff --git a/manager/src/main/resources/sureness.yml b/manager/src/main/resources/sureness.yml index 217dc0c..496710d 100644 --- a/manager/src/main/resources/sureness.yml +++ b/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 diff --git a/web-app/src/app/core/guard/detect-auth-guard.ts b/web-app/src/app/core/guard/detect-auth-guard.ts new file mode 100644 index 0000000..98c010e --- /dev/null +++ b/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 | Promise | boolean | UrlTree { + let activate = this.localStorageSvc.hasAuthorizationToken(); + if (!activate) { + setTimeout(() => { + this.notifySvc.warning('请先登陆!','') + this.router.navigateByUrl('/passport/login'); + }); + } + return activate; + } +} diff --git a/web-app/src/app/routes/routes-routing.module.ts b/web-app/src/app/routes/routes-routing.module.ts index 13c0610..053eba2 100644 --- a/web-app/src/app/routes/routes-routing.module.ts +++ b/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'}, diff --git a/web-app/src/app/service/local-storage.service.ts b/web-app/src/app/service/local-storage.service.ts index 128c989..7e7bd65 100644 --- a/web-app/src/app/service/local-storage.service.ts +++ b/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(); }