My linux world » Java Spring Security – Database User

Java Spring Security - Database User




To enable spring security, you must create :

public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
   //do nothing


To check if the user is allowed to connect, create :

public class UserDetailsServiceImpl implements UserService, UserDetailsService {	
	private MyUserService myUserService = null;	
	public Contact loadUserByUsername(String username) {
                // get User from database :
		User user = myUserService .getUser(username);		
		if(user == null) {
			// User not found, throw UsernameNotFoundException.
			throw new UsernameNotFoundException("UsernameNotFoundException");
                // user found :
		return user;


@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    private UserDetailsService userDetailService;
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // use BCrypt algorithm to encode passwords :
        auth.userDetailsService(userDetailService).passwordEncoder(new BCryptPasswordEncoder());
    protected void configure(HttpSecurity http) throws Exception {

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.