[英]How to use dynamic Role in Spring Security
我正在开发一个Web服务,该服务使用Spring Security工具箱通过“ Authority”授权请求。 自然,Web服务具有一个配置类,该配置类扩展到WebSecurityConfigurerAdapter
类,并且覆盖configure(HttpSecurity http)方法。
在该方法中,我使用以下代码编写了个人资料(角色或权威):
http
.authorizeRequests()
.antMatchers("/**").hasAnyAuthority("PERFIL")
.anyRequest().authenticated()
.and()
.logout().clearAuthentication(true)
.invalidateHttpSession(true)
.and()
.csrf().disable();
它工作得很好,但是我想从数据库中收取动态配置文件(角色或授权),因为我想更改它们而不更改Web服务。
有人知道怎么做吗?
问候。
为了从数据库传递这些详细信息,必须更改许多配置。
使用jdbc-user-service定义查询以执行数据库身份验证。
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query=
"select username,password, enabled from users where username=?"
authorities-by-username-query=
"select username, role from user_roles where username =? " />
</authentication-provider>
</authentication-manager>
按照本教程学习Spring Security的工作方式。
您可以在此处找到完整的工作示例。
尽管在我的项目中逻辑是相反的,但是您可以提取对您的情况有帮助的信息(例如如何“注入”这些权限以增强安全性)
在“权限”类中,我定义了2个静态权限 。 这是您可以从数据库中获取权威的地方。 您可能有一个空的List<Authority>
,它将在您的应用程序启动时自动从db中填充(请参阅@PostConstruct
)。
您的Authority类应该实现spring的GrantedAuthority ,如此处所示 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.