电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Security permitAll()不允許匿名訪問的操作

瀏覽:12日期:2023-07-08 09:48:08
Spring Security permitAll()不允許匿名訪問修改前

http.addFilterBefore(muiltpartFilter, ChannelProcessingFilter.class).addFilterBefore(cf, ChannelProcessingFilter.class).authorizeRequests() .anyRequest() .authenticated() .and().authorizeRequests() .antMatchers('/ping**') .permitAll() .and().formLogin() .loginPage('/login') .permitAll() .and().logout() .logoutUrl('/logout').logoutSuccessUrl('/login');修改后

http.addFilterBefore(muiltpartFilter, ChannelProcessingFilter.class).addFilterBefore(cf, ChannelProcessingFilter.class).authorizeRequests() .antMatchers('/ping**') .permitAll() .and().formLogin() .loginPage('/login') .permitAll() .and().authorizeRequests() .anyRequest() .authenticated() .and().logout() .logoutUrl('/logout').logoutSuccessUrl('/login');

permitAll() 順序很重要,如同在 XML 配置中,即把 authorizeRequests().anyRequest().authenticate 放到最后

Spring Security @PreAuthorize 攔截?zé)o效1. 在使用spring security的時候使用注解

@PreAuthorize('hasAnyRole(’ROLE_Admin’)')

放在對方法的訪問權(quán)限進(jìn)行控制失效,其中配置如:

@Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired UserDetailsService userDetailsService; @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userDetailsService); } @Override protected void configure(HttpSecurity http) throws Exception {http.csrf().disable() .authorizeRequests() .antMatchers('/res/**', '/login/login*').permitAll() .anyRequest().authenticated() .and().formLogin().loginPage('/login/login').defaultSuccessUrl('/').passwordParameter('password').usernameParameter('username') .and().logout().logoutSuccessUrl('/login/login'); }}

Controller中的方法如下:

@Controller@RequestMapping('/demo')public class DemoController extends CommonController{ @Autowired private UserService userService; @PreAuthorize('hasAnyRole(’ROLE_Admin’)') @RequestMapping(value = 'user-list') public void userList() { }}

使用一個沒有ROLE_Admin權(quán)限的用戶去訪問此方法發(fā)現(xiàn)無效。

修改一下 SecurityConfig:

  @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests() .antMatchers('/res/**', '/login/login*').permitAll() .antMatchers('/demo/user-list').access('hasRole(’ROLE_Admin’)') .anyRequest().authenticated() .and().formLogin().loginPage('/login/login').defaultSuccessUrl('/') .passwordParameter('password') .usernameParameter('username') .and().logout().logoutSuccessUrl('/login/login'); }

添加上:

.antMatchers('/demo/user-list').access('hasRole(’ROLE_Admin’)')

可以被正常攔截,說明是方法攔截沒有生效。

如果是基于xml,則需要在配置文件中加上:

<security:global-method-security pre-post-annotations='enabled' proxy-target- />

換成Annotation方式以后,則需要使用 @EnableGlobalMethodSecurity(prePostEnabled=true) 注解來開啟。

并且需要提供以下方法:

@Bean@Overridepublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}

才可正常攔截。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 水压力传感器_数字压力传感器|佛山一众传感仪器有限公司|首页 | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 苏州工作服定做-工作服定制-工作服厂家网站-尺品服饰科技(苏州)有限公司 | 齿轮减速机_齿轮减速电机-VEMT蜗轮蜗杆减速机马达生产厂家瓦玛特传动瑞环机电 | 黑龙江「京科脑康」医院-哈尔滨失眠医院_哈尔滨治疗抑郁症医院_哈尔滨精神心理医院 | 烟台条码打印机_烟台条码扫描器_烟台碳带_烟台数据采集终端_烟台斑马打印机-金鹏电子-金鹏电子 | 北京租车公司_汽车/客车/班车/大巴车租赁_商务会议/展会用车/旅游大巴出租_北京桐顺创业租车公司 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 烟台金蝶财务软件,烟台网站建设,烟台网络推广 | 层流手术室净化装修-检验科ICU改造施工-华锐净化工程-特殊科室建设厂家 | 自动部分收集器,进口无油隔膜真空泵,SPME固相微萃取头-上海楚定分析仪器有限公司 | 缠绕机|缠绕膜包装机|缠绕包装机-上海晏陵智能设备有限公司 | 二维运动混料机,加热型混料机,干粉混料机-南京腾阳干燥设备厂 | 上海佳武自动化科技有限公司 | 长江船运_国内海运_内贸船运_大件海运|运输_船舶运输价格_钢材船运_内河运输_风电甲板船_游艇运输_航运货代电话_上海交航船运 | 电竞馆加盟,沈阳网吧加盟费用选择嘉棋电竞_售后服务一体化 | 变色龙PPT-国内原创PPT模板交易平台 - PPT贰零 - 西安聚讯网络科技有限公司 | 西安耀程造价培训机构_工程预算实训_广联达实作实操培训 | 天津蒸汽/热水锅炉-电锅炉安装维修直销厂家-天津鑫淼暖通设备有限公司 | 泰安塞纳春天装饰公司【网站】 | 砂尘试验箱_淋雨试验房_冰水冲击试验箱_IPX9K淋雨试验箱_广州岳信试验设备有限公司 | 脱硫搅拌器厂家-淄博友胜不锈钢搅拌器厂家| 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | 智能汉显全自动量热仪_微机全自动胶质层指数测定仪-鹤壁市科达仪器仪表有限公司 | 台湾Apex减速机_APEX行星减速机_台湾精锐减速机厂家代理【现货】-杭州摩森机电 | 驾驶式洗地机/扫地机_全自动洗地机_工业洗地机_荣事达工厂官网 | 四川成人高考_四川成考报名网| 建大仁科-温湿度变送器|温湿度传感器|温湿度记录仪_厂家_价格-山东仁科 | 防爆大气采样器-防爆粉尘采样器-金属粉尘及其化合物采样器-首页|盐城银河科技有限公司 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 硫化罐-电加热蒸汽硫化罐生产厂家-山东鑫泰鑫智能装备有限公司 | 2025黄道吉日查询、吉时查询、老黄历查询平台- 黄道吉日查询网 | 304不锈钢无缝管_不锈钢管厂家 - 隆达钢业集团有限公司 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 谷歌关键词优化-外贸网站优化-Google SEO小语种推广-思亿欧外贸快车 | 泰安塞纳春天装饰公司【网站】 | 恒温槽_恒温水槽_恒温水浴槽-上海方瑞仪器有限公司 | 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 双效节能浓缩器-热回流提取浓缩机组-温州市利宏机械 | 艺术生文化课培训|艺术生文化课辅导冲刺-济南启迪学校 | 托盘租赁_塑料托盘租赁_托盘出租_栈板出租_青岛托盘租赁-优胜必达 |