Taha
Taha

Reputation: 1242

Authentication using AngularJS and Spring Security

I want to create a login page using AngularJS , Spring-Boot and Spring-security I follow this tutorial and every thing works fine.

But It's a static way to do login it's done with an application.yml file containing user credentials like this:

security:
  user:
    name: taha 
    password: password

how can I create a login page dynamicaly by checking user credentials in Mysql database ? what changes should I made ?

Upvotes: 0

Views: 815

Answers (1)

ali akbar azizkhani
ali akbar azizkhani

Reputation: 2279

First step you need have domain model User to store users and then create Repositoy and Service layer that find user by username from database and then create UserDetailService like this

@Component("userDetailsService")
public class CustomUserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {

    @Autowired
    private IUserService userService;

    private final Logger log = LoggerFactory.getLogger(CustomUserDetailsService.class);

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User userEntity = userService.findByUsername(username);
        if (userEntity != null) {
            log.debug("------------==" + userEntity.getUsername());
            log.debug("------------==" + userEntity.getPassword());
        } else if (userEntity == null)
            throw new UsernameNotFoundException("user not found");

        return userEntity;
    }

}

and the set UserDetailService in Security Configuration like this

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


    @Inject
    @Qualifier("userDetailsService")
    private UserDetailsService userDetailsService;

    @Inject
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .userDetailsService(userDetailsService);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {

    }

}

Upvotes: 1

Related Questions