猿问

有一个Spring security相关的问题(动态授权)

一般的做法,在controller层,已经把权限指定到对应的方法上了,如:
PreAuthorize
Secured
RolesAllowed
但是,这样方式在调整权限的时候要改代码,最讨厌了,一边写代码,还要一边想着该给个什么样合适的权限。
这样,预期是编码的人完成接口后,在程序启动后,从DB中查询出来URL/或controller层方法对应的角色,然后直接调用SpringSecurity相关的权限扫描(如上述注解的初始处理),然后把权限加载进去。后续验证用户角色即可。
那么问题来了,如何找到PreAuthorize,Secured,RolesAllowed这些注解的初始化方法,而这些方法有没有提供公开的接口供程序调用?
有人能指点下,不胜感激。
偶然的你
浏览 931回答 2
2回答

汪汪一只猫

这里在springboot使用security框架。具体过程如下:classWebSecurityConfigurationextendsWebSecurityConfigurerAdapter{@AutowiredprivateCustomAuthenticationProviderauthProvider@Overrideprotectedvoidconfigure(AuthenticationManagerBuilderauth)throwsException{//自定义认证过程auth.authenticationProvider(authProvider)}}@ComponentclassCustomAuthenticationProviderimplementsAuthenticationProvider{@OverrideAuthenticationauthenticate(Authenticationauthentication)throwsAuthenticationException{//认证过程...//认证成功生成token,关键在于这个SimpleGrantedAuthorityListlist=newArrayList();list.put(newSimpleGrantedAuthority("Expert"))returnnewUsernamePasswordAuthenticationToken(session,token,list)}}//这里的Expert对应的就是SimpleGrantedAuthority类里的role@PostMapping("/add")@PreAuthorize("hasAuthority('Expert')")Responserelease(@RequestBodyRequestreq){//....}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答