[英]How to Login with Angular by ldap with using Rest and Spring security
I hava problem . 我有问题。 I am trying to login in angular form .
我正在尝试以有角度的形式登录。
My users are in LDAP. 我的用户使用LDAP。 I will authenticate by Rest and Spring security
我将通过Rest和Spring安全性进行身份验证
but i don't know how to send username and password to the Backend and to authenticate the user. 但是我不知道如何将用户名和密码发送到后端以及对用户进行身份验证。
This is authentication.service.ts : 这是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});
}
}
This is WebSecurityConfig.java 这是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);
}
}
Please help me. 请帮我。
You can find a greate article for Spring LDAP here Spring-LDAP 您可以在此处找到有关Spring LDAP的精彩文章Spring-LDAP
Please use an HeaderInterceptor to set Request Header. 请使用HeaderInterceptor设置请求标头。 Example here:
这里的例子:
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;
}
}
And please use the POST-Method to send User-Credentials for Authentication. 并且,请使用POST方法发送用于身份验证的用户凭据。 Regards
问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.