java - 如何使用 Rest 和 Spring 安全性通过 ldap 登录 Angular?

标签 java angular spring spring-boot spring-security

我有一个问题。我正在尝试以 Angular 形式登录。

我的用户位于 LDAP 中。我将通过 Rest 和 Spring security 进行身份验证

但我不知道如何将用户名和密码发送到后端并对用户进行身份验证。

这是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 设置请求 header 。示例如下:

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 方法发送用户凭据以进行身份​​验证。

关于java - 如何使用 Rest 和 Spring 安全性通过 ldap 登录 Angular?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57333587/

相关文章:

java - 为什么重写的函数首先被调用?

java - "complete action using"不该弹出的对话框

java - 为什么 JPA\Hibernate 不自动创建这个映射字段?

java - 使用 Canvas (Android) 绘制半透明位图

css - 如何设置子组件的高度?

angular - 拥有一个自定义类型有什么好处,它只是另一个自定义类型对象的数组

java - org.springframework.beans.factory.NoSuchBeanDefinitionException : No qualifying bean of type [org. springframework.data.hadoop.hive.HiveOperations]

spring - Spring 服务中的 Drools XStream

spring - 在身份验证回复中添加其他信息

javascript - 如何通过点击按钮使元素为 `display: none`