SpringSecurity+Shiro学习笔记(哔哩哔哩狂神视频)

认证:authentication

授权:authorization

过滤器拦截器

简化变成框架

1. Spring Security

@EnableXXXX:开启某功能

@EnableWebSecurity:开启 WebSecurity 模式

  1. 导入依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
  1. 配置文件:
package com.wei.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人可以访问
        //功能页有权限的人才可以访问
        http.authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/level1/**").hasRole("vip1")
            .antMatchers("/level2/**").hasRole("vip2")
            .antMatchers("/level3/**").hasRole("vip3");

        //没有权限会自动跳转到登录页,需要开启的登录页面
        //定制登入页面,需一一对应
        http.formLogin().loginPage("/toLogin")
            .usernameParameter("user")
            .passwordParameter("pwd")
            .loginProcessingUrl("/login");
        //注销
        //注销成功去首页
        http.logout().logoutSuccessUrl("/");
        //关闭CSRF功能
        http.csrf().disable();
        //记住我功能,用Cookie实现,自定义接收前端参数
        http.rememberMe().rememberMeParameter("remember");
    }

    //密码编码加密:在Spring Security 5+中,新增了许多加密方法。password1中标明了编码加密
    //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //这些数据从数据库中读取
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
            .withUser("cosmos").password(new BCryptPasswordEncoder().encode("134567")).roles("vip1", "vip2")
            .and()
            .withUser("wei").password("123456").roles("vip3", "vip2")
            .and()
            .withUser("xu").password("123456").roles("vip3", "vip2", "vip1");
    }
}
  1. Thymeleaf整合
    1. 导入依赖
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>

​ 2. 导入命名空间sec

xmlns :sec="heep://thymeleaf.org/thymleaf=extras-springsecurity4"

​ 3. 根据不同权限显示不同组件

sec:authorize="hashole('vip1')"

2. Apache Shiro

可以在JavaSE中运行

  1. 导入依赖
  2. 配置文件
  3. QuikerStart
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
currentUser.isAuthenticated()
currentUser.getPrincipal() 
currentUser.hasRole("schwartz")
currentUser.isPermitted("lightsaber:wield")
currentUser.logout();

1.在SpringBoot中集成

在整合mybatis中遇到的一些问题:

  1. <select id="queryUserByName" parameterType="String" resultType="User">
    

    把parameterType,resultType写成parameterMap,resultMap报错

  2. url: jdbc:mysql://localhost:3306/cosmos?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    

    把cosmos数据库写成mybatis 直接写了以前的数据库名(复制以前代码)。这个参数是用来填数据库名的。

在授权中遇到的问题 1:

//授权
filterChainDefinitionMap.put("/user/add","perms[user:add]");
filterChainDefinitionMap.put("/user/updata","perms[user:updata]");

//拦截
filterChainDefinitionMap.put("/user/*", "authc");

授权与拦截语句位置颠倒会导致拦截失败。

在授权中遇到的问题 2:

filterChainDefinitionMap.put("/user/*", "perms[user:both]");

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ci9Agflw-1618022796750)(C:\Users\cosmoswei\AppData\Roaming\Typora\typora-user-images\image-20210409162847612.png)]

无法授权两个权限

解决方案:两次授权

filterChainDefinitionMap.put("/user/add", "perms[user:both]");
filterChainDefinitionMap.put("/user/updata", "perms[user:both]");

整合Thymeleaf

  1. 导入依赖
  2. 配置ShiroDialect
  3. 前端设置

遇到的问题:

  1. 多重权限列表无法加载进首页

  2. 整合Thymeleaf后权限失效,进无权限页面。

  3. 发现问题:

    • 多重权限不能展示权限列表但可以进权限界面

    • 单一权限可以展示权限列表不能进权限界面

    • 无权限展示权限列表错误不能进权限界面

    问题原因:上面给cosmos两次授权造成的权限冲突,注释代码可以解决。

    但是如何解决多权限认证呢?

af后权限失效,进无权限页面。

  1. 发现问题:

    • 多重权限不能展示权限列表但可以进权限界面

    • 单一权限可以展示权限列表不能进权限界面

    • 无权限展示权限列表错误不能进权限界面

    问题原因:上面给cosmos两次授权造成的权限冲突,注释代码可以解决。

    但是如何解决多权限认证呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值