创建自定义的myService接口

/**
 * @author etern
 * @title: MyService
 * @projectName springsecuritydemo
 * @description: TODO 自定义权限接口
 * @date 2022/1/22 20:51
 */
public interface MyService {
    /*
        request:获取对应的主体以及权限
        authentication:获取权限
     */
    boolean hasPermission(HttpServletRequest request, Authentication authentication);
}

创建对应接口的实现类

/**
 * @author etern
 * @title: MyServiceImpl
 * @projectName springsecuritydemo
 * @description: TODO 自定义访问权限的实现类
 * @date 2022/1/22 20:57
 */
public class MyServiceImpl implements MyService {
    @Override
    public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
        Object obj = authentication.getPrincipal();
        if (obj instanceof UserDetails) {
            UserDetails userDetails = (UserDetails) obj;
            //获取权限
            Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
            //判断权限中是不是有URI
            return authorities.contains(new SimpleGrantedAuthority(request.getRequestURI()));
        }
        return false;
    }
}

配置类SecurityConfig.java
设置自定义访问权限

//access结合自定义方法实现访问权限控制
.anyRequest().access("@myServiceImpl.hasPermission(request,authentication)");

注:自定义访问权限时,需要在创建用户时添加放行页面
UserDetailsServiceImpl.java

//创建用户以及权限设定,添加角色ROLE_前缀不能少,相当于告诉springmvc在ROLE_后面就是我添加的角色
return new User(username, password,
        //main.html,自定义访问权限时,缺少放行,在此处添加
        AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal,ROLE_abc,/main.html"));