【Spring-Security】Re06 自定义Access & 注解权限分配
一、基于ACCESS方法处理的实现:
我们之前使用的任何放行规则的方法,本质上还是调用access方法执行的

这也意味之我们可以直接使用access方法去方向,只需要注入不同的字符串即可
自定义Access实现:
业务层自定义接口:
package cn.zeal4j.service; import org.springframework.security.core.Authentication; import javax.servlet.http.HttpServletRequest; /**
* @author Administrator
* @file IntelliJ IDEA Spring-Security-Tutorial
* @create 2020 09 28 20:17
*/
public interface CustomService {
Boolean hasPermission(HttpServletRequest httpServletRequest, Authentication authentication);
}
实现类:
package cn.zeal4j.service; import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails; import javax.servlet.http.HttpServletRequest;
import java.util.Collection; /**
* @author Administrator
* @file IntelliJ IDEA Spring-Security-Tutorial
* @create 2020 09 28 20:20
*/
public class CustomServiceImpl implements CustomService{
@Override
public Boolean hasPermission(HttpServletRequest httpServletRequest, Authentication authentication) {
// 用户信息对象
Object principal = authentication.getPrincipal(); if (principal instanceof UserDetails) { UserDetails userDetails = (UserDetails) principal; Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities(); return authorities.contains(new SimpleGrantedAuthority(httpServletRequest.getRequestURI()));
}
return false;
}
}
Access配置:
package cn.zeal4j.configuration; import cn.zeal4j.handler.CustomAccessDeniedHandler;
import cn.zeal4j.handler.FarsAuthenticationFailureHandler;
import cn.zeal4j.handler.FarsAuthenticationSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.AccessDeniedHandler; /**
* @author Administrator
* @file IntelliJ IDEA Spring-Security-Tutorial
* @create 2020 09 27 21:55
*/
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired
private AccessDeniedHandler accessDeniedHandler; @Bean
public PasswordEncoder getPasswordEncoder() {
return new BCryptPasswordEncoder();
} @Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.formLogin(). // 设置登陆行为方式为表单登陆
// 登陆请求参数设置
usernameParameter("username").
passwordParameter("password").
loginPage("/login.html"). // 设置登陆页面URL路径
loginProcessingUrl("/login.action"). // 设置表单提交URL路径
// successForwardUrl("/main.page"). // 设置认证成功跳转URL路径 POST请求
successHandler(new FarsAuthenticationSuccessHandler("https://www.acfun.cn/")). // 使用自定义的重定向登陆
// failureForwardUrl("/error.page"); // 设置认证失败跳转URL路径 POST请求
failureHandler(new FarsAuthenticationFailureHandler("/error.html")); // 跨域处理,不需要跳转了 httpSecurity.authorizeRequests().
regexMatchers(HttpMethod.POST, "正则表达式").permitAll(). // 还可以对符合正则表达式的请求方式进行要求,这个属性使用来制定请求的方式
antMatchers("/**/*.js", "/**/*.css", "/**/images/*.*").permitAll(). // 静态资源放行
antMatchers("/login.html").permitAll(). // 登陆页面允许任意访问
antMatchers("/error.html").permitAll(). // 失败跳转后重定向的页面也需要被允许访问
antMatchers("/admin.page").hasAnyAuthority("admin").
/*antMatchers("/vip-01.page").hasAnyAuthority("vip-01").*/
antMatchers("/vip-01.page").hasRole("vip-01").
antMatchers("/ip.page").hasIpAddress("192.168.43.180").
// mvcMatchers("/main.page").servletPath("/xxx").permitAll(). // mvcMatchers资源放行匹配
// antMatchers("/xxx/main.page").permitAll(). // 或者多写MSP的前缀
anyRequest().authenticated(). // 其他请求均需要被授权访问
anyRequest().access("@customServiceImpl.hasPermission(httpServletRequest, authentication)"); // 自定义Access配置 // CSRF攻击拦截关闭
httpSecurity.csrf().disable(); httpSecurity.exceptionHandling().accessDeniedHandler(accessDeniedHandler); }
}
这个授权逻辑只允许赋予了对应的接口地址和的才能访问,像默认访问的index页面就会报错

二、@Secured注解:
package org.springframework.security.access.annotation; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Secured {
String[] value();
}
该注解可以标记的位置,方法和类上面
可以声名的值是一个字符串数组
官方的解释:
用于声名角色权限控制,判断访问此方法是否具备注解中的角色,参数值必须以ROLE_开头
具体使用:


二、@PreAuthorize & @PostAuthorize:
@PreAuthorize 用于在方法或者类之前先判断权限,参数和access方法相同
@PostAuthorize 恰恰相反,在方法和类执行之后判断
使用这些注解需要在启动类中打上@EnableGlobalMethodSecurity

内部字符串会被读取到,和在配置类中设置是差不多的

当然和配置类不同的是,该注解允许方法注入大写的角色权限前缀,例如ROLE_vip-01
【Spring-Security】Re06 自定义Access & 注解权限分配的更多相关文章
- Spring Security实现基于RBAC的权限表达式动态访问控制
昨天有个粉丝加了我,问我如何实现类似shiro的资源权限表达式的访问控制.我以前有一个小框架用的就是shiro,权限控制就用了资源权限表达式,所以这个东西对我不陌生,但是在Spring Securit ...
- spring security采用自定义登录页和退出功能
更新... 首先采用的是XML配置方式,请先查看 初识Spring security-添加security 在之前的示例中进行代码修改 项目结构如下: 一.修改spring-security.xml ...
- spring security 3 自定义认证,授权示例
1,建一个web project,并导入所有需要的lib. 2,配置web.xml,使用Spring的机制装载: <?xml version="1.0" encoding=& ...
- 基于Spring Security 的JSaaS应用的权限管理
1. 概述 权限管理,一般指根据系统设置的安全规则或者安全策略,用户可以访问而且只能访问自己被授权的资源.资源包括访问的页面,访问的数据等,这在传统的应用系统中比较常见.本文介绍的则是基于Saas系统 ...
- spring security实现动态配置url权限的两种方法
缘起 标准的RABC, 权限需要支持动态配置,spring security默认是在代码里约定好权限,真实的业务场景通常需要可以支持动态配置角色访问权限,即在运行时去配置url对应的访问角色. 基于s ...
- [转]Spring Security 可动态授权RBAC权限模块实践
RBAC:基于角色的访问控制(Role-Based Access Control) 先在web.xml 中配置一个过滤器(必须在Struts的过滤器之前) <filter> <fil ...
- SpringBoot Spring Security 核心组件 认证流程 用户权限信息获取详细讲解
前言 Spring Security 是一个安全框架, 可以简单地认为 Spring Security 是放在用户和 Spring 应用之间的一个安全屏障, 每一个 web 请求都先要经过 Sprin ...
- Spring Security实现统一登录与权限控制
1 项目介绍 最开始是一个单体应用,所有功能模块都写在一个项目里,后来觉得项目越来越大,于是决定把一些功能拆分出去,形成一个一个独立的微服务,于是就有个问题了,登录.退出.权限控制这些东西怎么办呢? ...
- spring security使用自定义登录界面后,不能返回到之前的请求界面的问题
昨天因为集成spring security oauth2,所以对之前spring security的配置进行了一些修改,然后就导致登录后不能正确跳转回被拦截的页面,而是返回到localhost根目录. ...
- Spring Security之动态配置资源权限
在Spring Security中实现通过数据库动态配置url资源权限,需要通过配置验证过滤器来实现资源权限的加载.验证.系统启动时,到数据库加载系统资源权限列表,当有请求访问时,通过对比系统资源权限 ...
随机推荐
- 算法金 | 读者问了个关于深度学习卷积神经网络(CNN)核心概念的问题
大侠幸会,在下全网同名[算法金] 0 基础转 AI 上岸,多个算法赛 Top [日更万日,让更多人享受智能乐趣] 读者问了个关于卷积神经网络核心概念的问题,如下, [问]神经元.权重.激活函数.参数 ...
- kettle从入门到精通 第四十一课 kettle 事务(单个转换文件)
1.大家都知道,我们在平常写java或者C#等代码时,如果涉及操作多个表时为了保持数据一致性需要开启事务,同样kettle也支持事务,今天我们一起来学习下kettle 单个转换文件内的事务特性. 转换 ...
- 使用final shell 连接使用 ubuntu server linux
书接上回,VM 安装ubuntu server:https://www.cnblogs.com/runliuv/p/16880599.html 1.从 https://www.hostbuf.com/ ...
- 手把手教你免费用Flashduty做消息通知
为什么需要消息通知? 如果有重要的情况发生,希望能通过各种媒介通知我们.可以举几个例子: 家里燃气费没有了,希望能有短信或者app通知 api频繁500报错,希望及时感知,及时修复 公司网站是http ...
- 性能分析: 快速定位SQL问题
在数据库性能调优的实践中,SQL性能分析是至关重要的一环.一个执行效率低下的SQL语句可能会导致整个系统的性能瓶颈. 为了快速定位并解决这些问题,我们需要对SQL进行性能分析.本文将介绍一些常用的方法 ...
- json 对象属性的输出顺序测试,fastJson 有序,jackson,gson无序(需代码中人工按约定来编码)接口数据签名规则
json 对象属性的输出顺序测试,fastJson 有序,jackson,gson无序(需代码中人工按约定来编码)接口数据签名规则 fastJson会根据对象的字段的首字母来排序.而jackson,g ...
- 用 Python 绘制现金流量图
目录 用 Python 绘制现金流量图 Python 实现 实现原理 具体代码 使用示例 1:根据现金流量表绘制现金流量图 使用示例 2:绘制等额.等差.等比序列现金流量图 等额序列现金流量图 等差序 ...
- AGC044C Strange Dance
在2020年A卷省选day2t2有类似操作trie的技巧. 题目链接 显然是建一棵三叉trie树,代表0/1/2 对这棵trie树,我们需要支持子树交换和全局加1 考虑第一个操作怎么做?直接打个懒标记 ...
- 在C#中进行单元测试
单元测试 前言 时隔多个月,终于抽空学习了点新知识,那么这次来记录一下C#怎么进行单元测试,单元测试是做什么的. 我相信大部分刚毕业的都很疑惑单元测试是干什么的?在小厂实习了6个月后,我发现每天除了写 ...
- Linux 内核:设备树(1)dtb格式
Linux 内核:设备树(1)dtb格式 背景 dtb作为二进制文件被加载到内存中,然后由内核读取并进行解析,如果对dtb文件的格式不了解,那么在看设备树解析相关的内核代码时将会寸步难行,而阅读源代码 ...