SpringSecurity自定义权限校验(三)

本文档展示了如何创建一个自定义权限校验接口MyAuthorityPermit,并实现了一个具体的权限校验逻辑类MyAuthorityPermitImpl。该实现用于在Spring Security中根据用户权限校验请求路径的访问权限。同时,配置了Spring Security的访问控制规则,包括对特定URL的匿名访问和认证访问设置。
摘要由CSDN通过智能技术生成
INSERT INTO `tb_resource` VALUES (5, '登录权限', 'all:login');
INSERT INTO `tb_resource` VALUES (6, '登出权限', 'all:logout');
INSERT INTO `tb_resource` VALUES (7, '错误逻辑显示权限', 'all:error');
INSERT INTO `tb_resource` VALUES (8, '入口页面显示权限', 'all:toMain');
INSERT INTO `tb_role_resource` VALUES (1, 5);
INSERT INTO `tb_role_resource` VALUES (1, 6);
INSERT INTO `tb_role_resource` VALUES (1, 7);
INSERT INTO `tb_role_resource` VALUES (1, 8);
INSERT INTO `tb_role_resource` VALUES (2, 5);
INSERT INTO `tb_role_resource` VALUES (2, 6);
INSERT INTO `tb_role_resource` VALUES (2, 7);
INSERT INTO `tb_role_resource` VALUES (2, 8);
/**
 * Created by Monologue_zsj on 2021/4/15 11:17
 * Author:小脸儿红扑扑
 * Description:自定义一个权限校验接口
 */
public interface MyAuthorityPermit {

    /**
     * 权限校验逻辑方法。登录逻辑,本质上,无法处理
     * 当用户有权限的时候,返回true
     * 无权限的时候,返回false
     * 当用户登录成功后,通过用户携带的权限集合,验证用户本次访问的路径是否有访问权限
     * @param request   请求对象,可以通过请求对象获取请求路径,请求参数,请求作用域数据
     * @param authentication 用户登录后,SpringSecurity维护的用户登录标记,内部包含登录用户的名称和权限集合
     * @return  true /  false, access方法,参数如果是字符串"true"代表,有访问权限,参数如果是字符串"false",无访问权限
     */
    public boolean hasAuthority(HttpServletRequest request, Authentication authentication);
}
/**
 * Created by Monologue_zsj on 2021/4/15 11:25
 * Author:小脸儿红扑扑
 * Description:本类型的对象,必须被Spring容器管理。否则在Security配置类型中,无法获取对象
 *      通常使用路径地址作为映射规则,直接匹配资源信息,不匹配角色信息
 *      数据库中保存的权限数据是 /login   /logout   /toMain   /failure   /success等
 *      代码中通过请求对象,获取请求地址 request.getServletPath() 校验
 */
@Component
public class MyAuthorityPermitImpl implements MyAuthorityPermit {

    /**
     * 定义:请求参数中,包含权限相关信息
     *  参数名:roles;参数值:字符串数组。代表本次请求地址,需要访问的权限
     *  参数名:authorities;参数值:字符串数组。代表本次请求的地址,需要访问的权限
     * @param request   请求对象,可以通过请求对象获取请求路径,请求参数,请求作用域数据
     * @param authentication 用户登录后,SpringSecurity维护的用户登录标记,内部包含登录用户的名称和权限集合
     * @return
     */
    @Override
    public boolean hasAuthority(HttpServletRequest request, Authentication authentication) {

        //获取请求参数
        String [] roles = request.getParameterValues("roles");
        String [] authorities = request.getParameterValues("authorities");

        //获取当前登录用户拥有的权限集合列表
        System.out.println("权限集合:" + authentication.getAuthorities());
        System.out.println("身份:" + authentication.getPrincipal());
        System.out.println("凭证:" + authentication.getCredentials());
        System.out.println("明细:" + authentication.getDetails());
        System.out.println("是否登录:" + authentication.isAuthenticated());

        for (String role : roles) {     //判断角色权限
            String temp = "ROLE_" + role;   //拼接前缀
            //判断用户权限集合中,是否包含当前请求路径需要的权限
            if (authentication.getAuthorities().contains(new SimpleGrantedAuthority(temp))) {
                //登录用户权限,可以访问
                return true;
            }
        }

        for (String authority : authorities) {
            if (authentication.getAuthorities().contains(authentication)) {
                //登录用户有权限
                return true;
            }
        }
        //登录用户无权限,不可以访问
        return false;
    }
}
http.authorizeRequests()
                //.antMatchers("/toLogin").access("anonymous")    //只能匿名访问
                .antMatchers("/toLogin").anonymous()    //只能匿名访问
                .antMatchers("/failure", "/favicon.ico").permitAll()    //toLogin请求地址,可以随便访问
                .antMatchers("/**/*.js").permitAll()    //授予所有目录下的所有.js文件可访问资源
                .regexMatchers(".*[.]css").permitAll()  //授予所有目录下的所有.css文件可访问权限
                /**
                 * 使用自定义代码逻辑,实现权限校验,返回true、false字符串
                 * 在access方法的参数中,可以使用SpringEL表达式传递代码的运行逻辑
                 * @beanName,可以获取Spring容器中的对象
                 * @beanName.methodName() 可以调用对象中的方法
                 * 方法中参数传递的对象,可以在WebSecurityExpressionRoot中查看,是这个类型中的所有属性
                 */
                .antMatchers("/**").access("@myAuthorityPermitImpl.hasAuthority(request, authentication)")

                .anyRequest().authenticated();  //任意的请求必须认证后才可以访问

        //关闭CSRF安全协议,关闭是为了完整流程的可用
        http.csrf().disable();

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值