目录本章只是一个小入门 SpringSecurity的功能是否强大,用户、角色、权限,还有注解配置控制器可以参考下一章,或者官方文档。
- 1、创建工程
- 2、简介
- 3、WebSecurityConfigurerAdapter
- 3.1 创建配置类
- 3.2、授权规则
- 3.3、开启登录、注销、记住我
- 3.4、添加自定义登录页面
- 4、AuthenticationManagerBuilder
- 4.1、添加认证规则
- 5、前端
- 5.1、导入命名空间
- 5.2、修改导航栏,增加认证判断
- 5.3、角色判断
- 5.4、登录看看
- 7、完整性代码
或者
2、简介org.springframework.boot spring-boot-starter-security org.thymeleaf.extras thymeleaf-extras-springsecurity5 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web
Spring Security 是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入 spring-boot-starter-security 模块,进行少量的配置,即可实现强大的安全管理!
记住几个类:
- WebSecurityConfigurerAdapter:自定义Security策略
- AuthenticationManagerBuilder:自定义认证策略
- @EnableWebSecurity:开启WebSecurity模式
@EnableWebSecurity//开启Security
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
}
//认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
}
3.2、授权规则
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRoles("vip2","vip3")
.antMatchers("/level3/**").hasRole("vip3");
测试发现在没有登录的情况下只能进入首页,其他页面必须登录并且有相对应的角色才能访问
3.3、开启登录、注销、记住我//没有权限默认跳转到登录页,需要开启登录到页面
http.formLogin();
//关闭csrf功能
http.csrf().disable();
//注销,开启注销功能,跳转首页
http.logout().logoutSuccessUrl("/");
//开启记住我功能
http.rememberMe();
点击记住我登录功能可以发现多了一条cookir用来记录到期时间
记住我
//自定义表单的name属性同名
http.rememberMe().rememberMeParameter("remember");
//自定义login页 默认登录请求页和登录页是应该页面 当然你可以如果loginProcessingUrl自定义
http.formLogin().loginPage("/toLogin")
//以下三个参数可以选择性使用
.loginProcessingUrl("login")//自定义登录请求
//自定义用户账号表单名 如果前端账号密码表单不是username 和password
.usernameParameter("reusername")
.passwordParameter("repassword");
4、AuthenticationManagerBuilder
4.1、添加认证规则
- 使用内存
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
在内存中定义,也可以在jdbc中去拿....
//Spring security 5.0中新增了多种加密方式,也改变了密码的格式。
//要想我们的项目还能够正常登陆,需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密
//spring security 官方推荐的是使用bcrypt加密方式。
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("leitoqing").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2", "vip3")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
- 使用jdbc
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username,password, enabled from users where username = ?")
.authoritiesByUsernameQuery("select username, role from user_roles where username = ?");
}
5、前端
5.1、导入命名空间
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
5.2、修改导航栏,增加认证判断- sec:authorize 是否显示
- isAuthenticated() 判断是否登录
5.3、角色判断
5.4、登录看看.........
发现2显示不了因为没有这个角色
@EnableWebSecurity//开启Security
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人都可以访问,功能页只有对应有权限都人才能访问
http.authorizeRequests()
.antMatchers("/").permitAll()//首页放行
.antMatchers("/level1/**").hasAnyRole("vip1", "vip2")///level1/开头需要什么角色
.antMatchers("/level2/**").hasRole("vip2");
//没有权限默认跳转到登录页,需要开启登录到页面
http.formLogin();
//关闭csrf功能
http.csrf().disable();
//注销,开启注销功能,跳转首页
http.logout().logoutSuccessUrl("/");
//开启记住我功能
http.rememberMe();
// 自定义login页 默认登录请求页和登录页是应该页面 当然你可以如果loginProcessingUrl自定义
http.formLogin().loginPage("/toLogin")
.loginProcessingUrl("login")//自定义登录请求
.usernameParameter("reusername")//自定义用户账号表单名 如果前端账号密码表单不是username 和password
.passwordParameter("repassword");
http.rememberMe().rememberMeParameter("remember");
}
//认证规则
// 密码编码 passwordEncoder
//在spring Secutiry5.0+ 增加类很多加密方法~
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这是数据正常应该从数据库中读
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("leitoqing").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2", "vip3")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
}



