java-Spring Security-除非通过mappableAuthorities指定,否则在HttpServletRequest中不可访问的角色
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-Spring Security-除非通过mappableAuthorities指定,否则在HttpServletRequest中不可访问的角色,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3067字,纯文字阅读大概需要5分钟。
内容图文
![java-Spring Security-除非通过mappableAuthorities指定,否则在HttpServletRequest中不可访问的角色](/upload/InfoBanner/zyjiaocheng/666/6a6d1958271847b0b1f82f14bdf67f36.jpg)
我有一个在Tomcat应用程序服务器上运行并针对第三方IdP进行身份验证的Spring Boot Web应用程序.
目前,我们在许多应用程序中使用< security-role>进行基于角色的身份验证.和< security-constraint>在web.xml中,并且可以正常工作.
现在,尝试使用Spring Security,我添加了以下配置类:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
String[] publicPaths = /*get public paths from properties*/
String[] authorizedPaths = /*get authorized paths from properties*/
String[] authorizedRoles = /*get authorized roles from properties*/
http.csrf().disable()
.jee()
.mappableAuthorities(authorizedRoles)
.and()
.authorizeRequests()
.antMatchers(publicPaths).permitAll()
.antMatchers(authorizedPaths).hasAnyRole(authorizedRoles)
.and()
.logout().disable()
;
}
}
上面配置中的authorizedRoles是有权访问此应用程序的角色.但是,应用程序中还有其他手动检查,它们仅调用HttpServletRequest.isUserInRole()来确定用户是否具有特定角色.在使用Spring Security之前,如果该用户在原始请求中具有该角色,则该调用将返回true.添加Spring Boot之后,如果角色是上面示例中传递给.mappableAuthorities()的角色之一,则该调用仅返回true.通过HttpServletRequest.isUserInRole()检查的角色存储在数据库中,并且可以经常更新,因此在应用程序加载时将它们传递给.mappableAuthorities()是不可行的.
因此,就我的问题而言,Spring Security似乎正在修改原始的HttpServletRequest并承担传递给.mappableAuthorities()的authorizedRoles中未包含的所有角色.
有没有一种方法可以避免这种行为,或者可以将某种通配符传递给.mappableAuthorities(),这样您就不必知道应用程序启动时的所有角色,即可通过调用HttpServletRequest.isUserInRole()来访问它们. ?我已经看了几个小时的Spring Security文档,还没有发现任何东西.
解决方法:
您只能看到映射的角色,因为SecurityContextHolderAwareRequestFilter包装了HttpServletRequest:
A Filter which populates the ServletRequest with a request wrapper which implements the servlet API security methods.
它使用SecurityContextHolderAwareRequestWrapper来实现Servlet API安全性方法:
A Spring Security-aware
HttpServletRequestWrapper
, which uses theSecurityContext
-definedAuthentication
object to implement the servlet API security methods:
getUserPrincipal()
isUserInRole(String)
HttpServletRequestWrapper.getRemoteUser()
.
要自定义角色映射,请参见J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource:
Implementation of AuthenticationDetailsSource which converts the user’s J2EE roles (as obtained by calling
HttpServletRequest.isUserInRole(String)
) intoGrantedAuthority
s and stores these in the authentication details object.
它使用MappableAttributesRetriever获取可映射角色:
Interface to be implemented by classes that can retrieve a list of mappable security attribute strings (for example the list of all available J2EE roles in a web or EJB application).
您可以编写自己的MappableAttributesRetriever,以从数据库中加载可映射角色.
或者,您可以使用WebXmlMappableAttributesRetriever,它从web.xml中检索角色:
This
MappableAttributesRetriever
implementation reads the list of defined J2EE roles from a web.xml file and returns these fromgetMappableAttributes()
.
内容总结
以上是互联网集市为您收集整理的java-Spring Security-除非通过mappableAuthorities指定,否则在HttpServletRequest中不可访问的角色全部内容,希望文章能够帮你解决java-Spring Security-除非通过mappableAuthorities指定,否则在HttpServletRequest中不可访问的角色所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。