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

This commit is contained in:
tomsun28
2021-12-21 15:39:53 +08:00
parent b21662cb1b
commit 1469e85443
5 changed files with 36 additions and 5 deletions

View File

@@ -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>

View File

@@ -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

View File

@@ -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;
}
}

View File

@@ -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'},

View File

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