Spring security用户认证(设置用户名密码)
Posted 爱上口袋的天空
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring security用户认证(设置用户名密码)相关的知识,希望对你有一定的参考价值。
1、通过配置文件实现设置用户名密码
重新启动我们的服务:
2、通过配置类继承接口实现设置用户名密码
2.1、创建一个SecurityConfig配置类
2.2、代码
package com.kgf.security.config; 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.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { /*** * 设置登录的用户 * @param auth * @throws Exception */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //用来加密 BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); String password = encoder.encode("123456"); auth.inMemoryAuthentication().withUser("test").password(password).roles("admin"); } /*** * 注入PasswordEncoder到spring中 * @return */ @Bean public PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } }
启动后,登录
3、通过自定义UserDetailsService的实现类设置用户名密码
3.1、项目结构如下
3.2、查看我们的SecurityConfig配置类
package com.kgf.security.config; 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.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; /*** * 设置登录的用户 * @param auth * @throws Exception */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //这是使用内存的方式 // BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); // String password = encoder.encode("123456"); // auth.inMemoryAuthentication().withUser("test").password(password).roles("admin"); //下面使用自定义接口实现类从数据库查询 auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } /*** * 注入PasswordEncoder到spring中 * @return */ @Bean public PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } }
3.3、查看我们自定义的MyUserDetailsService类
package com.kgf.security.service; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.AuthorityUtils; 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.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import java.util.List; /*** * spring security查询用户的时候会自动到这个类中去查找 */ @Service("userDetailsService") public class MyUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { //这里我们就不去查询数据库了,直接new一个对象 List<GrantedAuthority> authorityList = AuthorityUtils.commaSeparatedStringToAuthorityList("role"); return new User("admin",new BCryptPasswordEncoder().encode("123456"),authorityList); } }
3.4、启动项目访问
以上是关于Spring security用户认证(设置用户名密码)的主要内容,如果未能解决你的问题,请参考以下文章