Spring Security入门案例
一、spring security 简介
Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架。它是用于保护基于Spring的应用程序的实际标准。
Spring Security是一个框架,致力于为Java应用程序提供身份验证和授权。与所有Spring项目一样,Spring Security的真正强大之处在于可以轻松扩展以满足自定义要求
巴拉巴拉…(详情看官方文档)
二、入门案例(demo)
首先创建SpringBoot项目,核心依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
编写一个controller(我这里写的是TestController)
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
接着启动项目,注意看控制台
这里会默认生成一个登录密码,如果我们未做任何配置,security会在启动时自动生成一个随机密码。
然后打开浏览器,访问 http://localhost:8080/test/hello
如果出现这个登录界面,说明security已经起作用了,这里security默认用户名为 user,密码去控制台查看。
登录成功后显示如下
到这里demo案例就OK了,接着使用自定义的方式,修改登录密码。
三、自定义用户名和密码
3.1、在application.yml中修改
server:
port: 8080
spring:
security:
user:
name: admin #用户名
password: 1234 #密码
3.2、编写配置类修改
主要继承 **WebSecurityConfigurerAdapter**类,重写 configure()方法
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
//加密
String encode = bCryptPasswordEncoder.encode("1234");
auth.inMemoryAuthentication().withUser("admin").password(encode).roles("admin");
}
/**
* 密码加密
*
* @return BCryptPasswordEncoder
*/
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
3.3、自定义实现类(常用)
前面两个都是单用户,将用户名和密码存入内存中,实际操作中认证都是需要操作数据库的,从数据库中获取用户信息(权限列表等)
需要我们自己去是实现 UserDetailsService接口
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// @Override
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
// //加密
// String encode = bCryptPasswordEncoder.encode("1234");
// auth.inMemoryAuthentication().withUser("admin").password(encode).roles("admin");
// }
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//使用自定义UserDetailsService实现
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
/**
* 密码加密
*
* @return BCryptPasswordEncoder
*/
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private UserMapper userMapper;
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
//从数据库获取用户信息 mybatisPlus
QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", s);
Users users = userMapper.selectOne(queryWrapper);
if (users == null) {
throw new UsernameNotFoundException("用户不存在!");
}
List<GrantedAuthority> role = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
return new User(users.getUsername(), new BCryptPasswordEncoder().encode(users.getPassword()), role);
}
}