package com.springoauth2.demo.config; import com.springoauth2.demo.entity.User; import com.springoauth2.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter; import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; /** * Created by wuhuachuan on 16/5/12. */ @Configuration public class MyUserDetailService extends GlobalAuthenticationConfigurerAdapter { @Autowired private UserService userService; @Override public void init(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService()); } @Bean public UserDetailsService userDetailsService() { return new UserDetailsService() { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userService.findByUsername(username); if(user != null){ return new org.springframework.security.core.userdetails.User( user.getUsername(), user.getPassword(), true, true, true, true, AuthorityUtils.createAuthorityList("USER") ); } else { throw new UsernameNotFoundException("could not find the user '" + username + "'"); } } }; } }