// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/model/User.java
// @Entity
// public class User {
//     @Id @GeneratedValue
//     private Long id;
//     
//     private String username;
//     
//     private String passwordHash;
//     
//     private Role role;
//     
//     
//     public String getUsername() {
//         return username;
//     }
//     public void setUsername(String username) {
//         this.username = username;
//     }
//     public String getPasswordHash() {
//         return passwordHash;
//     }
//     public void setPasswordHash(String passwordHash) {
//         this.passwordHash = passwordHash;
//     }
//     public Role getRole() {
//         return role;
//     }
//     public void setRole(Role role) {
//         this.role = role;
//     }
// }


import java.util.Arrays;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.coverity.security.pie.example.model.User;
package com.coverity.security.pie.example.config;





@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @PersistenceContext
    private EntityManager entityManager;
    
    @Autowired
    public void configureGlobal(final AuthenticationManagerBuilder auth, final PasswordEncoder passwordEncoder) throws Exception {
        auth.authenticationProvider(new AuthenticationProvider() {

            @Override
            public Authentication authenticate(Authentication authentication) throws AuthenticationException {
                List<?> result = entityManager.createQuery("SELECT U FROM User U WHERE U.username = :username")
                        .setParameter("username", authentication.getName())
                        .getResultList();
                if (result.size() == 0) {
                    throw new UsernameNotFoundException("Invalid username.");
                }
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/model/User.java
// @Entity
// public class User {
//     @Id @GeneratedValue
//     private Long id;
//     
//     private String username;
//     
//     private String passwordHash;
//     
//     private Role role;
//     
//     
//     public String getUsername() {
//         return username;
//     }
//     public void setUsername(String username) {
//         this.username = username;
//     }
//     public String getPasswordHash() {
//         return passwordHash;
//     }
//     public void setPasswordHash(String passwordHash) {
//         this.passwordHash = passwordHash;
//     }
//     public Role getRole() {
//         return role;
//     }
//     public void setRole(Role role) {
//         this.role = role;
//     }
// }


// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/config/SecurityConfig.java
import java.util.Arrays;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.coverity.security.pie.example.model.User;

package com.coverity.security.pie.example.config;





@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @PersistenceContext
    private EntityManager entityManager;
    
    @Autowired
    public void configureGlobal(final AuthenticationManagerBuilder auth, final PasswordEncoder passwordEncoder) throws Exception {
        auth.authenticationProvider(new AuthenticationProvider() {

            @Override
            public Authentication authenticate(Authentication authentication) throws AuthenticationException {
                List<?> result = entityManager.createQuery("SELECT U FROM User U WHERE U.username = :username")
                        .setParameter("username", authentication.getName())
                        .getResultList();
                if (result.size() == 0) {
                    throw new UsernameNotFoundException("Invalid username.");
                }
                User user = (User)result.get(0);