一共有三种方式来设置登录的用户名和密码
- 通过SpringBoot的配置文件
- 通过配置类
- 通过自定义编写实现类
通过SpringBoot配置文件
server.port=8111
spring.security.user.name=root
spring.security.user.password=123456
通过配置类
在项目的config
文件夹下编写配置类,并实现WebSecurityConfigurerAdapter
package com.sheep.securitylearning.config;
import lombok.RequiredArgsConstructor;
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.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* Created By Intellij IDEA
*
* @author ssssheep
* @package com.sheep.securitylearning.config
* @datetime 2022/8/8 星期一
*/
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String password = encoder.encode("root");
auth.inMemoryAuthentication()
.withUser("root")
.password(password)
.roles("admin");
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
通过上述配置的配置,就将用户名和密码分别为:root和root的用户配置到了内存当中
自定义编写实现类
同样也是通过配置类的方式来实现,但是配置类中重写的方法不同
package com.sheep.securitylearning.config;
import lombok.RequiredArgsConstructor;
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.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* Created By Intellij IDEA
*
* @author ssssheep
* @package com.sheep.securitylearning.config
* @datetime 2022/8/8 星期一
*/
@Configuration
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
final UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
然后自定义一个userDetailsService
的Bean
并且需要实现UserDetailsService
这个接口
重写其中的方法,来返回UserDetails
但是UserDetails
实际上是一个接口,因此我们需要返回的其实是它的实现类User
@Service("userDetailsService")
public class MyUserDetailServiceImpl implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
return new User("mary", new BCryptPasswordEncoder().encode("123"), auths);
}
}