简体   繁体   English

来自 Ionic 的 Spring Security LDAP 身份验证

[英]Spring Security LDAP Auth from Ionic

I developed a Back End with using Java Spring and I added LDAP Authentication with extending WebSecurityConfigurerAdapter.我使用 Java Spring 开发了一个后端,并通过扩展 WebSecurityConfigurerAdapter 添加了 LDAP 身份验证。 I can get authenticated from POSTMAN but I can't from Ionic.我可以从 POSTMAN 获得身份验证,但不能从 Ionic 获得身份验证。

My Spring side ;我的春天的一面;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

//TODO: add other endpoints like /events in order to permit un-authenticated users
@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/login**").anonymous()
            .antMatchers("/resources/**").permitAll()
            .antMatchers("/assets/**").permitAll()
            .antMatchers("/").authenticated()
            .antMatchers("/home").authenticated()
            .antMatchers("/events/**").authenticated()
            .and()
            .formLogin()
            .and()
            .logout()
            .permitAll()
            .and()
            .cors()
            .and()
            .csrf()
            .disable();
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
            .ldapAuthentication()
            .contextSource()
            .url("ldap://ldap.forumsys.com/dc=example,dc=com")
            .and()
            .userSearchFilter("(uid={0})")
            .userSearchBase("ou=mathematicians")
            .userDnPatterns("uid={0}");

}

Login Controller;登录控制器;

    @RequestMapping(value = "/")
public String home() throws NamingException {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String currentPrincipalName = authentication.getName();
    return "hey, nice! = " + currentPrincipalName;
}

And my Postman Login;还有我的邮递员登录; Postman Screenshot邮递员截图

Lastly, my client (ionic) side auth code;最后,我的客户端(离子)端身份验证代码;

 authenticate(event) {
    event.preventDefault();
    const data = new URLSearchParams();
    data.append("username",this.state.username);
    data.append("password",this.state.password);

    fetch(host + "/login", {
        mode: 'no-cors',
        method: 'POST',
        body: data,
        redirect :"follow",
        headers: {
            'Accept': '*/*'
        },
        keepalive: true 
    })
   }

But from my Ionic side, I can't get "hey, nice! = euler" response as I get from POSTMAN.但是从我的 Ionic 方面来看,我无法从 POSTMAN 那里得到“嘿,不错!= euler”的回应。 I think that I handled with CORS but I didn't figure out whats the problem.我认为我处理了 CORS,但我没有弄清楚问题出在哪里。

I answered my question.我回答了我的问题。

I added proxy to my package.json and added credentials: "include" to post request header at front end.我在 package.json 中添加了代理并添加了凭据:“include”以在前端发布请求标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM