简体   繁体   中英

Spring Security LDAP Auth from Ionic

I developed a Back End with using Java Spring and I added LDAP Authentication with extending WebSecurityConfigurerAdapter. I can get authenticated from POSTMAN but I can't from 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. I think that I handled with CORS but I didn't figure out whats the problem.

I answered my question.

I added proxy to my package.json and added credentials: "include" to post request header at front end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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