【问题标题】:How to add new user role to jhipster(4.0.3) application如何将新用户角色添加到 jhipster(4.0.3) 应用程序
【发布时间】:2017-07-06 23:49:53
【问题描述】:

我使用 Jhipster 创建了 angular 2 应用程序。我想在我的应用程序中使用像 ROLE_MANAGER 这样的新用户角色,以便在登录后将他路由到不同的视图,我需要在后端和 UI 中更改哪些文件,谁能帮助我。

我尝试更改以下文件未成功,新角色未添加到数据库表中。

src\main\java\com\mycompany\myapp\security\AuthoritiesConstants.java
src\main\resources\config\liquibase\authorities.csv
src\main\resources\config\liquibase\users_authorities.csv

如果之前有人这样做过,请解释如何将每个用户路由到不同的视图。

【问题讨论】:

  • 这里有2个问题...如果您在迁移完成后更改这些文件,则正常没有更改。在开发模式下删除 /target 文件夹或使用 mvn/gradle 的 clean 任务...

标签: angular jhipster


【解决方案1】:

使用当前版本,您的问题可以通过对生成的 Angular 应用程序执行以下更改来解决

更改登录服务 (login.service.ts) 以返回帐户而不是令牌

login (credentials, callback?) {
    let cb = callback || function() {};

    return new Promise((resolve, reject) => {
        this.authServerProvider.login(credentials).subscribe(data => {
            this.principal.identity(true).then(account => {
                // After the login the language will be changed to
                // the language selected by the user during his registration
                if (account !== null) {
                    this.languageService.changeLanguage(account.langKey);
                }
                resolve(data);
            });
            return cb();
        }, err => {
            this.logout();
            reject(err);
            return cb(err);
        });
    });
}

resolve(data) 更改为resolve(account)

在 login.component.ts 中获取账号

在函数 login 中,像这样在 then() 中添加账号

login () {
    this.loginService.login({
        username: this.username,
        password: this.password,
        rememberMe: this.rememberMe
    }).then((account: Account) => {
        this.authenticationError = false;
        this.activeModal.dismiss('login success');
        if (this.router.url === '/register' || this.router.url === '/activate' ||
            this.router.url === '/finishReset' || this.router.url === '/requestReset') {
            this.router.navigate(['']);
        }

        this.eventManager.broadcast({
            name: 'authenticationSuccess',
            content: 'Sending Authentication Success'
        });

        // // previousState was set in the authExpiredInterceptor before being redirected to login modal.
        // // since login is succesful, go to stored previousState and clear previousState
        let previousState = this.stateStorageService.getPreviousState();
        //**CHANGED** check if we have are a manager
        let isManager = account.authorities.indexOf("ROLE_MANAGER") > -1;
        if(isManager) { 
            this.stateStorageService.resetPreviousState();
            this.router.navigate(['your-manager-specific-state']);
        } 
        else if (previousState) {
            this.stateStorageService.resetPreviousState();
            this.router.navigate([previousState.name], { queryParams:  previousState.params });
        }
    }).catch(() => {
        this.authenticationError = true;
    });
}

这将检查帐户是否存在 ROLE_MANAGER 角色,并覆盖默认行为,将用户重定向到之前的状态。

【讨论】:

    猜你喜欢
    • 2016-07-30
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    相关资源
    最近更新 更多