一共有三种方式来设置登录的用户名和密码

  • 通过SpringBoot的配置文件
  • 通过配置类
  • 通过自定义编写实现类

通过SpringBoot配置文件

  1. server.port=8111
  2. spring.security.user.name=root
  3. spring.security.user.password=123456

如上所示,用户名和密码分别为root和123456

通过配置类

在项目的config文件夹下编写配置类,并实现WebSecurityConfigurerAdapter

  1. package com.sheep.securitylearning.config;
  2. import lombok.RequiredArgsConstructor;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  6. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  7. import org.springframework.security.core.userdetails.UserDetailsService;
  8. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  9. import org.springframework.security.crypto.password.PasswordEncoder;
  10. /**
  11. * Created By Intellij IDEA
  12. *
  13. * @author ssssheep
  14. * @package com.sheep.securitylearning.config
  15. * @datetime 2022/8/8 星期一
  16. */
  17. @Configuration
  18. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  19. @Override
  20. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  21. BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
  22. String password = encoder.encode("root");
  23. auth.inMemoryAuthentication()
  24. .withUser("root")
  25. .password(password)
  26. .roles("admin");
  27. }
  28. @Bean
  29. PasswordEncoder passwordEncoder() {
  30. return new BCryptPasswordEncoder();
  31. }
  32. }

通过上述配置的配置,就将用户名和密码分别为:root和root的用户配置到了内存当中

自定义编写实现类

同样也是通过配置类的方式来实现,但是配置类中重写的方法不同

  1. package com.sheep.securitylearning.config;
  2. import lombok.RequiredArgsConstructor;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  6. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  7. import org.springframework.security.core.userdetails.UserDetailsService;
  8. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  9. import org.springframework.security.crypto.password.PasswordEncoder;
  10. /**
  11. * Created By Intellij IDEA
  12. *
  13. * @author ssssheep
  14. * @package com.sheep.securitylearning.config
  15. * @datetime 2022/8/8 星期一
  16. */
  17. @Configuration
  18. @RequiredArgsConstructor
  19. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  20. final UserDetailsService userDetailsService;
  21. @Override
  22. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  23. auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
  24. }
  25. @Bean
  26. PasswordEncoder passwordEncoder() {
  27. return new BCryptPasswordEncoder();
  28. }
  29. }

然后自定义一个userDetailsService的Bean
并且需要实现UserDetailsService这个接口
重写其中的方法,来返回UserDetails
但是UserDetails实际上是一个接口,因此我们需要返回的其实是它的实现类User
image.png
image.png

  1. @Service("userDetailsService")
  2. public class MyUserDetailServiceImpl implements UserDetailsService {
  3. @Override
  4. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  5. List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
  6. return new User("mary", new BCryptPasswordEncoder().encode("123"), auths);
  7. }
  8. }