[英]How to Login with Angular by ldap with using Rest and Spring security
我有問題。 我正在嘗試以有角度的形式登錄。
我的用戶使用LDAP。 我將通過Rest和Spring安全性進行身份驗證
但是我不知道如何將用戶名和密碼發送到后端以及對用戶進行身份驗證。
這是authentication.service.ts:
export class AuthenticationService {
private baseUrl = "http://localhost:8090;
constructor(private http: HttpClient) {
}
authentication(username: string, password: string): Observable<Object> {
const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(username + ':' + password) });
return this.http.get(`${this.baseUrl}` + '/' , {headers});
}
}
這是WebSecurityConfig.java
@EnableWebSecurity(debug = true)
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private String url = "ldap://dc.msv.net:389";
private String domain = "msv.net";
private String bsDn = "DC=msv,DC=net";
private String userDNPattern = "(&(userPrincipalName={0}))";
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().antMatchers("/login").permitAll().anyRequest().authenticated()
.and().formLogin().usernameParameter("username").passwordParameter("password");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
ActiveDirectoryLdapAuthenticationProvider adProvider = new ActiveDirectoryLdapAuthenticationProvider(
domain, url, bsDn);
adProvider.setConvertSubErrorCodesToExceptions(true);
adProvider.setUseAuthenticationRequestCredentials(true);
// Checks with the Distinguished Name pattern provided
if (this.userDNPattern != null && this.userDNPattern.trim().length() > 0) {
adProvider.setSearchFilter(this.userDNPattern);
}
auth.authenticationProvider(adProvider);
}
}
請幫我。
您可以在此處找到有關Spring LDAP的精彩文章Spring-LDAP
請使用HeaderInterceptor設置請求標頭。 這里的例子:
import {
HttpInterceptor,
HttpEvent,
HttpHandler,
HttpRequest,
HttpHeaders
} from '@angular/common/http';
@Injectable()
export class HeaderInterceptor implements HttpInterceptor {
addAuthHeader(request) {
const r: HttpRequest<any> = request.clone({
headers: new HttpHeaders({
'Content-Type': 'application/json',
Authorization: 'Bearer ' + this.userService.getAccessToken()
})
});
return r;
}
}
並且,請使用POST方法發送用於身份驗證的用戶憑據。 問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.