Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.web.mavenproject6.config; import com.web.mavenproject6.entities.Users; import com.web.mavenproject6.service.UserServiceImp; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Component; @Component public class CustomAuthenticationProvider implements AuthenticationProvider { @Autowired UserServiceImp userServiceImp; public CustomAuthenticationProvider() { super(); } // API @Override public Authentication authenticate(final Authentication authentication) throws AuthenticationException { final String name = authentication.getName(); final String password = authentication.getCredentials().toString(); System.err.println("!!!!Password " + password); for (Users u : userServiceImp.list()) { if (u.getUsername().equals(name) && u.getPassword().equals(password)) { final List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>(); grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); final UserDetails principal = new User(name, password, grantedAuths); final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password, grantedAuths); return auth; } } return null; } @Override public boolean supports(final Class<?> authentication) { return authentication.equals(UsernamePasswordAuthenticationToken.class); } }