SpringBoot 整合 SpringSecurity

1、导入 Maven 依赖

<!-- SpringSecurity -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- ThymeLeaf 和 Security 的整合包 -->
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>

2、创建 SpringSecurity 的配置类

// 开启 WebSecurity 功能
// 1、加载了 WebSecurityConfiguration 配置类,配置安全认证策略
// 2、加载了 AuthenticationConfiguration,配置了认证信息
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // 授权(Authorization)
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 链式编程
        // 首页所有人可以访问,功能页只有对应有权限的人才能访问
        http.authorizeRequests()
                // 添加权限控制
                // antMatchers(),指定访问地址,可以使用通配符
                // permitAll(),允许所有人访问,没有权限控制
                // hasRole(),必须拥有指定的角色才能访问
                .antMatchers("/", "/index", "/index.html").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
        // 开启登陆页面,如果没有权限,跳转到默认的登陆页面
        // loginPage(),自定义页面
        // loginProcessingUrl(),自定义验证地址,如果没有,默认与 loginPage() 方法中的地址一致
        http.formLogin().loginPage("/login.do").loginProcessingUrl("/login");
        // 开启注销功能,默认会跳转到登陆页面
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/");
        // 防止网络攻击,关闭 csrf 功能
        http.csrf().disable();
        // 开启账号登陆记录,通过 cookie 实现,默认保存两周,可手动清除
        // rememberMeParameter(),自定义接受参数的名称
        http.rememberMe().rememberMeParameter("remember");
    }

    // 认证(Authentication)
    // 密码编码错误:PasswordEncoder
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 从内存中读取数据
        // 如果要从数据库中读取,请使用 jdbcAuthentication() 方法开头
        // passwordEncoder(),设置密码加密方式
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                // 添加用户账号和密码,并设置角色
                // 此处密码需要加密,否则会抛出 PasswordEncoder 异常
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3")
                .and()
                .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2", "vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
    }
}

3、SpringSecurity 的命名空间

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

<!-- 主体内容 -->

</html>

4、学习途径

  1. 官方网站:https://spring.io/projects/spring-security
  2. 官方文档:https://docs.spring.io/spring-security/site/docs/current/reference/html5/#jc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vcatory

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值