我正在使用基于Spring的Auth-Server,该服务器创建JWT。
clients.jdbc(dataSource())
.withClient("sampleClientId")
.authorizedGrantTypes("implicit", "password", "authorization_code", "refresh_token")
.scopes("read", "write", "foo")
.autoApprove(false)
.accessTokenValiditySeconds(3600)
.redirectUris("xxx","http://localhost:8080/pmt/", "http://localhost:8080/pmt/index.html", "http://localhost:8080/login/oauth2/code/custom")
要保护对身份验证服务器的访问,我使用WebSecurityConfigurerAdapter:
public class ServerSecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.eraseCredentials(false);
auth.ldapAuthentication() ....;
}
...
}
在客户端,我有一个angular应用程序,其中使用angular-oauth2-oidc实现隐式流。
auth.service.ts:
export const authConfig: AuthConfig = {
loginUrl: 'http://localhost:8080/pmtauth/oauth/authorize',
redirectUri: 'http://localhost:8080/pmt/',
clientId: 'sampleClientId',
scope: 'read write foo',
responseType: 'id_token token',
requireHttps: false,
showDebugInformation: true,
tokenEndpoint: 'http://localhost:8080/pmtauth/oauth/token/',
oidc: false,
};
@Injectable()
export class AuthService {
constructor(
private route: ActivatedRoute,
private http: HttpClient,
private oauthService: OAuthService) {
this.oauthService.configure(authConfig);
this.oauthService.setStorage(sessionStorage);
this.oauthService.tryLogin();
}
login() {
this.oauthService.initImplicitFlow();
}
checkCredentials() {
if (this.oauthService.getAccessToken() === null) {
return false;
}
return true;
}
logout() {
this.oauthService.logOut();
location.reload();
}
...}
app.module.ts:
@NgModule({
bootstrap: [App],
declarations: [
App
],
imports: [ // import Angular's modules
BrowserModule,
HttpClientModule,
RouterModule,
FormsModule,
ReactiveFormsModule,
NgaModule.forRoot(),
NgbModule.forRoot(),
OAuthModule.forRoot(),
PagesModule,
routing
],
providers: [
AppState,
GlobalState,
AuthService,
{ provide: OAuthStorage, useValue: sessionStorage },
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
]
在调用方法initImplicitFlow()之后,将显示Auth-Server的Login-Page。当我输入正确的凭据(在我的情况下为LDAP凭据)时,客户端给出的redirectUri称为:
http://localhost:8080/pmt/#access_token=<Token here>
&token_type=bearer
&state=lfSUoxuFJdp7O59UNb0gtXQPOOzcIB4ege0GDnPc
&expires_in=2162
&organization=usernamempdDr
&jti=742fed68-5af3-42f5-b0d9-b93433e28ef7
然后,应用程序“重定向”到此页面http://localhost:8080/pmt/#
。
因此,正如我在URL中看到的那样,我从Auth-Server收到了有效的令牌,但是angular-oauth2-oidc不会提取它并将其放入会话存储中。 getIdToken,getAccessToken,hasValidIdToken等始终返回null / false。我的日志中没有错误。我调试了OAuthService类,但从未调用过callOnTokenReceivedIfExists()或storeAccessTokenResponse()。
顺便说一句:我第一次在Auth-Server上调用authorize-方法时,我必须允许每个作用域。对于隐式流来说,这是正常现象吗?