Spring security 为啥那么难。。。

2018-11-27 18:03:57 +08:00
 liuzhaowei55

想用 Spring Boot + Spring security 配合做一个 REST API 的后端服务,两天了,还没达到自己想要的效果。
现在有一个需求就是 org.springframework.security.access.AccessDeniedException 这个异常应该怎么处理?全局异常抓不到,httpSecurity.getConfigurer(ExceptionHandlingConfigurer.class).accessDeniedHandler(new CustomAccessDeniedHandler()); 自定义一个 handler 也不起作用,有大佬可以给个相关文档吗?

5834 次点击
所在节点    程序员
7 条回复
Mrbird
2018-11-27 18:33:39 +08:00
httpSecurity.exceptionHandling().accessDeniedHandler(accessDeniedHandler())

我这样设置是有效的。
tomoya92
2018-11-27 18:52:20 +08:00
确实很难,还是换 shiro 吧,要简单的多
qwx
2018-11-27 19:13:21 +08:00
实际上 spring security 要是了解了他的整个处理链就不难了,一开始确实恶心,做了个 jwt 的应用就全理解了,用起来也爽很多。我也是楼上的 httpSecurity 中配置的,运行正常。
diaolizhi
2018-11-27 20:25:42 +08:00
@qwx 请问有什么资料推荐吗
br00k
2018-11-28 00:49:15 +08:00
jhipster 了解下
hengo
2018-11-28 03:14:41 +08:00
我也在做和你同样的操作,不是很会用 security
qinxi
2018-11-28 11:28:41 +08:00
我是这样的...httpSecurity.exceptionHandling().authenticationEntryPoint(new MyAuthenticationEntryPoint())
.accessDeniedHandler(new MyAccessDeniedHandlerImpl())
.and().addFilterBefore(new JWTAuthenticationFilter(),
UsernamePasswordAuthenticationFilter.class)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/512045

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX