问题
I've created angular 2 application using Jhipster. i want use new user role like ROLE_MANAGER in my application to route him to different view after logging in, what are all the files i need to change in back-end and in UI can anyone help me.
i tried changing following files not succeded, new role is not adding in database table.
src\main\java\com\mycompany\myapp\security\AuthoritiesConstants.java
src\main\resources\config\liquibase\authorities.csv
src\main\resources\config\liquibase\users_authorities.csv
if anyone done this before please explain how to route each user to different views.
回答1:
With the current version, your problem may solved by performing the following changes to your generated angular application
change the login service (login.service.ts) to return account instead of token
in
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);
});
});
}
change resolve(data)
to resolve(account)
catch the account in login.component.ts
in the function login, add account to then() like this
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;
});
}
This will check the account for having the ROLE_MANAGER role being present, and overrides the default behavior, which redirects the user to the previous state.
来源:https://stackoverflow.com/questions/42298154/how-to-add-new-user-role-to-jhipster4-0-3-application