首页 > 解决方案 > Angular 8 - 使用 SSC 登录实现 oaUTH 2

问题描述

我正在尝试从现有地址使用 SSC 实现登录。但我无法从该地址正确发出请求。

网址:https://www.my-system/frontend//rest/oidc/auth .... 当我点击登录此页面时,后面会自动重定向到网址 http://localhost:8080/backend/ token?code=MDk2Mzc0NQ== 返回一个令牌和一个代码

我想用这个被重定向的 url 代码返回主页

但是每次调用带有 ?code 的 url 时,此请求都会无限循环。正确的做法是只重定向一次。

我想要的另一个功能是保存 que retorna o acces-token, refresh-token http://localhost:8080/backend/token?code=MDk2Mzc0NQ==

我需要通过 url http://localhost:8080/backend/token?code=MDk2Mzc0NQ== 实现刷新令牌

登录控制器

 constructor(
    private breakpointObserver: BreakpointObserver,
    private router: Router,
    public sessionHandler: SessionHandler,
    private authService: AuthService
  ) {}

  public ngOnInit(): void {
    if (!this.sessionHandler.hasToken && this.router.url !== '') {
      this.authService.getUrlSSC();
    } 
  }

身份验证服务

export class AuthService {
  
  private loginUrl = 'api/login'; // URL to web api
  private redirectUrl = 'http://localhost:4200/home';

  constructor(private http: HttpClient) {}

  logout() {
  }

  getUrlSSC() {
    window.location.href =
        'https://www.my-system/frontend//rest/oidc/auth?response_type=code&client_id=userSSC&scope=open:AUTHH&redirect_uri=' + this.redirectUrl;
  }

  returnToken(code: string) {
    this.retrieveToken(code);
  }

  retrieveToken(code: any) {
    let params = new URLSearchParams();   
    params.append('grant_type','authorization_code');
    params.append('code',code);

    // let headers = 
    //   new HttpHeaders({'Content-type': 'application/x-www-form-urlencoded; charset=utf-8'});
       
    //   this.http.post('http://localhost:8080/ebu1-backend/token', 
    //     params.toString(), { headers: headers })
    //     .subscribe(
    //       data => this.saveToken(data),
    //       err => console.log('Error')); 
  }

  saveToken(token: any) {
    var expireDate = new Date().getTime() + (1000 * token.expires_in);
    LocalStorage.set('token.access', token.token.access);
    console.log('Obtained Access token');
    window.location.href = 'http://localhost:4200/token';
  }

  checkCredentials() {
    let acces_token = LocalStorage.accessToken;

    if(acces_token) {
      return true;
    } else {
      return false;
    }
  } 

}

令牌控制器

export class TokenController implements OnInit {

   constructor(
      private route: ActivatedRoute, private authService: AuthService) {}

  ngOnInit(): void {
   this.isLoggin= true;
    let i = window.location.href.indexOf('code');
    if(!this.isLoggin) {
this.authService.returnToken(window.location.href.substring(i + 5));
    }

  }

}

标签: angular

解决方案


推荐阅读