Spring Security 设置登录的用户名和密码的三种方式
Posted hellodev
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Security 设置登录的用户名和密码的三种方式相关的知识,希望对你有一定的参考价值。
第一种方式:通过配置文件
spring:
security:
user:
name: root
password: root
第二种方式:通过配置类
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encode = passwordEncoder.encode("123");
auth.inMemoryAuthentication().withUser("root").password(encode).roles("admin");
@Bean
PasswordEncoder passwordEncoder()
return new BCryptPasswordEncoder();
第三种方式:自定义编写实现类
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
@Bean
PasswordEncoder passwordEncoder()
return new BCryptPasswordEncoder();
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException
List<GrantedAuthority> authorityList = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
return new User("root", new BCryptPasswordEncoder().encode("123"), authorityList);
转自:https://www.cnblogs.com/xl4ng/p/15055554.html
以上是关于Spring Security 设置登录的用户名和密码的三种方式的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 REST API 为 Spring-social 和 spring-security 设置登录服务?
Spring Security Ldap验证userDn和登录表单中的密码
通过facebook登录后设置spring security记住我的cookie