Preface:

After SpringBoot integrated OAuth2 series two (password), simple use of memory to achieve SpringBoot integrated OAuth2, today to sublimate, how to use UserDetailsService in real projects to use OAuth2

Series of articles:

SpringBoot integrated OAuth2 Series 1 (Simplest configuration)

SpringBoot integration OAuth2 series 2

SpringBoot integration oAuth2, Series 3 (UserDetailsService part)

SpringBoot integration with oAuth2, Series 4 (Cross-domain issues using oAuth2 in front and back Separated Web pages)

SpringBoot integration with oAuth2, Series 5

Effect of the referenceSpringBoot integration OAuth2 series 2

Code section

Full code: gitee.com/mayanze123/…

The following code has been changed on the basis of the previous article

1.WebSecurityConfig

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired MyCustomUserDetailsService userDetailsService; @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean public AuthenticationManager authenticationManager() throws Exception { return super.authenticationManager(); } @Bean @Override protected UserDetailsService userDetailsService() { return userDetailsService; } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @override protected void configure(HttpSecurity HTTP) throws Exception {// All requests must be authenticated http.authorizeRequests().anyRequest().authenticated(); }}Copy the code

2.MyCustomUserDetailsService

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.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service public class MyCustomUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); authorities.add(new SimpleGrantedAuthority("ROLE_SIMPLE_PROTECTED")); UserDetails userDetails = new User("mayanze","$2a$10$Ih5mveMHG.N.P4kK3VmKEe2f8CBRB6kcEbGp8TGzDRYucs5ck39tu",authorities); return userDetails; }}Copy the code

AuthorizationServerConfig

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; @Configuration public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @autoWired public PasswordEncoder PasswordEncoder; public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients .inMemory() .withClient("first-client") .secret(passwordEncoder.encode("noonewilleverguess")) .scopes("resource:read") .authorizedGrantTypes("password","authorization_code"); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) { endpoints.authenticationManager(authenticationManager); }}Copy the code