当前位置: 首页 > news >正文

高端模板建站报价seo网上课程

高端模板建站报价,seo网上课程,做百度推广设置网站统计,微信商城网站搭建最近学习若依框架,里面的权限注解涉及到了SpEL表达式 PreAuthorize("ss.hasPermi(system:user:list)"),若依项目中用的是自己写的方法进行权限处理, 也可以只用security 来实现权限逻辑代码,下面写如何用security 实现。…

最近学习若依框架,里面的权限注解涉及到了SpEL表达式 @PreAuthorize("@ss.hasPermi('system:user:list')"),若依项目中用的是自己写的方法进行权限处理, 也可以只用security 来实现权限逻辑代码,下面写如何用security 实现。

security中  @PreAuthorize("hasPermission(Object target, Object permission)")  的hasPermission方法,是通过PermissionEvaluator实现,默认继承类是DenyAllPermissionEvaluator,所有方法返回false,所以注解后的结果只有拒绝权限;所以继承PermissionEvaluator重写hasPermission即可。

逻辑如下:

1、securityConfig文件上加入注解@EnableMethodSecurity,且引入自定义评估器:CustomPermissionEvaluator

2、编写CustomPermissionEvaluator文件

:security 6.2.1 中 EnableGlobalMethodSecurity 已弃用,改使用 EnableMethodSecurity 且prePostEnabled为ture,只需添加注解@EnableMethodSecurity 即可

package com.example.securityDemo.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;/*** @description: security配置类* @author: mml* @create: 2024/01/26*/
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {// 引入自定义评估器@Beanstatic MethodSecurityExpressionHandler methodSecurityExpressionHandler(CustomPermissionEvaluator permissionEvaluator) {DefaultMethodSecurityExpressionHandler handler = new DefaultMethodSecurityExpressionHandler();handler.setPermissionEvaluator(permissionEvaluator);return handler;}@BeanUserDetailsService userDetailsService(){UserDetails user = User.withDefaultPasswordEncoder().username("mml").password("123").roles("USER").authorities("system:user:update","system:user:list").build();return new InMemoryUserDetailsManager(user);}/*** 是Spring Security 过滤器链,Spring Security 中的所有功能都是通过这个链来提供的* @date 2024/1/26*/@BeanSecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {// 拦截所有请求,但是不经过任何过滤器
//        return new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**"));httpSecurity.authorizeHttpRequests(p -> p.anyRequest().authenticated()).csrf(c -> c.disable()).formLogin((form) -> form.loginPage("/login").permitAll());return httpSecurity.build();}}

自定义评估器代码

import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;import java.io.Serializable;
import java.util.Collection;/*** @description: 自定义权限评估器* @author: mml* @create: 2024/02/17*/
@Component
public class CustomPermissionEvaluator implements PermissionEvaluator {AntPathMatcher antPathMatcher = new AntPathMatcher();@Overridepublic boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {// 获取当前用户的角色Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();for (GrantedAuthority authority : authorities) {// 权限判断if (antPathMatcher.match(authority.getAuthority(), (String) permission)){// 说明有权限return true;}}return false;}@Overridepublic boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {return false;}
}

接口层:

@RestController
public class UserController {@RequestMapping("/list")@PreAuthorize("hasPermission('/list','system:user:list')")public String list() {return "get list ";}@RequestMapping("/add")@PreAuthorize("hasPermission('/add','system:user:add')")public String add() {return "post add ";}
}

可以使用postman测试

测试结果如下:

list-有权限

add 方法-没有权限

security参考链接:方法安全 :: Spring Security

http://www.hkea.cn/news/254539/

相关文章:

  • 凤岗网站仿做靠谱seo外包定制
  • xampp安装wordpress说明徐州seo外包
  • 啥网站都能看的浏览器下载百度收录查询工具
  • 福田附近公司做网站建设哪家效益快奶糖 seo 博客
  • 临沂免费自助建站模板品牌整合营销
  • iis做本地视频网站找客户资源的网站
  • 做调查用哪个网站网络推广有多少种方法
  • 开发一个交易网站多少钱在线工具
  • 网站平台怎么建立的软文范例
  • 移动应用开发专业学什么东莞seo软件
  • 做宣传网站的公司手机百度极速版app下载安装
  • 私人可以做慈善网站吗外贸如何推广
  • 网站页面模板页面布局如何成为百度广告代理商
  • 瑞安外贸网站建设曲靖百度推广
  • 先做网站还是服务器销售营销方案100例
  • 用卫生纸做的礼物街网站免费网页空间到哪申请
  • 手游网站做cpc还是cpm广告号厦门网页搜索排名提升
  • 人个做外贸用什么网站好宁波百度seo点击软件
  • 诈骗网站怎么做的企业网站seo案例分析
  • 如何做网站接口湖南营销型网站建设
  • 进入兔展网站做PPt软文营销ppt
  • app网站新闻危机公关
  • 东莞关键词优化实力乐云seo南宁seo外包服务商
  • 做网站都是用源码么免费注册个人网站不花钱
  • 建设网站需要两种服务支持官网设计公司
  • 安庆做网站seo建站收费地震
  • 绵阳住房和城市建设局网站官网seo排名优化联系13火星软件
  • 网站开发建设费用关键词异地排名查询
  • 网站建设企业电话广州优化疫情防控举措
  • 重庆模板网站建设百度网站域名注册