0

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 ?

2
  • According to tutorial . "the first section we built a simple application that used HTTP Basic authentication to protect the backend resources. " Commented Mar 22, 2017 at 17:49
  • You will create UserDetailService and handle authenticate , Commented Mar 22, 2017 at 17:51

1 Answer 1

1

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 {

    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help but I'm new to spring environment can you please explain to me where to put this files in the same directory of user model and user Repository or in another folder ? also in the tutorial in configure method he used httpBasic() does I need to change it ?
I think you need some example to know, read jhipster.github.io or other site

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.